Last active
April 21, 2021 20:54
-
-
Save dimitrisnl/ea9a751e28ca3cd54618921360b17025 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import {Teammate} from '@prisma/client'; | |
| import Joi from 'joi'; | |
| import type {NextApiRequest, NextApiResponse} from 'next'; | |
| import {dbClient} from '@/b/db/db-client'; | |
| import {getUserMetadata} from '@/b/utils/get-all-user-metadata'; | |
| import {getSessionOrThrow} from '@/b/utils/get-user-session'; | |
| import {validateSchema} from '@/b/utils/validate-schema'; | |
| const DEFAULT_LIMIT = 20; | |
| const schema = Joi.object({ | |
| page: Joi.number().min(1).default(1), | |
| limit: Joi.number().min(1).max(100).default(DEFAULT_LIMIT), | |
| searchTerm: Joi.string().trim().optional().empty(''), | |
| }); | |
| export type GetAllTeammatesProps = { | |
| page?: number; | |
| limit?: number; | |
| searchTerm?: string; | |
| }; | |
| export type GetAllTeammatesData = { | |
| data: Teammate[]; | |
| pagination: { | |
| total: number; | |
| pageCount: number; | |
| currentPage: number; | |
| perPage: number; | |
| from: number; | |
| to: number; | |
| }; | |
| }; | |
| export async function getAllTeammates( | |
| req: NextApiRequest, | |
| res: NextApiResponse | |
| ): Promise<GetAllTeammatesData> { | |
| try { | |
| const {userEmail} = await getSessionOrThrow(req); | |
| const metadata = validateSchema<GetAllTeammatesProps>(schema, req.query); | |
| const user = await getUserMetadata(userEmail); | |
| const {page = 1, limit = DEFAULT_LIMIT, searchTerm} = metadata; | |
| const searchTermFilter = searchTerm | |
| ? {OR: [{name: {contains: searchTerm}}, {email: {contains: searchTerm}}]} | |
| : {}; | |
| const teammatesCount = await dbClient.teammate.count({ | |
| where: {orgId: user.membership.orgId, ...searchTermFilter}, | |
| }); | |
| const teammates = await dbClient.teammate.findMany({ | |
| where: {orgId: user.membership.orgId, ...searchTermFilter}, | |
| skip: limit * (page - 1), | |
| take: limit, | |
| }); | |
| const response = { | |
| data: teammates, | |
| pagination: { | |
| total: teammatesCount, | |
| pageCount: Math.ceil(teammatesCount / limit), | |
| currentPage: page, | |
| perPage: limit, | |
| from: (page - 1) * limit + 1, | |
| to: (page - 1) * limit + teammates.length, | |
| }, | |
| }; | |
| return res.status(200).json(response); | |
| } catch (error) { | |
| console.log(error); | |
| res.status(422).json({message: error.message}); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment