39588-vm/backend/src/db/api/fraud_flags.js
2026-04-12 11:09:01 +00:00

551 lines
13 KiB
JavaScript

const db = require('../models');
const FileDBApi = require('./file');
const crypto = require('crypto');
const Utils = require('../utils');
const Sequelize = db.Sequelize;
const Op = Sequelize.Op;
module.exports = class Fraud_flagsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const fraud_flags = await db.fraud_flags.create(
{
id: data.id || undefined,
severity: data.severity
||
null
,
status: data.status
||
null
,
reason: data.reason
||
null
,
flagged_at: data.flagged_at
||
null
,
resolved_at: data.resolved_at
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await fraud_flags.setUser( data.user || null, {
transaction,
});
await fraud_flags.setScan_session( data.scan_session || null, {
transaction,
});
return fraud_flags;
}
static async bulkImport(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
// Prepare data - wrapping individual data transformations in a map() method
const fraud_flagsData = data.map((item, index) => ({
id: item.id || undefined,
severity: item.severity
||
null
,
status: item.status
||
null
,
reason: item.reason
||
null
,
flagged_at: item.flagged_at
||
null
,
resolved_at: item.resolved_at
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const fraud_flags = await db.fraud_flags.bulkCreate(fraud_flagsData, { transaction });
// For each item created, replace relation files
return fraud_flags;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const fraud_flags = await db.fraud_flags.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.severity !== undefined) updatePayload.severity = data.severity;
if (data.status !== undefined) updatePayload.status = data.status;
if (data.reason !== undefined) updatePayload.reason = data.reason;
if (data.flagged_at !== undefined) updatePayload.flagged_at = data.flagged_at;
if (data.resolved_at !== undefined) updatePayload.resolved_at = data.resolved_at;
updatePayload.updatedById = currentUser.id;
await fraud_flags.update(updatePayload, {transaction});
if (data.user !== undefined) {
await fraud_flags.setUser(
data.user,
{ transaction }
);
}
if (data.scan_session !== undefined) {
await fraud_flags.setScan_session(
data.scan_session,
{ transaction }
);
}
return fraud_flags;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const fraud_flags = await db.fraud_flags.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of fraud_flags) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of fraud_flags) {
await record.destroy({transaction});
}
});
return fraud_flags;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const fraud_flags = await db.fraud_flags.findByPk(id, options);
await fraud_flags.update({
deletedBy: currentUser.id
}, {
transaction,
});
await fraud_flags.destroy({
transaction
});
return fraud_flags;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const fraud_flags = await db.fraud_flags.findOne(
{ where },
{ transaction },
);
if (!fraud_flags) {
return fraud_flags;
}
const output = fraud_flags.get({plain: true});
output.user = await fraud_flags.getUser({
transaction
});
output.scan_session = await fraud_flags.getScan_session({
transaction
});
return output;
}
static async findAll(
filter,
options
) {
const limit = filter.limit || 0;
let offset = 0;
let where = {};
const currentPage = +filter.page;
offset = currentPage * limit;
const orderBy = null;
const transaction = (options && options.transaction) || undefined;
let include = [
{
model: db.users,
as: 'user',
where: filter.user ? {
[Op.or]: [
{ id: { [Op.in]: filter.user.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.camera_scan_sessions,
as: 'scan_session',
where: filter.scan_session ? {
[Op.or]: [
{ id: { [Op.in]: filter.scan_session.split('|').map(term => Utils.uuid(term)) } },
{
status: {
[Op.or]: filter.scan_session.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.reason) {
where = {
...where,
[Op.and]: Utils.ilike(
'fraud_flags',
'reason',
filter.reason,
),
};
}
if (filter.flagged_atRange) {
const [start, end] = filter.flagged_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
flagged_at: {
...where.flagged_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
flagged_at: {
...where.flagged_at,
[Op.lte]: end,
},
};
}
}
if (filter.resolved_atRange) {
const [start, end] = filter.resolved_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
resolved_at: {
...where.resolved_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
resolved_at: {
...where.resolved_at,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.severity) {
where = {
...where,
severity: filter.severity,
};
}
if (filter.status) {
where = {
...where,
status: filter.status,
};
}
if (filter.createdAtRange) {
const [start, end] = filter.createdAtRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
['createdAt']: {
...where.createdAt,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
['createdAt']: {
...where.createdAt,
[Op.lte]: end,
},
};
}
}
}
const queryOptions = {
where,
include,
distinct: true,
order: filter.field && filter.sort
? [[filter.field, filter.sort]]
: [['createdAt', 'desc']],
transaction: options?.transaction,
logging: console.log
};
if (!options?.countOnly) {
queryOptions.limit = limit ? Number(limit) : undefined;
queryOptions.offset = offset ? Number(offset) : undefined;
}
try {
const { rows, count } = await db.fraud_flags.findAndCountAll(queryOptions);
return {
rows: options?.countOnly ? [] : rows,
count: count
};
} catch (error) {
console.error('Error executing query:', error);
throw error;
}
}
static async findAllAutocomplete(query, limit, offset, ) {
let where = {};
if (query) {
where = {
[Op.or]: [
{ ['id']: Utils.uuid(query) },
Utils.ilike(
'fraud_flags',
'reason',
query,
),
],
};
}
const records = await db.fraud_flags.findAll({
attributes: [ 'id', 'reason' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['reason', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.reason,
}));
}
};