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