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 Workout_exercisesDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const workout_exercises = await db.workout_exercises.create( { id: data.id || undefined, name: data.name || null , equipment_level: data.equipment_level || null , instructions: data.instructions || null , sets: data.sets || null , reps: data.reps || null , duration_seconds: data.duration_seconds || null , rest_seconds: data.rest_seconds || null , order_index: data.order_index || null , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await workout_exercises.setWorkout_plan( data.workout_plan || null, { transaction, }); return workout_exercises; } 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 workout_exercisesData = data.map((item, index) => ({ id: item.id || undefined, name: item.name || null , equipment_level: item.equipment_level || null , instructions: item.instructions || null , sets: item.sets || null , reps: item.reps || null , duration_seconds: item.duration_seconds || null , rest_seconds: item.rest_seconds || null , order_index: item.order_index || null , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const workout_exercises = await db.workout_exercises.bulkCreate(workout_exercisesData, { transaction }); // For each item created, replace relation files return workout_exercises; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const workout_exercises = await db.workout_exercises.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.name !== undefined) updatePayload.name = data.name; if (data.equipment_level !== undefined) updatePayload.equipment_level = data.equipment_level; if (data.instructions !== undefined) updatePayload.instructions = data.instructions; if (data.sets !== undefined) updatePayload.sets = data.sets; if (data.reps !== undefined) updatePayload.reps = data.reps; if (data.duration_seconds !== undefined) updatePayload.duration_seconds = data.duration_seconds; if (data.rest_seconds !== undefined) updatePayload.rest_seconds = data.rest_seconds; if (data.order_index !== undefined) updatePayload.order_index = data.order_index; updatePayload.updatedById = currentUser.id; await workout_exercises.update(updatePayload, {transaction}); if (data.workout_plan !== undefined) { await workout_exercises.setWorkout_plan( data.workout_plan, { transaction } ); } return workout_exercises; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const workout_exercises = await db.workout_exercises.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of workout_exercises) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of workout_exercises) { await record.destroy({transaction}); } }); return workout_exercises; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const workout_exercises = await db.workout_exercises.findByPk(id, options); await workout_exercises.update({ deletedBy: currentUser.id }, { transaction, }); await workout_exercises.destroy({ transaction }); return workout_exercises; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const workout_exercises = await db.workout_exercises.findOne( { where }, { transaction }, ); if (!workout_exercises) { return workout_exercises; } const output = workout_exercises.get({plain: true}); output.workout_plan = await workout_exercises.getWorkout_plan({ 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.workout_plans, as: 'workout_plan', where: filter.workout_plan ? { [Op.or]: [ { id: { [Op.in]: filter.workout_plan.split('|').map(term => Utils.uuid(term)) } }, { title: { [Op.or]: filter.workout_plan.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.name) { where = { ...where, [Op.and]: Utils.ilike( 'workout_exercises', 'name', filter.name, ), }; } if (filter.instructions) { where = { ...where, [Op.and]: Utils.ilike( 'workout_exercises', 'instructions', filter.instructions, ), }; } if (filter.setsRange) { const [start, end] = filter.setsRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, sets: { ...where.sets, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, sets: { ...where.sets, [Op.lte]: end, }, }; } } if (filter.repsRange) { const [start, end] = filter.repsRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, reps: { ...where.reps, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, reps: { ...where.reps, [Op.lte]: end, }, }; } } if (filter.duration_secondsRange) { const [start, end] = filter.duration_secondsRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, duration_seconds: { ...where.duration_seconds, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, duration_seconds: { ...where.duration_seconds, [Op.lte]: end, }, }; } } if (filter.rest_secondsRange) { const [start, end] = filter.rest_secondsRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, rest_seconds: { ...where.rest_seconds, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, rest_seconds: { ...where.rest_seconds, [Op.lte]: end, }, }; } } if (filter.order_indexRange) { const [start, end] = filter.order_indexRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, order_index: { ...where.order_index, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, order_index: { ...where.order_index, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true' }; } if (filter.equipment_level) { where = { ...where, equipment_level: filter.equipment_level, }; } 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.workout_exercises.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( 'workout_exercises', 'name', query, ), ], }; } const records = await db.workout_exercises.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, })); } };