import { MongoModel } from '@birbal/mongo-utilities/dist/mongo-model'; import { LogAndError } from '@birbal/common-utilities/dist/error'; import { Document, Schema, SchemaTypes } from 'mongoose'; import { BirbalSuperSecret } from '@birbal/super-secret-utilities/dist/super-secret-validator'; import { BirbalCrypto } from '@birbal/common-utilities/dist/encryption'; export interface ISystemIdentity { _id?: string; userId?: string; userType?: string; idValue?: string; idType?: string; } /** Sequelize Schema */ export interface SystemIdentityDocument extends Document { _id: string; userId?: string; userType?: string; idValue?: string; idType?: string; } export class SystemIdentityModel extends MongoModel { TABLE_NAME = "profile_sys_identities"; private _SUPER_SECRET: string; constructor(dbName: string, superSecret: string) { super(dbName); this._SUPER_SECRET = superSecret; } getDataModel(doc: SystemIdentityDocument): ISystemIdentity { if (!doc) { throw new LogAndError("Invalid doc object"); } try { return { _id: doc._id , userId: doc.userId , userType: doc.userType , idValue: doc.idValue , idType: doc.idType } } catch (e) { throw e; } } async defineSchema() { try { if (!this.DATABASE_NAME || !this.TABLE_NAME) { throw new LogAndError('Invalid db name/ tbl name'); } let schema = new Schema({ _id: SchemaTypes.String , userId: SchemaTypes.String , userType: SchemaTypes.String , idValue: SchemaTypes.String , idType: SchemaTypes.String }, {collection: this.TABLE_NAME}); await this.define(schema); } catch (e) { throw e; } } async getDocId( userType: string, idValue: string, idType: string ): Promise { if (!idValue || !idType || !userType) { throw new LogAndError("Invalid input") } try { await BirbalSuperSecret.validate(this._SUPER_SECRET); let encValue = await BirbalCrypto.encrypt(idValue, this._SUPER_SECRET); return `${userType}-${idType}-${encValue}` } catch (e) { throw e; } } }