39002-vm/backend/src/db/api/quests.js
2026-03-05 10:11:40 +00:00

529 lines
12 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 QuestsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const quests = await db.quests.create(
{
id: data.id || undefined,
name: data.name
||
null
,
code: data.code
||
null
,
quest_type: data.quest_type
||
null
,
summary: data.summary
||
null
,
reward_text: data.reward_text
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await quests.setWorld( data.world || null, {
transaction,
});
await quests.setStages(data.stages || [], {
transaction,
});
return quests;
}
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 questsData = data.map((item, index) => ({
id: item.id || undefined,
name: item.name
||
null
,
code: item.code
||
null
,
quest_type: item.quest_type
||
null
,
summary: item.summary
||
null
,
reward_text: item.reward_text
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const quests = await db.quests.bulkCreate(questsData, { transaction });
// For each item created, replace relation files
return quests;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const quests = await db.quests.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.name !== undefined) updatePayload.name = data.name;
if (data.code !== undefined) updatePayload.code = data.code;
if (data.quest_type !== undefined) updatePayload.quest_type = data.quest_type;
if (data.summary !== undefined) updatePayload.summary = data.summary;
if (data.reward_text !== undefined) updatePayload.reward_text = data.reward_text;
updatePayload.updatedById = currentUser.id;
await quests.update(updatePayload, {transaction});
if (data.world !== undefined) {
await quests.setWorld(
data.world,
{ transaction }
);
}
if (data.stages !== undefined) {
await quests.setStages(data.stages, { transaction });
}
return quests;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const quests = await db.quests.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of quests) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of quests) {
await record.destroy({transaction});
}
});
return quests;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const quests = await db.quests.findByPk(id, options);
await quests.update({
deletedBy: currentUser.id
}, {
transaction,
});
await quests.destroy({
transaction
});
return quests;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const quests = await db.quests.findOne(
{ where },
{ transaction },
);
if (!quests) {
return quests;
}
const output = quests.get({plain: true});
output.quest_stages_quest = await quests.getQuest_stages_quest({
transaction
});
output.quest_states_quest = await quests.getQuest_states_quest({
transaction
});
output.world = await quests.getWorld({
transaction
});
output.stages = await quests.getStages({
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.worlds,
as: 'world',
where: filter.world ? {
[Op.or]: [
{ id: { [Op.in]: filter.world.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.world.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.quest_stages,
as: 'stages',
required: false,
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.name) {
where = {
...where,
[Op.and]: Utils.ilike(
'quests',
'name',
filter.name,
),
};
}
if (filter.code) {
where = {
...where,
[Op.and]: Utils.ilike(
'quests',
'code',
filter.code,
),
};
}
if (filter.summary) {
where = {
...where,
[Op.and]: Utils.ilike(
'quests',
'summary',
filter.summary,
),
};
}
if (filter.reward_text) {
where = {
...where,
[Op.and]: Utils.ilike(
'quests',
'reward_text',
filter.reward_text,
),
};
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.quest_type) {
where = {
...where,
quest_type: filter.quest_type,
};
}
if (filter.stages) {
const searchTerms = filter.stages.split('|');
include = [
{
model: db.quest_stages,
as: 'stages_filter',
required: searchTerms.length > 0,
where: searchTerms.length > 0 ? {
[Op.or]: [
{ id: { [Op.in]: searchTerms.map(term => Utils.uuid(term)) } },
{
title: {
[Op.or]: searchTerms.map(term => ({ [Op.iLike]: `%${term}%` }))
}
}
]
} : undefined
},
...include,
]
}
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.quests.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(
'quests',
'name',
query,
),
],
};
}
const records = await db.quests.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,
}));
}
};