import { LogAndError } from '@birbal/common-utilities/dist/error'; import {SystemIdentityModel, ISystemIdentity} from '../../Model/System/system-identity' import * as UUID from 'uuid'; import { MongoFactory } from '@birbal/mongo-utilities/dist/mongo-factory'; const DEFAULT_LIMIT = 10; const DEFAULT_SKIP = 0; export class SystemResource { private _DATABASE_NAME?: string; private _SUPER_SECRET?: string; constructor(dbName: string, superSecret: string) { this._DATABASE_NAME = dbName; this._SUPER_SECRET = superSecret; } async addIdentity( userId: string , userType: string , idValue: string , idType: string , dbTxnId?: string ): Promise { if (!userId || !userType || !idValue || !idType) { throw new LogAndError("Invalid input"); } if (!this._SUPER_SECRET || !this._DATABASE_NAME) { throw new LogAndError("Invalid input"); } try { let sysModel = new SystemIdentityModel(this._DATABASE_NAME, this._SUPER_SECRET); await sysModel.defineSchema(); let docId = await sysModel.getDocId(userType, idValue, idType); await sysModel.create(docId, { idType, idValue, userId, userType }, dbTxnId); return docId; } catch (e) { throw e; } } async removeIdentity( userType: string , idValue: string , idType: string , dbTxnId?: string ) { if (!userType || !idValue || !idType) { throw new LogAndError("Invalid input"); } if (!this._SUPER_SECRET || !this._DATABASE_NAME) { throw new LogAndError("Invalid input"); } try { let sysModel = new SystemIdentityModel(this._DATABASE_NAME, this._SUPER_SECRET); await sysModel.defineSchema(); let docId =await sysModel.getDocId(userType, idValue, idType); console.log("222222222222222222", docId); await sysModel.delete(docId, dbTxnId); } catch (e) { throw e; } } async advancedSearch( dbName: string , limit?: number , skip?: number , userId? : string , userType?: string , idValue?: string , idType?: string ): Promise<[number, Array]> { /** Check for madatory Inputs */ if (!this._SUPER_SECRET || !this._DATABASE_NAME) { throw new LogAndError("Invalid input"); } try { let params:ISystemIdentity = { }; if(userId) { params.userId = userId; } if(userType) { params.userType = userType; } if(idType) { params.idType = idType; } if(idValue) { params.idValue = idValue; } console.log("pppppppppppp", params); let db = await MongoFactory.getSingleton().getBirbalDatabase(dbName); let conn = await db.getDatabase(); let sysModel = new SystemIdentityModel(this._DATABASE_NAME, this._SUPER_SECRET); if (!limit) { limit = DEFAULT_LIMIT; } if (!skip) { skip = DEFAULT_SKIP; } let cursor = conn.collection(sysModel.TABLE_NAME).find(params); let count = await cursor.count(); let limitedCursor = cursor.limit(limit).skip(skip); let results = await limitedCursor.toArray(); return [count, results]; } catch (e) { throw e; } } }