624 lines
16 KiB
JavaScript
624 lines
16 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 Scenario_factionsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const scenario_factions = await db.scenario_factions.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
side: data.side
|
|
||
|
|
null
|
|
,
|
|
|
|
callsign_prefix: data.callsign_prefix
|
|
||
|
|
null
|
|
,
|
|
|
|
starting_budget: data.starting_budget
|
|
||
|
|
null
|
|
,
|
|
|
|
starting_fuel: data.starting_fuel
|
|
||
|
|
null
|
|
,
|
|
|
|
starting_ammo: data.starting_ammo
|
|
||
|
|
null
|
|
,
|
|
|
|
starting_supplies: data.starting_supplies
|
|
||
|
|
null
|
|
,
|
|
|
|
notes: data.notes
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await scenario_factions.setScenario( data.scenario || null, {
|
|
transaction,
|
|
});
|
|
|
|
await scenario_factions.setFaction( data.faction || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return scenario_factions;
|
|
}
|
|
|
|
|
|
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 scenario_factionsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
side: item.side
|
|
||
|
|
null
|
|
,
|
|
|
|
callsign_prefix: item.callsign_prefix
|
|
||
|
|
null
|
|
,
|
|
|
|
starting_budget: item.starting_budget
|
|
||
|
|
null
|
|
,
|
|
|
|
starting_fuel: item.starting_fuel
|
|
||
|
|
null
|
|
,
|
|
|
|
starting_ammo: item.starting_ammo
|
|
||
|
|
null
|
|
,
|
|
|
|
starting_supplies: item.starting_supplies
|
|
||
|
|
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 scenario_factions = await db.scenario_factions.bulkCreate(scenario_factionsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return scenario_factions;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const scenario_factions = await db.scenario_factions.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.side !== undefined) updatePayload.side = data.side;
|
|
|
|
|
|
if (data.callsign_prefix !== undefined) updatePayload.callsign_prefix = data.callsign_prefix;
|
|
|
|
|
|
if (data.starting_budget !== undefined) updatePayload.starting_budget = data.starting_budget;
|
|
|
|
|
|
if (data.starting_fuel !== undefined) updatePayload.starting_fuel = data.starting_fuel;
|
|
|
|
|
|
if (data.starting_ammo !== undefined) updatePayload.starting_ammo = data.starting_ammo;
|
|
|
|
|
|
if (data.starting_supplies !== undefined) updatePayload.starting_supplies = data.starting_supplies;
|
|
|
|
|
|
if (data.notes !== undefined) updatePayload.notes = data.notes;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await scenario_factions.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.scenario !== undefined) {
|
|
await scenario_factions.setScenario(
|
|
|
|
data.scenario,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.faction !== undefined) {
|
|
await scenario_factions.setFaction(
|
|
|
|
data.faction,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return scenario_factions;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const scenario_factions = await db.scenario_factions.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of scenario_factions) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of scenario_factions) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return scenario_factions;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const scenario_factions = await db.scenario_factions.findByPk(id, options);
|
|
|
|
await scenario_factions.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await scenario_factions.destroy({
|
|
transaction
|
|
});
|
|
|
|
return scenario_factions;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const scenario_factions = await db.scenario_factions.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!scenario_factions) {
|
|
return scenario_factions;
|
|
}
|
|
|
|
const output = scenario_factions.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.units_scenario_faction = await scenario_factions.getUnits_scenario_faction({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
|
|
output.objectives_assigned_to_faction = await scenario_factions.getObjectives_assigned_to_faction({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.scenario = await scenario_factions.getScenario({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.faction = await scenario_factions.getFaction({
|
|
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.scenarios,
|
|
as: 'scenario',
|
|
|
|
where: filter.scenario ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.scenario.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
name: {
|
|
[Op.or]: filter.scenario.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.factions,
|
|
as: 'faction',
|
|
|
|
where: filter.faction ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.faction.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
name: {
|
|
[Op.or]: filter.faction.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.callsign_prefix) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'scenario_factions',
|
|
'callsign_prefix',
|
|
filter.callsign_prefix,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.notes) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'scenario_factions',
|
|
'notes',
|
|
filter.notes,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.starting_budgetRange) {
|
|
const [start, end] = filter.starting_budgetRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
starting_budget: {
|
|
...where.starting_budget,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
starting_budget: {
|
|
...where.starting_budget,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.starting_fuelRange) {
|
|
const [start, end] = filter.starting_fuelRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
starting_fuel: {
|
|
...where.starting_fuel,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
starting_fuel: {
|
|
...where.starting_fuel,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.starting_ammoRange) {
|
|
const [start, end] = filter.starting_ammoRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
starting_ammo: {
|
|
...where.starting_ammo,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
starting_ammo: {
|
|
...where.starting_ammo,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.starting_suppliesRange) {
|
|
const [start, end] = filter.starting_suppliesRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
starting_supplies: {
|
|
...where.starting_supplies,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
starting_supplies: {
|
|
...where.starting_supplies,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.side) {
|
|
where = {
|
|
...where,
|
|
side: filter.side,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.scenario_factions.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(
|
|
'scenario_factions',
|
|
'callsign_prefix',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.scenario_factions.findAll({
|
|
attributes: [ 'id', 'callsign_prefix' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['callsign_prefix', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.callsign_prefix,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|