const db = require('../models'); const Utils = require('../utils'); const Sequelize = db.Sequelize; const Op = Sequelize.Op; module.exports = class VillagesDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; return db.villages.create( { id: data.id || undefined, nom: data.nom || null, commune: data.commune || null, arrondissement: data.arrondissement || null, departement: data.departement || null, region: data.region || null, duree_periode_jours: data.duree_periode_jours || null, notes: data.notes || null, importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); } static async bulkImport(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const rows = data.map((item, index) => ({ id: item.id || undefined, nom: item.nom || null, commune: item.commune || null, arrondissement: item.arrondissement || null, departement: item.departement || null, region: item.region || null, duree_periode_jours: item.duree_periode_jours || null, notes: item.notes || null, importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); return db.villages.bulkCreate(rows, { transaction }); } static async update(id, data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const record = await db.villages.findByPk(id, { transaction }); const payload = { updatedById: currentUser.id, }; if (data.nom !== undefined) payload.nom = data.nom; if (data.commune !== undefined) payload.commune = data.commune; if (data.arrondissement !== undefined) payload.arrondissement = data.arrondissement; if (data.departement !== undefined) payload.departement = data.departement; if (data.region !== undefined) payload.region = data.region; if (data.duree_periode_jours !== undefined) payload.duree_periode_jours = data.duree_periode_jours; if (data.notes !== undefined) payload.notes = data.notes; await record.update(payload, { transaction }); return record; } static async deleteByIds(ids, options) { const transaction = (options && options.transaction) || undefined; const records = await db.villages.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); for (const record of records) { await record.destroy({ transaction }); } return records; } static async remove(id, options) { const transaction = (options && options.transaction) || undefined; const record = await db.villages.findByPk(id, { transaction }); await record.destroy({ transaction }); return record; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const record = await db.villages.findOne({ where, include: [ { model: db.quartiers, as: 'quartiers', attributes: ['id', 'nom'], }, ], transaction, }); if (!record) { return record; } return record.get({ plain: true }); } static async findAll(filter, options) { const limit = filter.limit || 0; const currentPage = +filter.page || 0; const offset = currentPage * limit; let where = {}; if (filter.id) { where.id = Utils.uuid(filter.id); } if (filter.nom) { where = { ...where, [Op.and]: Utils.ilike('villages', 'nom', filter.nom), }; } for (const field of ['commune', 'arrondissement', 'departement', 'region', 'notes']) { if (filter[field]) { where = { ...where, [Op.and]: Utils.ilike('villages', field, filter[field]), }; } } if (filter.duree_periode_joursRange) { const values = Array.isArray(filter.duree_periode_joursRange) ? filter.duree_periode_joursRange : [filter.duree_periode_joursRange]; const [start, end] = values; if (start !== undefined && start !== '') { where.duree_periode_jours = { ...where.duree_periode_jours, [Op.gte]: start, }; } if (end !== undefined && end !== '') { where.duree_periode_jours = { ...where.duree_periode_jours, [Op.lte]: end, }; } } const queryOptions = { where, include: [ { model: db.quartiers, as: 'quartiers', attributes: ['id', 'nom'], required: false, }, ], distinct: true, order: filter.field && filter.sort ? [[filter.field, filter.sort]] : [['nom', 'asc']], transaction: options?.transaction, logging: console.log, }; if (!options?.countOnly) { queryOptions.limit = limit ? Number(limit) : undefined; queryOptions.offset = offset ? Number(offset) : undefined; } const { rows, count } = await db.villages.findAndCountAll(queryOptions); return { rows: options?.countOnly ? [] : rows, count, }; } static async findAllAutocomplete(query, limit, offset) { let where = {}; if (query) { where = { [Op.or]: [ { id: Utils.uuid(query) }, Utils.ilike('villages', 'nom', query), ], }; } const records = await db.villages.findAll({ attributes: ['id', 'nom'], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, order: [['nom', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.nom, })); } };