152 lines
3.4 KiB
JavaScript
152 lines
3.4 KiB
JavaScript
const db = require('../db/models');
|
|
const SchoolsDBApi = require('../db/api/schools');
|
|
const parseImportFile = require('./importFileParser');
|
|
const ValidationError = require('./notifications/errors/validation');
|
|
|
|
const SCHOOL_STATUSES = new Set(['active', 'setup', 'suspended']);
|
|
|
|
function cleanString(value) {
|
|
if (value === undefined || value === null) return null;
|
|
const text = String(value).trim();
|
|
return text || null;
|
|
}
|
|
|
|
function normalizeSchoolData(data = {}) {
|
|
const normalized = {
|
|
name: cleanString(data.name),
|
|
nif: cleanString(data.nif),
|
|
phone: cleanString(data.phone),
|
|
email: cleanString(data.email),
|
|
province: cleanString(data.province),
|
|
municipality: cleanString(data.municipality),
|
|
address: cleanString(data.address),
|
|
logoUrl: cleanString(data.logoUrl),
|
|
status: cleanString(data.status) || 'active',
|
|
};
|
|
|
|
if (!normalized.name) {
|
|
throw new ValidationError('schoolsNameRequired');
|
|
}
|
|
|
|
if (normalized.email && !normalized.email.includes('@')) {
|
|
throw new ValidationError('schoolsEmailInvalid');
|
|
}
|
|
|
|
if (!SCHOOL_STATUSES.has(normalized.status)) {
|
|
throw new ValidationError('schoolsStatusInvalid');
|
|
}
|
|
|
|
return normalized;
|
|
}
|
|
|
|
module.exports = class SchoolsService {
|
|
static async create(data, currentUser) {
|
|
const transaction = await db.sequelize.transaction();
|
|
try {
|
|
await SchoolsDBApi.create(
|
|
normalizeSchoolData(data),
|
|
{
|
|
currentUser,
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
await transaction.commit();
|
|
} catch (error) {
|
|
await transaction.rollback();
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
static async bulkImport(req, res) {
|
|
const transaction = await db.sequelize.transaction();
|
|
|
|
try {
|
|
const results = await parseImportFile(req, res);
|
|
|
|
await SchoolsDBApi.bulkImport(results, {
|
|
transaction,
|
|
ignoreDuplicates: true,
|
|
validate: true,
|
|
currentUser: req.currentUser
|
|
});
|
|
|
|
await transaction.commit();
|
|
} catch (error) {
|
|
await transaction.rollback();
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
static async update(data, id, currentUser) {
|
|
const transaction = await db.sequelize.transaction();
|
|
try {
|
|
let schools = await SchoolsDBApi.findBy(
|
|
{id},
|
|
{ transaction, currentUser },
|
|
);
|
|
|
|
if (!schools) {
|
|
throw new ValidationError(
|
|
'schoolsNotFound',
|
|
);
|
|
}
|
|
|
|
const updatedSchools = await SchoolsDBApi.update(
|
|
id,
|
|
normalizeSchoolData(data),
|
|
{
|
|
currentUser,
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
await transaction.commit();
|
|
return updatedSchools;
|
|
|
|
} catch (error) {
|
|
await transaction.rollback();
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
static async deleteByIds(ids, currentUser) {
|
|
const transaction = await db.sequelize.transaction();
|
|
|
|
try {
|
|
await SchoolsDBApi.deleteByIds(ids, {
|
|
currentUser,
|
|
transaction,
|
|
});
|
|
|
|
await transaction.commit();
|
|
} catch (error) {
|
|
await transaction.rollback();
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
static async remove(id, currentUser) {
|
|
const transaction = await db.sequelize.transaction();
|
|
|
|
try {
|
|
await SchoolsDBApi.remove(
|
|
id,
|
|
{
|
|
currentUser,
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
await transaction.commit();
|
|
} catch (error) {
|
|
await transaction.rollback();
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|