323 lines
8.1 KiB
JavaScript
323 lines
8.1 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 SavingsDBApi {
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const savings = await db.savings.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
monthly_savings: data.monthly_savings || null,
|
|
savings_goal: data.savings_goal || null,
|
|
progress_percentage: data.progress_percentage || null,
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
return savings;
|
|
}
|
|
|
|
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 savingsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
monthly_savings: item.monthly_savings || null,
|
|
savings_goal: item.savings_goal || null,
|
|
progress_percentage: item.progress_percentage || null,
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const savings = await db.savings.bulkCreate(savingsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
return savings;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const savings = await db.savings.findByPk(id, {}, { transaction });
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.monthly_savings !== undefined)
|
|
updatePayload.monthly_savings = data.monthly_savings;
|
|
|
|
if (data.savings_goal !== undefined)
|
|
updatePayload.savings_goal = data.savings_goal;
|
|
|
|
if (data.progress_percentage !== undefined)
|
|
updatePayload.progress_percentage = data.progress_percentage;
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await savings.update(updatePayload, { transaction });
|
|
|
|
return savings;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const savings = await db.savings.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of savings) {
|
|
await record.update({ deletedBy: currentUser.id }, { transaction });
|
|
}
|
|
for (const record of savings) {
|
|
await record.destroy({ transaction });
|
|
}
|
|
});
|
|
|
|
return savings;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const savings = await db.savings.findByPk(id, options);
|
|
|
|
await savings.update(
|
|
{
|
|
deletedBy: currentUser.id,
|
|
},
|
|
{
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
await savings.destroy({
|
|
transaction,
|
|
});
|
|
|
|
return savings;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const savings = await db.savings.findOne({ where }, { transaction });
|
|
|
|
if (!savings) {
|
|
return savings;
|
|
}
|
|
|
|
const output = savings.get({ plain: true });
|
|
|
|
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 = [];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
if (filter.monthly_savingsRange) {
|
|
const [start, end] = filter.monthly_savingsRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
monthly_savings: {
|
|
...where.monthly_savings,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
monthly_savings: {
|
|
...where.monthly_savings,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.savings_goalRange) {
|
|
const [start, end] = filter.savings_goalRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
savings_goal: {
|
|
...where.savings_goal,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
savings_goal: {
|
|
...where.savings_goal,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.progress_percentageRange) {
|
|
const [start, end] = filter.progress_percentageRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
progress_percentage: {
|
|
...where.progress_percentage,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
progress_percentage: {
|
|
...where.progress_percentage,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true',
|
|
};
|
|
}
|
|
|
|
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.savings.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('savings', 'monthly_savings', query),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.savings.findAll({
|
|
attributes: ['id', 'monthly_savings'],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['monthly_savings', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.monthly_savings,
|
|
}));
|
|
}
|
|
};
|