657 lines
17 KiB
JavaScript
657 lines
17 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 Eco_action_logsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const eco_action_logs = await db.eco_action_logs.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
log_date: data.log_date
|
|
||
|
|
null
|
|
,
|
|
|
|
transport_type: data.transport_type
|
|
||
|
|
null
|
|
,
|
|
|
|
energy_use: data.energy_use
|
|
||
|
|
null
|
|
,
|
|
|
|
waste_disposal_method: data.waste_disposal_method
|
|
||
|
|
null
|
|
,
|
|
|
|
transport_points: data.transport_points
|
|
||
|
|
null
|
|
,
|
|
|
|
energy_points: data.energy_points
|
|
||
|
|
null
|
|
,
|
|
|
|
waste_points: data.waste_points
|
|
||
|
|
null
|
|
,
|
|
|
|
total_points: data.total_points
|
|
||
|
|
null
|
|
,
|
|
|
|
eco_score: data.eco_score
|
|
||
|
|
null
|
|
,
|
|
|
|
notes: data.notes
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await eco_action_logs.setUser( data.user || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return eco_action_logs;
|
|
}
|
|
|
|
|
|
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 eco_action_logsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
log_date: item.log_date
|
|
||
|
|
null
|
|
,
|
|
|
|
transport_type: item.transport_type
|
|
||
|
|
null
|
|
,
|
|
|
|
energy_use: item.energy_use
|
|
||
|
|
null
|
|
,
|
|
|
|
waste_disposal_method: item.waste_disposal_method
|
|
||
|
|
null
|
|
,
|
|
|
|
transport_points: item.transport_points
|
|
||
|
|
null
|
|
,
|
|
|
|
energy_points: item.energy_points
|
|
||
|
|
null
|
|
,
|
|
|
|
waste_points: item.waste_points
|
|
||
|
|
null
|
|
,
|
|
|
|
total_points: item.total_points
|
|
||
|
|
null
|
|
,
|
|
|
|
eco_score: item.eco_score
|
|
||
|
|
null
|
|
,
|
|
|
|
notes: item.notes
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const eco_action_logs = await db.eco_action_logs.bulkCreate(eco_action_logsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return eco_action_logs;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const eco_action_logs = await db.eco_action_logs.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.log_date !== undefined) updatePayload.log_date = data.log_date;
|
|
|
|
|
|
if (data.transport_type !== undefined) updatePayload.transport_type = data.transport_type;
|
|
|
|
|
|
if (data.energy_use !== undefined) updatePayload.energy_use = data.energy_use;
|
|
|
|
|
|
if (data.waste_disposal_method !== undefined) updatePayload.waste_disposal_method = data.waste_disposal_method;
|
|
|
|
|
|
if (data.transport_points !== undefined) updatePayload.transport_points = data.transport_points;
|
|
|
|
|
|
if (data.energy_points !== undefined) updatePayload.energy_points = data.energy_points;
|
|
|
|
|
|
if (data.waste_points !== undefined) updatePayload.waste_points = data.waste_points;
|
|
|
|
|
|
if (data.total_points !== undefined) updatePayload.total_points = data.total_points;
|
|
|
|
|
|
if (data.eco_score !== undefined) updatePayload.eco_score = data.eco_score;
|
|
|
|
|
|
if (data.notes !== undefined) updatePayload.notes = data.notes;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await eco_action_logs.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.user !== undefined) {
|
|
await eco_action_logs.setUser(
|
|
|
|
data.user,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return eco_action_logs;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const eco_action_logs = await db.eco_action_logs.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of eco_action_logs) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of eco_action_logs) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return eco_action_logs;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const eco_action_logs = await db.eco_action_logs.findByPk(id, options);
|
|
|
|
await eco_action_logs.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await eco_action_logs.destroy({
|
|
transaction
|
|
});
|
|
|
|
return eco_action_logs;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const eco_action_logs = await db.eco_action_logs.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!eco_action_logs) {
|
|
return eco_action_logs;
|
|
}
|
|
|
|
const output = eco_action_logs.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.user = await eco_action_logs.getUser({
|
|
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}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.notes) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'eco_action_logs',
|
|
'notes',
|
|
filter.notes,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.log_dateRange) {
|
|
const [start, end] = filter.log_dateRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
log_date: {
|
|
...where.log_date,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
log_date: {
|
|
...where.log_date,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.transport_pointsRange) {
|
|
const [start, end] = filter.transport_pointsRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
transport_points: {
|
|
...where.transport_points,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
transport_points: {
|
|
...where.transport_points,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.energy_pointsRange) {
|
|
const [start, end] = filter.energy_pointsRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
energy_points: {
|
|
...where.energy_points,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
energy_points: {
|
|
...where.energy_points,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.waste_pointsRange) {
|
|
const [start, end] = filter.waste_pointsRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
waste_points: {
|
|
...where.waste_points,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
waste_points: {
|
|
...where.waste_points,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.total_pointsRange) {
|
|
const [start, end] = filter.total_pointsRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
total_points: {
|
|
...where.total_points,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
total_points: {
|
|
...where.total_points,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.eco_scoreRange) {
|
|
const [start, end] = filter.eco_scoreRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
eco_score: {
|
|
...where.eco_score,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
eco_score: {
|
|
...where.eco_score,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.transport_type) {
|
|
where = {
|
|
...where,
|
|
transport_type: filter.transport_type,
|
|
};
|
|
}
|
|
|
|
if (filter.energy_use) {
|
|
where = {
|
|
...where,
|
|
energy_use: filter.energy_use,
|
|
};
|
|
}
|
|
|
|
if (filter.waste_disposal_method) {
|
|
where = {
|
|
...where,
|
|
waste_disposal_method: filter.waste_disposal_method,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.eco_action_logs.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(
|
|
'eco_action_logs',
|
|
'notes',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.eco_action_logs.findAll({
|
|
attributes: [ 'id', 'notes' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['notes', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.notes,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|