555 lines
14 KiB
JavaScript
555 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 License_activationsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const license_activations = await db.license_activations.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
event_type: data.event_type
|
|
||
|
|
null
|
|
,
|
|
|
|
event_at: data.event_at
|
|
||
|
|
null
|
|
,
|
|
|
|
ip_address: data.ip_address
|
|
||
|
|
null
|
|
,
|
|
|
|
client_nonce: data.client_nonce
|
|
||
|
|
null
|
|
,
|
|
|
|
result: data.result
|
|
||
|
|
null
|
|
,
|
|
|
|
message: data.message
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await license_activations.setLicense_key( data.license_key || null, {
|
|
transaction,
|
|
});
|
|
|
|
await license_activations.setDevice_binding( data.device_binding || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return license_activations;
|
|
}
|
|
|
|
|
|
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 license_activationsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
event_type: item.event_type
|
|
||
|
|
null
|
|
,
|
|
|
|
event_at: item.event_at
|
|
||
|
|
null
|
|
,
|
|
|
|
ip_address: item.ip_address
|
|
||
|
|
null
|
|
,
|
|
|
|
client_nonce: item.client_nonce
|
|
||
|
|
null
|
|
,
|
|
|
|
result: item.result
|
|
||
|
|
null
|
|
,
|
|
|
|
message: item.message
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const license_activations = await db.license_activations.bulkCreate(license_activationsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return license_activations;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const license_activations = await db.license_activations.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.event_type !== undefined) updatePayload.event_type = data.event_type;
|
|
|
|
|
|
if (data.event_at !== undefined) updatePayload.event_at = data.event_at;
|
|
|
|
|
|
if (data.ip_address !== undefined) updatePayload.ip_address = data.ip_address;
|
|
|
|
|
|
if (data.client_nonce !== undefined) updatePayload.client_nonce = data.client_nonce;
|
|
|
|
|
|
if (data.result !== undefined) updatePayload.result = data.result;
|
|
|
|
|
|
if (data.message !== undefined) updatePayload.message = data.message;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await license_activations.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.license_key !== undefined) {
|
|
await license_activations.setLicense_key(
|
|
|
|
data.license_key,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.device_binding !== undefined) {
|
|
await license_activations.setDevice_binding(
|
|
|
|
data.device_binding,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return license_activations;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const license_activations = await db.license_activations.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of license_activations) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of license_activations) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return license_activations;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const license_activations = await db.license_activations.findByPk(id, options);
|
|
|
|
await license_activations.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await license_activations.destroy({
|
|
transaction
|
|
});
|
|
|
|
return license_activations;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const license_activations = await db.license_activations.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!license_activations) {
|
|
return license_activations;
|
|
}
|
|
|
|
const output = license_activations.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.license_key = await license_activations.getLicense_key({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.device_binding = await license_activations.getDevice_binding({
|
|
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.license_keys,
|
|
as: 'license_key',
|
|
|
|
where: filter.license_key ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.license_key.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
license_key: {
|
|
[Op.or]: filter.license_key.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.device_bindings,
|
|
as: 'device_binding',
|
|
|
|
where: filter.device_binding ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.device_binding.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
hardware_id: {
|
|
[Op.or]: filter.device_binding.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.ip_address) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'license_activations',
|
|
'ip_address',
|
|
filter.ip_address,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.client_nonce) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'license_activations',
|
|
'client_nonce',
|
|
filter.client_nonce,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.message) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'license_activations',
|
|
'message',
|
|
filter.message,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
if (filter.calendarStart && filter.calendarEnd) {
|
|
where = {
|
|
...where,
|
|
[Op.or]: [
|
|
{
|
|
event_at: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
{
|
|
event_at: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
],
|
|
};
|
|
}
|
|
|
|
|
|
|
|
if (filter.event_atRange) {
|
|
const [start, end] = filter.event_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
event_at: {
|
|
...where.event_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
event_at: {
|
|
...where.event_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.event_type) {
|
|
where = {
|
|
...where,
|
|
event_type: filter.event_type,
|
|
};
|
|
}
|
|
|
|
if (filter.result) {
|
|
where = {
|
|
...where,
|
|
result: filter.result,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.license_activations.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(
|
|
'license_activations',
|
|
'client_nonce',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.license_activations.findAll({
|
|
attributes: [ 'id', 'client_nonce' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['client_nonce', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.client_nonce,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|