Skip to content

Instantly share code, notes, and snippets.

@harryWonder
Created December 8, 2023 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harryWonder/8eb5682c2dca2e5e27fd9a4ad72652c2 to your computer and use it in GitHub Desktop.
Save harryWonder/8eb5682c2dca2e5e27fd9a4ad72652c2 to your computer and use it in GitHub Desktop.
const { omit } = require('lodash');
const DataSource = require('./data-source');
const { DataTypes } = require('sequelize');
const photo_log = require('../dao/photo_log.dao');
class PhotoEntity {
/** @type {import('sequelize').Model} */
photoModel = {}
constructor() {
/** @type {import('sequelize').Model} */
const Photo = DataSource.define('Photo', {
id: {
allowNull: false,
primaryKey: true,
type: DataTypes.UUID,
defaultValue: Sequelize.UUIDV4,
unique: true,
},
face_id: {
type: DataTypes.STRING(60),
},
image_id: {
type: DataTypes.STRING(60),
},
filename: {
type: DataTypes.STRING(120),
},
bvn_filename: {
type: DataTypes.STRING(120),
},
duplicate_image: {
type: DataTypes.INTEGER(5),
defaultValue: 0,
},
duplicate_image_count: {
type: DataTypes.INTEGER(11),
defaultValue: 0,
},
duplicate_image_destination: {
type: DataTypes.STRING(25),
},
upload_status: {
type: DataTypes.INTEGER(5),
defaultValue: 0,
},
status: {
type: DataTypes.INTEGER(5),
defaultValue: 1,
},
similarity: {
type: Sequelize.DECIMAL(11, 4),
defaultValue: 0,
get() {
const value = this.getDataValue('similarity');
return value === null ? null : parseFloat(value);
},
},
bvn: {
type: Sequelize.STRING(15),
},
bvn_profile_similarity: {
type: Sequelize.INTEGER(11),
defaultValue: 0,
},
image_processed: {
type: Sequelize.INTEGER(11),
defaultValue: 0,
},
liveness: {
type: DataTypes.STRING(25),
},
message: {
type: DataTypes.STRING(100),
},
}, {
modelName: 'Photo',
tableName: 'Photos',
paranoid: true,
underscored: true,
updatedAt: 'updated_at',
createdAt: 'created_at'
});
this.photoModel = DataSource.models.Photo || Photo;
}
async afterCreateHook() {
return this.photoModel.addHook('afterCreate', async(data) => {
const cacheObj = omit(data.get({ plain: true }), ['deleted_at', 'deletedAt']);
cacheObj.user_id = data.id;
delete cacheObj.id;
try {
await photo_log.insert(cacheObj);
} catch (err) { console.log(err, 'Photo After Create Hook Failed!') }
});
}
async afterUpdateHook() {
return this.photoModel.addHook('afterUpdate', async(data) => {
const cacheObj = omit(data.get({ plain: true }), ['deleted_at', 'deletedAt']);
cacheObj.user_id = data.id;
delete cacheObj.id;
try {
await photo_log.insert(cacheObj);
} catch (err) { console.log(err, 'Photo After Update Hook Failed!') }
});
}
}
const photoEntity = new PhotoEntity();
// Release the hooks
photoEntity.afterCreateHook()
.then((resp) => console.log(resp, 'Listening for create || insert hooks on Photo Models...'));
photoEntity.afterUpdateHook()
.then((resp) => console.log(resp, 'Listening for update hooks on Photo Models...'));
/** @type {import('sequelize').Model} */
module.exports = photoEntity.photoModel;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment