import { LogAndError } from '@birbal/common-utilities/dist/error'; import { EducationalInfo, IEducationalInfoBody, IEducationalInfo } from '../../Model/Demography/education'; import { VirtualBaseResource } from '../../Utility/Profile/virtual'; export class EducationInfoResource extends VirtualBaseResource { protected ID_TYPE = "educationInfo"; constructor(dbName: string, superSecret: string) { super(dbName, superSecret); } async setInfo(userId: string, details: IEducationalInfoBody, dbTxnId?: string): Promise { if (!userId || !details) { throw new LogAndError("Invalid userid or details"); } try { let [virtualId, state ]= await this.makeEntry(userId, dbTxnId); // console.log("wwwwwwww" , userId, details); // Set details let model = new EducationalInfo(this.DATABASE_NAME); await model.defineSchema(); let body: IEducationalInfo ={ educationInfo:{ virtualId: virtualId , info: details , status: state } } //console.log("wwwwwwww" , userId, details); 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"); } if (!this.SUPER_SECRET || !this.DATABASE_NAME) { throw new LogAndError("Invalid dbname or super secret"); } try { let [state, userId] = await this.updateStatus(virtualId, action, dbTxnId); let model = new EducationalInfo(this.DATABASE_NAME); await model.defineSchema(); let infoDoc = await model.find(userId ,dbTxnId); let info = model.getDataModel(infoDoc); if (!info || !info.educationInfo) { throw new LogAndError("Invalid info details"); } info.educationInfo.status = state; await model.update(userId, info, dbTxnId); } catch (e) { throw e; } } }