38260-vm/backend/src/db/api/equipment_templates.js
2026-02-07 03:34:22 +00:00

706 lines
18 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 Equipment_templatesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const equipment_templates = await db.equipment_templates.create(
{
id: data.id || undefined,
name: data.name
||
null
,
equipment_class: data.equipment_class
||
null
,
weight_kg: data.weight_kg
||
null
,
effective_range_km: data.effective_range_km
||
null
,
damage: data.damage
||
null
,
accuracy: data.accuracy
||
null
,
rate_of_fire: data.rate_of_fire
||
null
,
ammo_capacity: data.ammo_capacity
||
null
,
is_guided: data.is_guided
||
false
,
requires_line_of_sight: data.requires_line_of_sight
||
false
,
description: data.description
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.equipment_templates.getTableName(),
belongsToColumn: 'images',
belongsToId: equipment_templates.id,
},
data.images,
options,
);
return equipment_templates;
}
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 equipment_templatesData = data.map((item, index) => ({
id: item.id || undefined,
name: item.name
||
null
,
equipment_class: item.equipment_class
||
null
,
weight_kg: item.weight_kg
||
null
,
effective_range_km: item.effective_range_km
||
null
,
damage: item.damage
||
null
,
accuracy: item.accuracy
||
null
,
rate_of_fire: item.rate_of_fire
||
null
,
ammo_capacity: item.ammo_capacity
||
null
,
is_guided: item.is_guided
||
false
,
requires_line_of_sight: item.requires_line_of_sight
||
false
,
description: item.description
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const equipment_templates = await db.equipment_templates.bulkCreate(equipment_templatesData, { transaction });
// For each item created, replace relation files
for (let i = 0; i < equipment_templates.length; i++) {
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.equipment_templates.getTableName(),
belongsToColumn: 'images',
belongsToId: equipment_templates[i].id,
},
data[i].images,
options,
);
}
return equipment_templates;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const equipment_templates = await db.equipment_templates.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.name !== undefined) updatePayload.name = data.name;
if (data.equipment_class !== undefined) updatePayload.equipment_class = data.equipment_class;
if (data.weight_kg !== undefined) updatePayload.weight_kg = data.weight_kg;
if (data.effective_range_km !== undefined) updatePayload.effective_range_km = data.effective_range_km;
if (data.damage !== undefined) updatePayload.damage = data.damage;
if (data.accuracy !== undefined) updatePayload.accuracy = data.accuracy;
if (data.rate_of_fire !== undefined) updatePayload.rate_of_fire = data.rate_of_fire;
if (data.ammo_capacity !== undefined) updatePayload.ammo_capacity = data.ammo_capacity;
if (data.is_guided !== undefined) updatePayload.is_guided = data.is_guided;
if (data.requires_line_of_sight !== undefined) updatePayload.requires_line_of_sight = data.requires_line_of_sight;
if (data.description !== undefined) updatePayload.description = data.description;
updatePayload.updatedById = currentUser.id;
await equipment_templates.update(updatePayload, {transaction});
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.equipment_templates.getTableName(),
belongsToColumn: 'images',
belongsToId: equipment_templates.id,
},
data.images,
options,
);
return equipment_templates;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const equipment_templates = await db.equipment_templates.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of equipment_templates) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of equipment_templates) {
await record.destroy({transaction});
}
});
return equipment_templates;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const equipment_templates = await db.equipment_templates.findByPk(id, options);
await equipment_templates.update({
deletedBy: currentUser.id
}, {
transaction,
});
await equipment_templates.destroy({
transaction
});
return equipment_templates;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const equipment_templates = await db.equipment_templates.findOne(
{ where },
{ transaction },
);
if (!equipment_templates) {
return equipment_templates;
}
const output = equipment_templates.get({plain: true});
output.unit_loadouts_equipment_template = await equipment_templates.getUnit_loadouts_equipment_template({
transaction
});
output.images = await equipment_templates.getImages({
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.file,
as: 'images',
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.name) {
where = {
...where,
[Op.and]: Utils.ilike(
'equipment_templates',
'name',
filter.name,
),
};
}
if (filter.description) {
where = {
...where,
[Op.and]: Utils.ilike(
'equipment_templates',
'description',
filter.description,
),
};
}
if (filter.weight_kgRange) {
const [start, end] = filter.weight_kgRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
weight_kg: {
...where.weight_kg,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
weight_kg: {
...where.weight_kg,
[Op.lte]: end,
},
};
}
}
if (filter.effective_range_kmRange) {
const [start, end] = filter.effective_range_kmRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
effective_range_km: {
...where.effective_range_km,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
effective_range_km: {
...where.effective_range_km,
[Op.lte]: end,
},
};
}
}
if (filter.damageRange) {
const [start, end] = filter.damageRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
damage: {
...where.damage,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
damage: {
...where.damage,
[Op.lte]: end,
},
};
}
}
if (filter.accuracyRange) {
const [start, end] = filter.accuracyRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
accuracy: {
...where.accuracy,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
accuracy: {
...where.accuracy,
[Op.lte]: end,
},
};
}
}
if (filter.rate_of_fireRange) {
const [start, end] = filter.rate_of_fireRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
rate_of_fire: {
...where.rate_of_fire,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
rate_of_fire: {
...where.rate_of_fire,
[Op.lte]: end,
},
};
}
}
if (filter.ammo_capacityRange) {
const [start, end] = filter.ammo_capacityRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
ammo_capacity: {
...where.ammo_capacity,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
ammo_capacity: {
...where.ammo_capacity,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.equipment_class) {
where = {
...where,
equipment_class: filter.equipment_class,
};
}
if (filter.is_guided) {
where = {
...where,
is_guided: filter.is_guided,
};
}
if (filter.requires_line_of_sight) {
where = {
...where,
requires_line_of_sight: filter.requires_line_of_sight,
};
}
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.equipment_templates.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(
'equipment_templates',
'name',
query,
),
],
};
}
const records = await db.equipment_templates.findAll({
attributes: [ 'id', 'name' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['name', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.name,
}));
}
};