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 { AadhaarResource } from "../../Resource/Identity/aadhaar"; import { BankResource } from "../../Resource/Identity/bank"; import { EmailResource } from "../../Resource/Identity/email"; import { MobileResource } from "../../Resource/Identity/mobile"; import { PanResource } from "../../Resource/Identity/pan"; import { PayTMResource } from "../../Resource/Identity/payTM"; import { UPIResource } from "../../Resource/Identity/upi"; import { UserNameResource } from "../../Resource/Identity/username"; import { IAadhaarBody } from "../../Model/Identity/aadhaar"; import { IBankBody } from "../../Model/Identity/bank"; import { IEmailBody } from "../../Model/Identity/email"; import { IMobileBody } from "../../Model/Identity/mobile"; import { IPanBody } from "../../Model/Identity/pan"; import { IUsernameBody } from "../../Model/Identity/username"; import { IPayTMBody } from "../../Model/Identity/paytm"; import { IUPI } from "../../Model/Identity/upi"; 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 IIdentityReqBody { email: string; mobile: IMobileBody; pan: IPanBody; aadhaar: IAadhaarBody; bank: IBankBody; payTM: IPayTMBody; vap: string; username: string; } @Path("profile") export class IdentityEndpoints { @Path("/:userId/identity") @PUT public async addUsersIdentity( @HeaderParam("Authorization") token: string, @PathParam("userId") ownerId: string, body: IIdentityReqBody ) { console.log(token, ownerId, body); /// Check madatory inputs if (!token || !ownerId || !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; // Begin Custom Code /// TODO: Need to add the identities to the system credentials. if (!body.email) { } else { console.log('ttttttttttttt'); let model = new EmailResource(MONGO_DB_NAME, SUPER_SECRET); await model.setInfo(ownerId, { emailId: body.email }, dbTxnId); } if (!body.mobile) { } else { let model = new MobileResource(MONGO_DB_NAME, SUPER_SECRET); await model.setInfo(ownerId, body.mobile, dbTxnId); } if (!body.pan) { } else { let model = new PanResource(MONGO_DB_NAME, SUPER_SECRET); await model.setInfo(ownerId, body.pan, dbTxnId); } if (!body.aadhaar) { } else { let model = new AadhaarResource(MONGO_DB_NAME, SUPER_SECRET); await model.setInfo(ownerId, body.aadhaar, dbTxnId); } if (!body.bank) { } else { let model = new BankResource(MONGO_DB_NAME, SUPER_SECRET); await model.setInfo(ownerId, body.bank, dbTxnId); } if (!body.username) { } else { let model = new UserNameResource(MONGO_DB_NAME, SUPER_SECRET); await model.setInfo(ownerId, {username: body.username}, dbTxnId); } if (!body.payTM) { } else { let model = new PayTMResource(MONGO_DB_NAME, SUPER_SECRET); await model.setInfo(ownerId, body.payTM, dbTxnId); } if (!body.vap) { } else { let model = new UPIResource(MONGO_DB_NAME, SUPER_SECRET); await model.setInfo(ownerId, body.vap, dbTxnId); } // End: Custom Code // await txn.commitTxn(); let res = new ObjectApiResponse(true, "Success", undefined); return res.toJSON(); } catch (e) { console.log(e); Logger.log(e, 'ERROR'); // if (txn) { // await txn.rollbackTxn(); // } let res = new SimpleApiResponse(false, "Failed", e.message); return res.toJSON(); } } }