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 { ProfileResource } from "../../Resource/Profile/profile"; 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"]; @Path("profile") export class ProfileEndpoints { @Path("/search") @GET public async advancedSearch( @HeaderParam("Authorization") token: string, @QueryParam("userId") userId: string ) { if (!userId) { let res = new SimpleApiResponse(false, "Invalid Params values"); return res.toJSON(); } // let txn: MongoTxn; 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 gameRes = new ProfileResource(MONGO_DB_NAME, SUPER_SECRET); let [count, results] = await gameRes.advancedSearch( MONGO_DB_NAME, userId ); // await txn.commitTxn(); let res = new ObjectApiResponse(true, "Success", undefined, { count: count, rows: results }); return res.toJSON(); } catch (e) { Logger.log(e, 'ERROR'); let res = new SimpleApiResponse(false, "Failed", e.message); return res.toJSON(); } } }