142 lines
5.1 KiB
JavaScript
142 lines
5.1 KiB
JavaScript
const db = require('../models');
|
|
const Utils = require('../utils');
|
|
const Sequelize = db.Sequelize;
|
|
const Op = Sequelize.Op;
|
|
|
|
module.exports = class retailersDBApi {
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const record = await db.retailers.create(
|
|
{
|
|
id: data.id || undefined,
|
|
name: data.name || null,
|
|
importHash: data.importHash || null,
|
|
tenantId: data.tenant || currentUser.tenantId || null,
|
|
organizationsId: data.organizations || currentUser.organizationsId || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
return record;
|
|
}
|
|
|
|
static async bulkImport(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const recordsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
name: item.name || null,
|
|
importHash: item.importHash || null,
|
|
tenantId: item.tenant || currentUser.tenantId || null,
|
|
organizationsId: item.organizations || currentUser.organizationsId || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
return await db.retailers.bulkCreate(recordsData, { transaction });
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const record = await db.retailers.findByPk(id, {}, { transaction });
|
|
const updatePayload = {};
|
|
if (data.name !== undefined) updatePayload.name = data.name;
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await record.update(updatePayload, { transaction });
|
|
|
|
if (data.tenant !== undefined) await record.setTenant(data.tenant, { transaction });
|
|
if (data.organizations !== undefined) await record.setOrganizations(data.organizations, { transaction });
|
|
|
|
return record;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const records = await db.retailers.findAll({
|
|
where: { id: { [Op.in]: ids } },
|
|
transaction,
|
|
});
|
|
|
|
for (const record of records) {
|
|
await record.destroy({ transaction });
|
|
}
|
|
return records;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
const record = await db.retailers.findByPk(id, options);
|
|
await record.destroy({ transaction });
|
|
return record;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
return await db.retailers.findOne({ where }, { transaction });
|
|
}
|
|
|
|
static async findAll(filter, globalAccess, options) {
|
|
const limit = filter.limit || 0;
|
|
const currentPage = +filter.page || 0;
|
|
const offset = currentPage * limit;
|
|
let where = {};
|
|
|
|
const user = (options && options.currentUser) || null;
|
|
if (!globalAccess && user?.organizationsId) {
|
|
where.organizationsId = user.organizationsId;
|
|
}
|
|
|
|
if (filter) {
|
|
if (filter.id) where.id = Utils.uuid(filter.id);
|
|
if (filter.name) where[Op.and] = Utils.ilike('retailers', 'name', filter.name);
|
|
}
|
|
|
|
const { rows, count } = await db.retailers.findAndCountAll({
|
|
where,
|
|
include: [
|
|
{ model: db.tenants, as: 'tenant' },
|
|
{ model: db.organizations, as: 'organizations' }
|
|
],
|
|
distinct: true,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
order: filter.field && filter.sort ? [[filter.field, filter.sort]] : [['createdAt', 'desc']],
|
|
transaction: options?.transaction,
|
|
});
|
|
|
|
return { rows, count };
|
|
}
|
|
|
|
static async findAllAutocomplete(query, limit, offset, globalAccess, organizationId) {
|
|
let where = {};
|
|
if (!globalAccess && organizationId) where.organizationsId = organizationId;
|
|
|
|
if (query) {
|
|
where[Op.or] = [
|
|
{ id: Utils.uuid(query) },
|
|
Utils.ilike('retailers', 'name', query),
|
|
];
|
|
}
|
|
|
|
const records = await db.retailers.findAll({
|
|
attributes: ['id', 'name'],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
order: [['name', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({ id: record.id, label: record.name }));
|
|
}
|
|
};
|