40245-vm/backend/src/db/api/tuning_recipes.js
2026-06-11 07:53:44 +00:00

625 lines
15 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 Tuning_recipesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const tuning_recipes = await db.tuning_recipes.create(
{
id: data.id || undefined,
target_class: data.target_class
||
null
,
recommended_drivetrain: data.recommended_drivetrain
||
null
,
drivetrain_checklist: data.drivetrain_checklist
||
null
,
engine_checklist: data.engine_checklist
||
null
,
chassis_tires_checklist: data.chassis_tires_checklist
||
null
,
aero_checklist: data.aero_checklist
||
null
,
alerts: data.alerts
||
null
,
fine_tuning_tip: data.fine_tuning_tip
||
null
,
recipe_status: data.recipe_status
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await tuning_recipes.setUser( data.user || null, {
transaction,
});
await tuning_recipes.setCar( data.car || null, {
transaction,
});
await tuning_recipes.setRace_type( data.race_type || null, {
transaction,
});
return tuning_recipes;
}
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 tuning_recipesData = data.map((item, index) => ({
id: item.id || undefined,
target_class: item.target_class
||
null
,
recommended_drivetrain: item.recommended_drivetrain
||
null
,
drivetrain_checklist: item.drivetrain_checklist
||
null
,
engine_checklist: item.engine_checklist
||
null
,
chassis_tires_checklist: item.chassis_tires_checklist
||
null
,
aero_checklist: item.aero_checklist
||
null
,
alerts: item.alerts
||
null
,
fine_tuning_tip: item.fine_tuning_tip
||
null
,
recipe_status: item.recipe_status
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const tuning_recipes = await db.tuning_recipes.bulkCreate(tuning_recipesData, { transaction });
// For each item created, replace relation files
return tuning_recipes;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const tuning_recipes = await db.tuning_recipes.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.target_class !== undefined) updatePayload.target_class = data.target_class;
if (data.recommended_drivetrain !== undefined) updatePayload.recommended_drivetrain = data.recommended_drivetrain;
if (data.drivetrain_checklist !== undefined) updatePayload.drivetrain_checklist = data.drivetrain_checklist;
if (data.engine_checklist !== undefined) updatePayload.engine_checklist = data.engine_checklist;
if (data.chassis_tires_checklist !== undefined) updatePayload.chassis_tires_checklist = data.chassis_tires_checklist;
if (data.aero_checklist !== undefined) updatePayload.aero_checklist = data.aero_checklist;
if (data.alerts !== undefined) updatePayload.alerts = data.alerts;
if (data.fine_tuning_tip !== undefined) updatePayload.fine_tuning_tip = data.fine_tuning_tip;
if (data.recipe_status !== undefined) updatePayload.recipe_status = data.recipe_status;
updatePayload.updatedById = currentUser.id;
await tuning_recipes.update(updatePayload, {transaction});
if (data.user !== undefined) {
await tuning_recipes.setUser(
data.user,
{ transaction }
);
}
if (data.car !== undefined) {
await tuning_recipes.setCar(
data.car,
{ transaction }
);
}
if (data.race_type !== undefined) {
await tuning_recipes.setRace_type(
data.race_type,
{ transaction }
);
}
return tuning_recipes;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const tuning_recipes = await db.tuning_recipes.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of tuning_recipes) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of tuning_recipes) {
await record.destroy({transaction});
}
});
return tuning_recipes;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const tuning_recipes = await db.tuning_recipes.findByPk(id, options);
await tuning_recipes.update({
deletedBy: currentUser.id
}, {
transaction,
});
await tuning_recipes.destroy({
transaction
});
return tuning_recipes;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const tuning_recipes = await db.tuning_recipes.findOne(
{ where },
{ transaction },
);
if (!tuning_recipes) {
return tuning_recipes;
}
const output = tuning_recipes.get({plain: true});
output.user = await tuning_recipes.getUser({
transaction
});
output.car = await tuning_recipes.getCar({
transaction
});
output.race_type = await tuning_recipes.getRace_type({
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.cars,
as: 'car',
where: filter.car ? {
[Op.or]: [
{ id: { [Op.in]: filter.car.split('|').map(term => Utils.uuid(term)) } },
{
model: {
[Op.or]: filter.car.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.race_types,
as: 'race_type',
where: filter.race_type ? {
[Op.or]: [
{ id: { [Op.in]: filter.race_type.split('|').map(term => Utils.uuid(term)) } },
{
category_name: {
[Op.or]: filter.race_type.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.drivetrain_checklist) {
where = {
...where,
[Op.and]: Utils.ilike(
'tuning_recipes',
'drivetrain_checklist',
filter.drivetrain_checklist,
),
};
}
if (filter.engine_checklist) {
where = {
...where,
[Op.and]: Utils.ilike(
'tuning_recipes',
'engine_checklist',
filter.engine_checklist,
),
};
}
if (filter.chassis_tires_checklist) {
where = {
...where,
[Op.and]: Utils.ilike(
'tuning_recipes',
'chassis_tires_checklist',
filter.chassis_tires_checklist,
),
};
}
if (filter.aero_checklist) {
where = {
...where,
[Op.and]: Utils.ilike(
'tuning_recipes',
'aero_checklist',
filter.aero_checklist,
),
};
}
if (filter.alerts) {
where = {
...where,
[Op.and]: Utils.ilike(
'tuning_recipes',
'alerts',
filter.alerts,
),
};
}
if (filter.fine_tuning_tip) {
where = {
...where,
[Op.and]: Utils.ilike(
'tuning_recipes',
'fine_tuning_tip',
filter.fine_tuning_tip,
),
};
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.target_class) {
where = {
...where,
target_class: filter.target_class,
};
}
if (filter.recommended_drivetrain) {
where = {
...where,
recommended_drivetrain: filter.recommended_drivetrain,
};
}
if (filter.recipe_status) {
where = {
...where,
recipe_status: filter.recipe_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.tuning_recipes.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(
'tuning_recipes',
'fine_tuning_tip',
query,
),
],
};
}
const records = await db.tuning_recipes.findAll({
attributes: [ 'id', 'fine_tuning_tip' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['fine_tuning_tip', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.fine_tuning_tip,
}));
}
};