38185-vm/backend/src/db/api/people.js
2026-02-04 13:29:57 +00:00

545 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 PeopleDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const people = await db.people.create(
{
id: data.id || undefined,
full_name: data.full_name
||
null
,
native_name: data.native_name
||
null
,
person_type: data.person_type
||
null
,
bio: data.bio
||
null
,
birth_date: data.birth_date
||
null
,
country: data.country
||
null
,
is_active: data.is_active
||
false
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.people.getTableName(),
belongsToColumn: 'profile_image',
belongsToId: people.id,
},
data.profile_image,
options,
);
return people;
}
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 peopleData = data.map((item, index) => ({
id: item.id || undefined,
full_name: item.full_name
||
null
,
native_name: item.native_name
||
null
,
person_type: item.person_type
||
null
,
bio: item.bio
||
null
,
birth_date: item.birth_date
||
null
,
country: item.country
||
null
,
is_active: item.is_active
||
false
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const people = await db.people.bulkCreate(peopleData, { transaction });
// For each item created, replace relation files
for (let i = 0; i < people.length; i++) {
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.people.getTableName(),
belongsToColumn: 'profile_image',
belongsToId: people[i].id,
},
data[i].profile_image,
options,
);
}
return people;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const people = await db.people.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.full_name !== undefined) updatePayload.full_name = data.full_name;
if (data.native_name !== undefined) updatePayload.native_name = data.native_name;
if (data.person_type !== undefined) updatePayload.person_type = data.person_type;
if (data.bio !== undefined) updatePayload.bio = data.bio;
if (data.birth_date !== undefined) updatePayload.birth_date = data.birth_date;
if (data.country !== undefined) updatePayload.country = data.country;
if (data.is_active !== undefined) updatePayload.is_active = data.is_active;
updatePayload.updatedById = currentUser.id;
await people.update(updatePayload, {transaction});
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.people.getTableName(),
belongsToColumn: 'profile_image',
belongsToId: people.id,
},
data.profile_image,
options,
);
return people;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const people = await db.people.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of people) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of people) {
await record.destroy({transaction});
}
});
return people;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const people = await db.people.findByPk(id, options);
await people.update({
deletedBy: currentUser.id
}, {
transaction,
});
await people.destroy({
transaction
});
return people;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const people = await db.people.findOne(
{ where },
{ transaction },
);
if (!people) {
return people;
}
const output = people.get({plain: true});
output.title_people_person = await people.getTitle_people_person({
transaction
});
output.profile_image = await people.getProfile_image({
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.file,
as: 'profile_image',
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.full_name) {
where = {
...where,
[Op.and]: Utils.ilike(
'people',
'full_name',
filter.full_name,
),
};
}
if (filter.native_name) {
where = {
...where,
[Op.and]: Utils.ilike(
'people',
'native_name',
filter.native_name,
),
};
}
if (filter.bio) {
where = {
...where,
[Op.and]: Utils.ilike(
'people',
'bio',
filter.bio,
),
};
}
if (filter.country) {
where = {
...where,
[Op.and]: Utils.ilike(
'people',
'country',
filter.country,
),
};
}
if (filter.birth_dateRange) {
const [start, end] = filter.birth_dateRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
birth_date: {
...where.birth_date,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
birth_date: {
...where.birth_date,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.person_type) {
where = {
...where,
person_type: filter.person_type,
};
}
if (filter.is_active) {
where = {
...where,
is_active: filter.is_active,
};
}
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.people.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(
'people',
'full_name',
query,
),
],
};
}
const records = await db.people.findAll({
attributes: [ 'id', 'full_name' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['full_name', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.full_name,
}));
}
};