38452-vm/backend/src/db/api/try_on_sessions.js
2026-02-15 15:49:01 +00:00

718 lines
18 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 Try_on_sessionsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const try_on_sessions = await db.try_on_sessions.create(
{
id: data.id || undefined,
input_product_url: data.input_product_url
||
null
,
status: data.status
||
null
,
render_mode: data.render_mode
||
null
,
estimated_cost: data.estimated_cost
||
null
,
error_message: data.error_message
||
null
,
requested_at: data.requested_at
||
null
,
completed_at: data.completed_at
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await try_on_sessions.setUser( data.user || null, {
transaction,
});
await try_on_sessions.setUser_photo( data.user_photo || null, {
transaction,
});
await try_on_sessions.setSize_profile( data.size_profile || null, {
transaction,
});
await try_on_sessions.setProduct( data.product || null, {
transaction,
});
await try_on_sessions.setRenders(data.renders || [], {
transaction,
});
return try_on_sessions;
}
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 try_on_sessionsData = data.map((item, index) => ({
id: item.id || undefined,
input_product_url: item.input_product_url
||
null
,
status: item.status
||
null
,
render_mode: item.render_mode
||
null
,
estimated_cost: item.estimated_cost
||
null
,
error_message: item.error_message
||
null
,
requested_at: item.requested_at
||
null
,
completed_at: item.completed_at
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const try_on_sessions = await db.try_on_sessions.bulkCreate(try_on_sessionsData, { transaction });
// For each item created, replace relation files
return try_on_sessions;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const try_on_sessions = await db.try_on_sessions.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.input_product_url !== undefined) updatePayload.input_product_url = data.input_product_url;
if (data.status !== undefined) updatePayload.status = data.status;
if (data.render_mode !== undefined) updatePayload.render_mode = data.render_mode;
if (data.estimated_cost !== undefined) updatePayload.estimated_cost = data.estimated_cost;
if (data.error_message !== undefined) updatePayload.error_message = data.error_message;
if (data.requested_at !== undefined) updatePayload.requested_at = data.requested_at;
if (data.completed_at !== undefined) updatePayload.completed_at = data.completed_at;
updatePayload.updatedById = currentUser.id;
await try_on_sessions.update(updatePayload, {transaction});
if (data.user !== undefined) {
await try_on_sessions.setUser(
data.user,
{ transaction }
);
}
if (data.user_photo !== undefined) {
await try_on_sessions.setUser_photo(
data.user_photo,
{ transaction }
);
}
if (data.size_profile !== undefined) {
await try_on_sessions.setSize_profile(
data.size_profile,
{ transaction }
);
}
if (data.product !== undefined) {
await try_on_sessions.setProduct(
data.product,
{ transaction }
);
}
if (data.renders !== undefined) {
await try_on_sessions.setRenders(data.renders, { transaction });
}
return try_on_sessions;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const try_on_sessions = await db.try_on_sessions.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of try_on_sessions) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of try_on_sessions) {
await record.destroy({transaction});
}
});
return try_on_sessions;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const try_on_sessions = await db.try_on_sessions.findByPk(id, options);
await try_on_sessions.update({
deletedBy: currentUser.id
}, {
transaction,
});
await try_on_sessions.destroy({
transaction
});
return try_on_sessions;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const try_on_sessions = await db.try_on_sessions.findOne(
{ where },
{ transaction },
);
if (!try_on_sessions) {
return try_on_sessions;
}
const output = try_on_sessions.get({plain: true});
output.try_on_renders_session = await try_on_sessions.getTry_on_renders_session({
transaction
});
output.looks_try_on_session = await try_on_sessions.getLooks_try_on_session({
transaction
});
output.user = await try_on_sessions.getUser({
transaction
});
output.user_photo = await try_on_sessions.getUser_photo({
transaction
});
output.size_profile = await try_on_sessions.getSize_profile({
transaction
});
output.product = await try_on_sessions.getProduct({
transaction
});
output.renders = await try_on_sessions.getRenders({
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.user_photos,
as: 'user_photo',
where: filter.user_photo ? {
[Op.or]: [
{ id: { [Op.in]: filter.user_photo.split('|').map(term => Utils.uuid(term)) } },
{
capture_notes: {
[Op.or]: filter.user_photo.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.size_profiles,
as: 'size_profile',
where: filter.size_profile ? {
[Op.or]: [
{ id: { [Op.in]: filter.size_profile.split('|').map(term => Utils.uuid(term)) } },
{
profile_name: {
[Op.or]: filter.size_profile.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.products,
as: 'product',
where: filter.product ? {
[Op.or]: [
{ id: { [Op.in]: filter.product.split('|').map(term => Utils.uuid(term)) } },
{
product_name: {
[Op.or]: filter.product.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.try_on_renders,
as: 'renders',
required: false,
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.input_product_url) {
where = {
...where,
[Op.and]: Utils.ilike(
'try_on_sessions',
'input_product_url',
filter.input_product_url,
),
};
}
if (filter.error_message) {
where = {
...where,
[Op.and]: Utils.ilike(
'try_on_sessions',
'error_message',
filter.error_message,
),
};
}
if (filter.estimated_costRange) {
const [start, end] = filter.estimated_costRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
estimated_cost: {
...where.estimated_cost,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
estimated_cost: {
...where.estimated_cost,
[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.completed_atRange) {
const [start, end] = filter.completed_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
completed_at: {
...where.completed_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
completed_at: {
...where.completed_at,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.status) {
where = {
...where,
status: filter.status,
};
}
if (filter.render_mode) {
where = {
...where,
render_mode: filter.render_mode,
};
}
if (filter.renders) {
const searchTerms = filter.renders.split('|');
include = [
{
model: db.try_on_renders,
as: 'renders_filter',
required: searchTerms.length > 0,
where: searchTerms.length > 0 ? {
[Op.or]: [
{ id: { [Op.in]: searchTerms.map(term => Utils.uuid(term)) } },
{
provider_job_ref: {
[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.try_on_sessions.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(
'try_on_sessions',
'input_product_url',
query,
),
],
};
}
const records = await db.try_on_sessions.findAll({
attributes: [ 'id', 'input_product_url' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['input_product_url', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.input_product_url,
}));
}
};