38893-vm/backend/src/db/api/unlock_challenges.js
2026-02-28 23:06:20 +00:00

651 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 Unlock_challengesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const unlock_challenges = await db.unlock_challenges.create(
{
id: data.id || undefined,
domain: data.domain
||
null
,
challenge_type: data.challenge_type
||
null
,
status: data.status
||
null
,
pass_threshold_percent: data.pass_threshold_percent
||
null
,
requested_at: data.requested_at
||
null
,
evaluated_at: data.evaluated_at
||
null
,
expires_at: data.expires_at
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await unlock_challenges.setUser( data.user || null, {
transaction,
});
await unlock_challenges.setBlocked_website( data.blocked_website || null, {
transaction,
});
await unlock_challenges.setTest_session( data.test_session || null, {
transaction,
});
return unlock_challenges;
}
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 unlock_challengesData = data.map((item, index) => ({
id: item.id || undefined,
domain: item.domain
||
null
,
challenge_type: item.challenge_type
||
null
,
status: item.status
||
null
,
pass_threshold_percent: item.pass_threshold_percent
||
null
,
requested_at: item.requested_at
||
null
,
evaluated_at: item.evaluated_at
||
null
,
expires_at: item.expires_at
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const unlock_challenges = await db.unlock_challenges.bulkCreate(unlock_challengesData, { transaction });
// For each item created, replace relation files
return unlock_challenges;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const unlock_challenges = await db.unlock_challenges.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.domain !== undefined) updatePayload.domain = data.domain;
if (data.challenge_type !== undefined) updatePayload.challenge_type = data.challenge_type;
if (data.status !== undefined) updatePayload.status = data.status;
if (data.pass_threshold_percent !== undefined) updatePayload.pass_threshold_percent = data.pass_threshold_percent;
if (data.requested_at !== undefined) updatePayload.requested_at = data.requested_at;
if (data.evaluated_at !== undefined) updatePayload.evaluated_at = data.evaluated_at;
if (data.expires_at !== undefined) updatePayload.expires_at = data.expires_at;
updatePayload.updatedById = currentUser.id;
await unlock_challenges.update(updatePayload, {transaction});
if (data.user !== undefined) {
await unlock_challenges.setUser(
data.user,
{ transaction }
);
}
if (data.blocked_website !== undefined) {
await unlock_challenges.setBlocked_website(
data.blocked_website,
{ transaction }
);
}
if (data.test_session !== undefined) {
await unlock_challenges.setTest_session(
data.test_session,
{ transaction }
);
}
return unlock_challenges;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const unlock_challenges = await db.unlock_challenges.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of unlock_challenges) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of unlock_challenges) {
await record.destroy({transaction});
}
});
return unlock_challenges;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const unlock_challenges = await db.unlock_challenges.findByPk(id, options);
await unlock_challenges.update({
deletedBy: currentUser.id
}, {
transaction,
});
await unlock_challenges.destroy({
transaction
});
return unlock_challenges;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const unlock_challenges = await db.unlock_challenges.findOne(
{ where },
{ transaction },
);
if (!unlock_challenges) {
return unlock_challenges;
}
const output = unlock_challenges.get({plain: true});
output.temporary_unlocks_unlock_challenge = await unlock_challenges.getTemporary_unlocks_unlock_challenge({
transaction
});
output.user = await unlock_challenges.getUser({
transaction
});
output.blocked_website = await unlock_challenges.getBlocked_website({
transaction
});
output.test_session = await unlock_challenges.getTest_session({
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.users,
as: 'user',
where: filter.user ? {
[Op.or]: [
{ id: { [Op.in]: filter.user.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.blocked_websites,
as: 'blocked_website',
where: filter.blocked_website ? {
[Op.or]: [
{ id: { [Op.in]: filter.blocked_website.split('|').map(term => Utils.uuid(term)) } },
{
domain: {
[Op.or]: filter.blocked_website.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.test_sessions,
as: 'test_session',
where: filter.test_session ? {
[Op.or]: [
{ id: { [Op.in]: filter.test_session.split('|').map(term => Utils.uuid(term)) } },
{
origin: {
[Op.or]: filter.test_session.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.domain) {
where = {
...where,
[Op.and]: Utils.ilike(
'unlock_challenges',
'domain',
filter.domain,
),
};
}
if (filter.pass_threshold_percentRange) {
const [start, end] = filter.pass_threshold_percentRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
pass_threshold_percent: {
...where.pass_threshold_percent,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
pass_threshold_percent: {
...where.pass_threshold_percent,
[Op.lte]: end,
},
};
}
}
if (filter.requested_atRange) {
const [start, end] = filter.requested_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
requested_at: {
...where.requested_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
requested_at: {
...where.requested_at,
[Op.lte]: end,
},
};
}
}
if (filter.evaluated_atRange) {
const [start, end] = filter.evaluated_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
evaluated_at: {
...where.evaluated_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
evaluated_at: {
...where.evaluated_at,
[Op.lte]: end,
},
};
}
}
if (filter.expires_atRange) {
const [start, end] = filter.expires_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
expires_at: {
...where.expires_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
expires_at: {
...where.expires_at,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.challenge_type) {
where = {
...where,
challenge_type: filter.challenge_type,
};
}
if (filter.status) {
where = {
...where,
status: filter.status,
};
}
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.unlock_challenges.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(
'unlock_challenges',
'domain',
query,
),
],
};
}
const records = await db.unlock_challenges.findAll({
attributes: [ 'id', 'domain' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['domain', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.domain,
}));
}
};