553 lines
13 KiB
JavaScript
553 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 InvitationsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const invitations = await db.invitations.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
channel: data.channel
|
|
||
|
|
null
|
|
,
|
|
|
|
sent_at: data.sent_at
|
|
||
|
|
null
|
|
,
|
|
|
|
responded_at: data.responded_at
|
|
||
|
|
null
|
|
,
|
|
|
|
invite_code: data.invite_code
|
|
||
|
|
null
|
|
,
|
|
|
|
message: data.message
|
|
||
|
|
null
|
|
,
|
|
|
|
delivery_status: data.delivery_status
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await invitations.setEvent( data.event || null, {
|
|
transaction,
|
|
});
|
|
|
|
await invitations.setGuest( data.guest || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return invitations;
|
|
}
|
|
|
|
|
|
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 invitationsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
channel: item.channel
|
|
||
|
|
null
|
|
,
|
|
|
|
sent_at: item.sent_at
|
|
||
|
|
null
|
|
,
|
|
|
|
responded_at: item.responded_at
|
|
||
|
|
null
|
|
,
|
|
|
|
invite_code: item.invite_code
|
|
||
|
|
null
|
|
,
|
|
|
|
message: item.message
|
|
||
|
|
null
|
|
,
|
|
|
|
delivery_status: item.delivery_status
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const invitations = await db.invitations.bulkCreate(invitationsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return invitations;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const invitations = await db.invitations.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.channel !== undefined) updatePayload.channel = data.channel;
|
|
|
|
|
|
if (data.sent_at !== undefined) updatePayload.sent_at = data.sent_at;
|
|
|
|
|
|
if (data.responded_at !== undefined) updatePayload.responded_at = data.responded_at;
|
|
|
|
|
|
if (data.invite_code !== undefined) updatePayload.invite_code = data.invite_code;
|
|
|
|
|
|
if (data.message !== undefined) updatePayload.message = data.message;
|
|
|
|
|
|
if (data.delivery_status !== undefined) updatePayload.delivery_status = data.delivery_status;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await invitations.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.event !== undefined) {
|
|
await invitations.setEvent(
|
|
|
|
data.event,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.guest !== undefined) {
|
|
await invitations.setGuest(
|
|
|
|
data.guest,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return invitations;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const invitations = await db.invitations.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of invitations) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of invitations) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return invitations;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const invitations = await db.invitations.findByPk(id, options);
|
|
|
|
await invitations.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await invitations.destroy({
|
|
transaction
|
|
});
|
|
|
|
return invitations;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const invitations = await db.invitations.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!invitations) {
|
|
return invitations;
|
|
}
|
|
|
|
const output = invitations.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.event = await invitations.getEvent({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.guest = await invitations.getGuest({
|
|
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.events,
|
|
as: 'event',
|
|
|
|
where: filter.event ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.event.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
name: {
|
|
[Op.or]: filter.event.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.guests,
|
|
as: 'guest',
|
|
|
|
where: filter.guest ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.guest.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
last_name: {
|
|
[Op.or]: filter.guest.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.invite_code) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'invitations',
|
|
'invite_code',
|
|
filter.invite_code,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.message) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'invitations',
|
|
'message',
|
|
filter.message,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.sent_atRange) {
|
|
const [start, end] = filter.sent_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
sent_at: {
|
|
...where.sent_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
sent_at: {
|
|
...where.sent_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.responded_atRange) {
|
|
const [start, end] = filter.responded_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
responded_at: {
|
|
...where.responded_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
responded_at: {
|
|
...where.responded_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.channel) {
|
|
where = {
|
|
...where,
|
|
channel: filter.channel,
|
|
};
|
|
}
|
|
|
|
if (filter.delivery_status) {
|
|
where = {
|
|
...where,
|
|
delivery_status: filter.delivery_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.invitations.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(
|
|
'invitations',
|
|
'invite_code',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.invitations.findAll({
|
|
attributes: [ 'id', 'invite_code' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['invite_code', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.invite_code,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|