import { LogAndError } from '@birbal/common-utilities/dist/error'; import { AadhaarModel, IAadhaarBody, IAadhaar} from '../../Model/Identity/aadhaar'; import { VirtualBaseResource } from '../../Utility/Profile/virtual'; export class AadhaarResource extends VirtualBaseResource { protected ID_TYPE = "aadhaar" constructor(dbName: string, superSecret: string) { super(dbName, superSecret); } async setInfo(userId: string, details: IAadhaarBody, dbTxnId?: string): Promise { if (!userId || !details) { throw new LogAndError("Invalid user id or details in identity resource"); } try { let [virtualId, state] = await this.makeEntry(userId, dbTxnId); // Set details let model = new AadhaarModel(this.DATABASE_NAME); await model.defineSchema(); let body: IAadhaar = { aadhaar: { virtualId: virtualId , info: details , status: state } } await model.upsert(userId, body, dbTxnId); return virtualId; } catch (e) { throw e; } } async performAction( virtualId: string , action: 'remove'|'reject'|'approve'|'expire'|'revoke' , dbTxnId?: string ) { if (!virtualId || !action) { throw new LogAndError("Invalid virtual id or action in identity resource"); } if (!this.SUPER_SECRET || !this.DATABASE_NAME) { throw new LogAndError("Invalid database name or super secret in identity resource"); } try { let [state, userId] = await this.updateStatus(virtualId, action, dbTxnId); let model = new AadhaarModel(this.DATABASE_NAME); await model.defineSchema(); let infoDoc = await model.find(userId ,dbTxnId); let info = model.getDataModel(infoDoc); if (!info || !info.aadhaar ) { throw new LogAndError("Invalid info details in identity resource"); } info.aadhaar.status = state; await model.update(userId, info, dbTxnId); } catch (e) { throw e; } } }