592 lines
14 KiB
JavaScript
592 lines
14 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 Customer_addressesDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const customer_addresses = await db.customer_addresses.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
street: data.street
|
|
||
|
|
null
|
|
,
|
|
|
|
number: data.number
|
|
||
|
|
null
|
|
,
|
|
|
|
complement: data.complement
|
|
||
|
|
null
|
|
,
|
|
|
|
neighborhood: data.neighborhood
|
|
||
|
|
null
|
|
,
|
|
|
|
city: data.city
|
|
||
|
|
null
|
|
,
|
|
|
|
state: data.state
|
|
||
|
|
null
|
|
,
|
|
|
|
country: data.country
|
|
||
|
|
null
|
|
,
|
|
|
|
zip_code: data.zip_code
|
|
||
|
|
null
|
|
,
|
|
|
|
is_default: data.is_default
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await customer_addresses.setCustomer( data.customer || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return customer_addresses;
|
|
}
|
|
|
|
|
|
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 customer_addressesData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
street: item.street
|
|
||
|
|
null
|
|
,
|
|
|
|
number: item.number
|
|
||
|
|
null
|
|
,
|
|
|
|
complement: item.complement
|
|
||
|
|
null
|
|
,
|
|
|
|
neighborhood: item.neighborhood
|
|
||
|
|
null
|
|
,
|
|
|
|
city: item.city
|
|
||
|
|
null
|
|
,
|
|
|
|
state: item.state
|
|
||
|
|
null
|
|
,
|
|
|
|
country: item.country
|
|
||
|
|
null
|
|
,
|
|
|
|
zip_code: item.zip_code
|
|
||
|
|
null
|
|
,
|
|
|
|
is_default: item.is_default
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const customer_addresses = await db.customer_addresses.bulkCreate(customer_addressesData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return customer_addresses;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const customer_addresses = await db.customer_addresses.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.street !== undefined) updatePayload.street = data.street;
|
|
|
|
|
|
if (data.number !== undefined) updatePayload.number = data.number;
|
|
|
|
|
|
if (data.complement !== undefined) updatePayload.complement = data.complement;
|
|
|
|
|
|
if (data.neighborhood !== undefined) updatePayload.neighborhood = data.neighborhood;
|
|
|
|
|
|
if (data.city !== undefined) updatePayload.city = data.city;
|
|
|
|
|
|
if (data.state !== undefined) updatePayload.state = data.state;
|
|
|
|
|
|
if (data.country !== undefined) updatePayload.country = data.country;
|
|
|
|
|
|
if (data.zip_code !== undefined) updatePayload.zip_code = data.zip_code;
|
|
|
|
|
|
if (data.is_default !== undefined) updatePayload.is_default = data.is_default;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await customer_addresses.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.customer !== undefined) {
|
|
await customer_addresses.setCustomer(
|
|
|
|
data.customer,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return customer_addresses;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const customer_addresses = await db.customer_addresses.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of customer_addresses) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of customer_addresses) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return customer_addresses;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const customer_addresses = await db.customer_addresses.findByPk(id, options);
|
|
|
|
await customer_addresses.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await customer_addresses.destroy({
|
|
transaction
|
|
});
|
|
|
|
return customer_addresses;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const customer_addresses = await db.customer_addresses.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!customer_addresses) {
|
|
return customer_addresses;
|
|
}
|
|
|
|
const output = customer_addresses.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.customer = await customer_addresses.getCustomer({
|
|
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.customers,
|
|
as: 'customer',
|
|
|
|
where: filter.customer ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.customer.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
email: {
|
|
[Op.or]: filter.customer.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.street) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'customer_addresses',
|
|
'street',
|
|
filter.street,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.number) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'customer_addresses',
|
|
'number',
|
|
filter.number,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.complement) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'customer_addresses',
|
|
'complement',
|
|
filter.complement,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.neighborhood) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'customer_addresses',
|
|
'neighborhood',
|
|
filter.neighborhood,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.city) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'customer_addresses',
|
|
'city',
|
|
filter.city,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.state) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'customer_addresses',
|
|
'state',
|
|
filter.state,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.country) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'customer_addresses',
|
|
'country',
|
|
filter.country,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.zip_code) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'customer_addresses',
|
|
'zip_code',
|
|
filter.zip_code,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.is_default) {
|
|
where = {
|
|
...where,
|
|
is_default: filter.is_default,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.customer_addresses.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(
|
|
'customer_addresses',
|
|
'street',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.customer_addresses.findAll({
|
|
attributes: [ 'id', 'street' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['street', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.street,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|