|
| 1 | +import { |
| 2 | + USER_NOT_AUTHORIZED_ERROR, |
| 3 | + USER_NOT_FOUND_ERROR, |
| 4 | + SAMPLE_ORGANIZATION_ALREADY_EXISTS, |
| 5 | +} from "../../constants"; |
| 6 | +import { errors, requestContext } from "../../libraries"; |
| 7 | +import type { MutationResolvers } from "../../types/generatedGraphQLTypes"; |
| 8 | +import { createSampleOrganization as createSampleOrgUtil } from "../../utilities/createSampleOrganizationUtil"; |
| 9 | +import { SampleData, User } from "../../models"; |
| 10 | + |
| 11 | +/** |
| 12 | + * Generates sample data for testing or development purposes. |
| 13 | + * @returns True if the sample data generation is successful, false otherwise. |
| 14 | + */ |
| 15 | +export const createSampleOrganization: MutationResolvers["createSampleOrganization"] = |
| 16 | + async (_parent, _args, _context) => { |
| 17 | + const currentUser = await User.findOne({ |
| 18 | + _id: _context.userId, |
| 19 | + }).lean(); |
| 20 | + |
| 21 | + if (!currentUser) { |
| 22 | + throw new errors.NotFoundError( |
| 23 | + requestContext.translate(USER_NOT_FOUND_ERROR.MESSAGE), |
| 24 | + USER_NOT_FOUND_ERROR.CODE, |
| 25 | + USER_NOT_FOUND_ERROR.PARAM |
| 26 | + ); |
| 27 | + } |
| 28 | + |
| 29 | + if ( |
| 30 | + !( |
| 31 | + currentUser.userType === "SUPERADMIN" || |
| 32 | + currentUser.userType === "ADMIN" |
| 33 | + ) |
| 34 | + ) { |
| 35 | + throw new errors.UnauthorizedError( |
| 36 | + requestContext.translate(USER_NOT_AUTHORIZED_ERROR.MESSAGE), |
| 37 | + USER_NOT_AUTHORIZED_ERROR.CODE, |
| 38 | + USER_NOT_AUTHORIZED_ERROR.PARAM |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + const existingOrganization = await SampleData.findOne({ |
| 43 | + collectionName: "Organization", |
| 44 | + }); |
| 45 | + |
| 46 | + if (existingOrganization) { |
| 47 | + throw new errors.UnauthorizedError( |
| 48 | + requestContext.translate(SAMPLE_ORGANIZATION_ALREADY_EXISTS.MESSAGE), |
| 49 | + SAMPLE_ORGANIZATION_ALREADY_EXISTS.CODE, |
| 50 | + SAMPLE_ORGANIZATION_ALREADY_EXISTS.PARAM |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + await createSampleOrgUtil(); |
| 55 | + return true; |
| 56 | + }; |
0 commit comments