2026-05-09 04:18:41 +00:00

483 lines
11 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 PokesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const pokes = await db.pokes.create(
{
id: data.id || undefined,
message: data.message
||
null
,
poked_at: data.poked_at
||
null
,
is_seen: data.is_seen
||
false
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await pokes.setFrom_user( data.from_user || null, {
transaction,
});
await pokes.setTo_user( data.to_user || null, {
transaction,
});
return pokes;
}
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 pokesData = data.map((item, index) => ({
id: item.id || undefined,
message: item.message
||
null
,
poked_at: item.poked_at
||
null
,
is_seen: item.is_seen
||
false
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const pokes = await db.pokes.bulkCreate(pokesData, { transaction });
// For each item created, replace relation files
return pokes;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const pokes = await db.pokes.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.message !== undefined) updatePayload.message = data.message;
if (data.poked_at !== undefined) updatePayload.poked_at = data.poked_at;
if (data.is_seen !== undefined) updatePayload.is_seen = data.is_seen;
updatePayload.updatedById = currentUser.id;
await pokes.update(updatePayload, {transaction});
if (data.from_user !== undefined) {
await pokes.setFrom_user(
data.from_user,
{ transaction }
);
}
if (data.to_user !== undefined) {
await pokes.setTo_user(
data.to_user,
{ transaction }
);
}
return pokes;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const pokes = await db.pokes.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of pokes) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of pokes) {
await record.destroy({transaction});
}
});
return pokes;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const pokes = await db.pokes.findByPk(id, options);
await pokes.update({
deletedBy: currentUser.id
}, {
transaction,
});
await pokes.destroy({
transaction
});
return pokes;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const pokes = await db.pokes.findOne(
{ where },
{ transaction },
);
if (!pokes) {
return pokes;
}
const output = pokes.get({plain: true});
output.from_user = await pokes.getFrom_user({
transaction
});
output.to_user = await pokes.getTo_user({
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: 'from_user',
where: filter.from_user ? {
[Op.or]: [
{ id: { [Op.in]: filter.from_user.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.from_user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.users,
as: 'to_user',
where: filter.to_user ? {
[Op.or]: [
{ id: { [Op.in]: filter.to_user.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.to_user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.message) {
where = {
...where,
[Op.and]: Utils.ilike(
'pokes',
'message',
filter.message,
),
};
}
if (filter.poked_atRange) {
const [start, end] = filter.poked_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
poked_at: {
...where.poked_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
poked_at: {
...where.poked_at,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.is_seen) {
where = {
...where,
is_seen: filter.is_seen,
};
}
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.pokes.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(
'pokes',
'message',
query,
),
],
};
}
const records = await db.pokes.findAll({
attributes: [ 'id', 'message' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['message', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.message,
}));
}
};