import { Server, Path, GET, POST, PathParam, HeaderParam, QueryParam, FormParam, PUT, DELETE, PATCH } from "typescript-rest"; import { LogAndError } from "@birbal/common-utilities/dist/error"; import { MongoTxn } from "@birbal/mongo-utilities/dist/mongo-txn"; import { MongoTxnRegistry } from "@birbal/mongo-utilities/dist/mongo-txn-registry"; import { SimpleApiResponse, ObjectApiResponse } from "@birbal/api-utilities/dist/api-response"; import { AvatarInfoResource } from "../../Resource/Demography/avatar"; import { PersonalInfoResource } from "../../Resource/Demography/personal-info"; import { PrimaryAddressResource } from "../../Resource/Demography/primary-address"; import { SecondaryAddressResource } from "../../Resource/Demography/secondary-address"; import { SpecializationResource } from "../../Resource/Demography/specialization"; import { EducationInfoResource } from "../../Resource/Demography/education"; import { ExperienceInfoResource } from "../../Resource/Demography/experience"; import { ProfileResource } from "../../Resource/Profile/profile"; import { IEducationalInfoBody } from "../../Model/Demography/education"; import { IExperienceInfoBody, ExperienceInfoModel } from "../../Model/Demography/experience"; import { IPersonalInfoBody } from "../../Model/Demography/personal-info"; import { ISecondaryAddressBody } from "../../Model/Demography/secondary-address"; import { ISpecializationInfoBody } from "../../Model/Demography/specialization"; import { IPrimaryAddressBody } from "../../Model/Demography/primary-address"; import { Authorization } from "../authorization"; import { Logger } from '@birbal/common-utilities/dist/logger'; const MONGO_DB_NAME = process.env["BIRBAL_MAIN_DATABASE"]; const SUPER_SECRET = process.env["BIRBAL_SUPER_SECRET"]; interface IDemographyReqBody { avatarUrl?: string; education?: IEducationalInfoBody; experience?: IExperienceInfoBody; specialization?: ISpecializationInfoBody; personalInfo?: IPersonalInfoBody; primaryAddress?: IPrimaryAddressBody; secondaryAddress?: ISecondaryAddressBody; } @Path("profile") export class DemographyEndpoints { @Path("/:userId/demography") @PUT public async addUser( @HeaderParam("Authorization") token: string, @PathParam("userId") userId: string, body: IDemographyReqBody ) { if (!userId || !body) { throw new LogAndError("Invalid account details"); } // let txn: MongoTxn | undefined; try { /// Validate authorization let auth = new Authorization(token, SUPER_SECRET); await auth.validate(); // txn = await MongoTxnRegistry.getSingleton().registerNewTxn(MONGO_DB_NAME); // await txn.beginTxn(); let dbTxnId = undefined;// txn.id; if (!body.avatarUrl) { } else { let model = new AvatarInfoResource(MONGO_DB_NAME, SUPER_SECRET); await model.setInfo(userId, { avatarUrl: body.avatarUrl }, dbTxnId); } if (!body.education) { } else { let model = new EducationInfoResource(MONGO_DB_NAME, SUPER_SECRET); await model.setInfo(userId, body.education, dbTxnId); } if (!body.experience) { } else { let model = new ExperienceInfoResource(MONGO_DB_NAME, SUPER_SECRET); await model.setInfo(userId, body.experience, dbTxnId); } if (!body.personalInfo) { } else { let model = new PersonalInfoResource(MONGO_DB_NAME, SUPER_SECRET); await model.setInfo(userId, body.personalInfo, dbTxnId); } if (!body.primaryAddress) { } else { let model = new PrimaryAddressResource(MONGO_DB_NAME, SUPER_SECRET); await model.setInfo(userId, body.primaryAddress, dbTxnId); } if (!body.secondaryAddress) { } else { let model = new SecondaryAddressResource(MONGO_DB_NAME, SUPER_SECRET); await model.setInfo(userId, body.secondaryAddress, dbTxnId); } if (!body.specialization) { } else { let model = new SpecializationResource(MONGO_DB_NAME, SUPER_SECRET); await model.setInfo(userId, body.specialization, dbTxnId); } // await txn.commitTxn(); let res = new ObjectApiResponse(true, "Success", undefined); return res.toJSON(); } catch (e) { // if (txn) { // await txn.rollbackTxn(); // } Logger.log(e, 'ERROR'); let res = new SimpleApiResponse(false, "Failed", e.message); return res.toJSON(); } } }