38675-vm/backend/src/db/api/plan_recipes.js
2026-02-21 20:13:56 +00:00

541 lines
13 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 Plan_recipesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const plan_recipes = await db.plan_recipes.create(
{
id: data.id || undefined,
rank_reason: data.rank_reason
||
null
,
estimated_recipe_cost: data.estimated_recipe_cost
||
null
,
coverage_rate: data.coverage_rate
||
null
,
allergen_flags: data.allergen_flags
||
null
,
notes: data.notes
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await plan_recipes.setPlan( data.plan || null, {
transaction,
});
await plan_recipes.setRecipe( data.recipe || null, {
transaction,
});
return plan_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 plan_recipesData = data.map((item, index) => ({
id: item.id || undefined,
rank_reason: item.rank_reason
||
null
,
estimated_recipe_cost: item.estimated_recipe_cost
||
null
,
coverage_rate: item.coverage_rate
||
null
,
allergen_flags: item.allergen_flags
||
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 plan_recipes = await db.plan_recipes.bulkCreate(plan_recipesData, { transaction });
// For each item created, replace relation files
return plan_recipes;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const plan_recipes = await db.plan_recipes.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.rank_reason !== undefined) updatePayload.rank_reason = data.rank_reason;
if (data.estimated_recipe_cost !== undefined) updatePayload.estimated_recipe_cost = data.estimated_recipe_cost;
if (data.coverage_rate !== undefined) updatePayload.coverage_rate = data.coverage_rate;
if (data.allergen_flags !== undefined) updatePayload.allergen_flags = data.allergen_flags;
if (data.notes !== undefined) updatePayload.notes = data.notes;
updatePayload.updatedById = currentUser.id;
await plan_recipes.update(updatePayload, {transaction});
if (data.plan !== undefined) {
await plan_recipes.setPlan(
data.plan,
{ transaction }
);
}
if (data.recipe !== undefined) {
await plan_recipes.setRecipe(
data.recipe,
{ transaction }
);
}
return plan_recipes;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const plan_recipes = await db.plan_recipes.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of plan_recipes) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of plan_recipes) {
await record.destroy({transaction});
}
});
return plan_recipes;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const plan_recipes = await db.plan_recipes.findByPk(id, options);
await plan_recipes.update({
deletedBy: currentUser.id
}, {
transaction,
});
await plan_recipes.destroy({
transaction
});
return plan_recipes;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const plan_recipes = await db.plan_recipes.findOne(
{ where },
{ transaction },
);
if (!plan_recipes) {
return plan_recipes;
}
const output = plan_recipes.get({plain: true});
output.generated_instructions_plan_recipe = await plan_recipes.getGenerated_instructions_plan_recipe({
transaction
});
output.plan = await plan_recipes.getPlan({
transaction
});
output.recipe = await plan_recipes.getRecipe({
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.plans,
as: 'plan',
where: filter.plan ? {
[Op.or]: [
{ id: { [Op.in]: filter.plan.split('|').map(term => Utils.uuid(term)) } },
{
title: {
[Op.or]: filter.plan.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.recipes,
as: 'recipe',
where: filter.recipe ? {
[Op.or]: [
{ id: { [Op.in]: filter.recipe.split('|').map(term => Utils.uuid(term)) } },
{
title: {
[Op.or]: filter.recipe.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.allergen_flags) {
where = {
...where,
[Op.and]: Utils.ilike(
'plan_recipes',
'allergen_flags',
filter.allergen_flags,
),
};
}
if (filter.notes) {
where = {
...where,
[Op.and]: Utils.ilike(
'plan_recipes',
'notes',
filter.notes,
),
};
}
if (filter.estimated_recipe_costRange) {
const [start, end] = filter.estimated_recipe_costRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
estimated_recipe_cost: {
...where.estimated_recipe_cost,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
estimated_recipe_cost: {
...where.estimated_recipe_cost,
[Op.lte]: end,
},
};
}
}
if (filter.coverage_rateRange) {
const [start, end] = filter.coverage_rateRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
coverage_rate: {
...where.coverage_rate,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
coverage_rate: {
...where.coverage_rate,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.rank_reason) {
where = {
...where,
rank_reason: filter.rank_reason,
};
}
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.plan_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(
'plan_recipes',
'rank_reason',
query,
),
],
};
}
const records = await db.plan_recipes.findAll({
attributes: [ 'id', 'rank_reason' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['rank_reason', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.rank_reason,
}));
}
};