Skip to content

Commit

Permalink
calculateResults mappers took in transformResponse organizationApi
Browse files Browse the repository at this point in the history
  • Loading branch information
levil664 committed Jul 5, 2024
1 parent 3e77fdc commit c629aa0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 44 deletions.
4 changes: 2 additions & 2 deletions apps/schools/domains/common/redux/serializers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ interface GetStudentProfile {
id?: string
name: string
age?: number
phone?: string
phone: string
photo: GetPhoto
parent_names?: string
parent_phones?: string
parent_phones: string
}

export interface GetAnalytics {
Expand Down
23 changes: 23 additions & 0 deletions apps/schools/domains/organization/redux/organizationApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { mapReturnedData } from '@domains/common/redux/utils'
import { TableType as TableTypeCircle } from '@domains/circle/components/circleList/interfaces'
import { TableType as TableTypeTickets } from '@domains/ticket/components/ticketList/interfaces'
import { TableType as TableTypeQuery } from '@domains/query/components/queryList/interfaces'
import { TableType as TableTypeStudent } from '@domains/student/components/studentList/interfaces'

const organizationApi = commonApi.injectEndpoints({
endpoints: (build) => ({
Expand Down Expand Up @@ -70,6 +71,17 @@ const organizationApi = commonApi.injectEndpoints({
method: 'GET',
params: params,
}),
transformResponse: (response: ReturnedData<GetStudent[]>) => {
return mapReturnedData(response, (query) => {
const transformedQuery = structuredClone(query) as unknown as TableTypeStudent
transformedQuery.id = query.id
transformedQuery.student_name = query.name
transformedQuery.student_phone = query.student_profile.phone
transformedQuery.parent_phone = query.student_profile.parent_phones?.replaceAll(',', '\n')
transformedQuery.circle_name = query.circle.name
return transformedQuery
})!
},
providesTags: (result) => providesList(result?.results, 'Student'),
}),
getAllCircles: build.query<ReturnedData<TableTypeCircle[]>, GetOrganizationCircleListData>({
Expand Down Expand Up @@ -168,6 +180,17 @@ const organizationApi = commonApi.injectEndpoints({
method: 'GET',
params: params,
}),
transformResponse: (response: ReturnedData<GetCircleInviteStudent[]>) => {
return mapReturnedData(response, (query) => {
const transformedQuery = structuredClone(query) as unknown as TableTypeStudent
transformedQuery.id = query.body.id
transformedQuery.student_name = query.body.name
transformedQuery.student_phone = query.additional.phone
transformedQuery.parent_phone = query.recipient.parent_phones.replaceAll(',', '\n')
transformedQuery.circle_name = query.sender.name
return transformedQuery
})!
},
providesTags: (result) => providesList(result?.results, 'Student'),
}),
getAllJoinCircleQueries: build.query<ReturnedData<GetStudentJoinCircle[]>, AllStudentJoinCircleQueriesData>({
Expand Down
45 changes: 3 additions & 42 deletions apps/schools/domains/student/handlers/resultsCalculate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,11 @@ export const calculateResults = (
students: { count: number | undefined; results: any[] } | undefined,
): RowType[] => {
if (paginationParams.page < Math.ceil((invites?.count ?? 0) / paginationParams.pageSize)) {
return (invites?.results ?? []).map((x) => {
return {
id: x.body.id,
student_name: x.body.name,
student_phone: x.additional.phone,
parent_phone: x.recipient.parent_phones.replaceAll(',', '\n'),
circle_name: x.sender.name,
} as RowType
})
return invites?.results ?? []
} else if (paginationParams.page === Math.ceil((invites?.count ?? 0) / paginationParams.pageSize)) {
return [...(invites?.results ?? []), ...(students?.results ?? [])]
.slice(0, paginationParams.pageSize)
.map((x) => {
if ('body' in x) {
return {
id: x.body.id,
student_name: x.body.name,
student_phone: x.additional.phone,
parent_phone: x.recipient.parent_phones.replaceAll(',', '\n'),
circle_name: x.sender.name,
} as RowType
} else {
return {
id: x.id,
student_name: x.name,
student_phone: x.student_profile.phone,
parent_phone: x.student_profile.parent_phones?.replaceAll(',', '\n'),
circle_name: x.circle.name,
} as RowType
}
})
return [...(invites?.results ?? []), ...(students?.results ?? [])].slice(0, paginationParams.pageSize)
} else {
const temp = paginationParams.pageSize - ((invites?.count ?? 0) % paginationParams.pageSize)
const mapper = (x: any) =>
({
id: x.id,
student_name: x.name,
student_phone: x.student_profile.phone,
parent_phone: x.student_profile.parent_phones?.replaceAll(',', '\n'),
circle_name: x.circle.name,
}) as RowType

return students?.results.length === 1
? [mapper(students?.results[0])]
: (students?.results ?? []).slice(temp).map(mapper)
return students?.results.length === 1 ? [students?.results[0]] : (students?.results ?? []).slice(temp)
}
}

0 comments on commit c629aa0

Please sign in to comment.