532 lines
15 KiB
JavaScript
532 lines
15 KiB
JavaScript
import { Meteor } from 'meteor/meteor';
|
|
import { PoblesCollection } from '/imports/api/pobles.js';
|
|
import { check } from "meteor/check";
|
|
|
|
import { Roles } from 'meteor/roles';
|
|
import { ROLS_GLOBALS, ROLS_DE_POBLE } from '../imports/roles';
|
|
import { NecessitatsCollection } from '../imports/api/necessitats';
|
|
import { TipusCollection } from '../imports/api/tipus';
|
|
import { CodisCollection } from '../imports/api/codis';
|
|
// import { Codis } from '../imports/ui/Codis';
|
|
|
|
async function insertPoble({ nomPoble, cp, comarca }) {
|
|
await PoblesCollection.insertAsync({ nomPoble, cp, comarca, createdAt: new Date() });
|
|
}
|
|
|
|
Meteor.startup(async () => {
|
|
// If the Pobles collection is empty, add some data.
|
|
if (await PoblesCollection.find().countAsync() === 0) {
|
|
await insertPoble({
|
|
nomPoble: "Alaquàs",
|
|
cp: "",
|
|
comarca: ""
|
|
});
|
|
|
|
await insertPoble({
|
|
nomPoble: "Albal",
|
|
cp: "",
|
|
comarca: ""
|
|
});
|
|
await insertPoble({
|
|
nomPoble: "Aldaia",
|
|
cp: "",
|
|
comarca: ""
|
|
});
|
|
await insertPoble({
|
|
nomPoble: "Alfafar",
|
|
cp: "",
|
|
comarca: ""
|
|
});
|
|
await insertPoble({
|
|
nomPoble: "Algemesí",
|
|
cp: "",
|
|
comarca: ""
|
|
});
|
|
await insertPoble({
|
|
nomPoble: "Alginet",
|
|
cp: "",
|
|
comarca: ""
|
|
});
|
|
await insertPoble({
|
|
nomPoble: "Alcàsser",
|
|
cp: "",
|
|
comarca: ""
|
|
});
|
|
await insertPoble({
|
|
nomPoble: "Benetússer",
|
|
cp: "",
|
|
comarca: ""
|
|
});
|
|
await insertPoble({
|
|
nomPoble: "Beniparrell",
|
|
cp: "",
|
|
comarca: ""
|
|
});
|
|
await insertPoble({
|
|
nomPoble: "Carlet",
|
|
cp: "",
|
|
comarca: ""
|
|
});
|
|
await insertPoble({
|
|
nomPoble: "Catarroja",
|
|
cp: "",
|
|
comarca: ""
|
|
});
|
|
await insertPoble({
|
|
nomPoble: "Dosaigües",
|
|
cp: "",
|
|
comarca: ""
|
|
});
|
|
|
|
await insertPoble({
|
|
nomPoble: "Godelleta",
|
|
cp: "",
|
|
comarca: ""
|
|
});
|
|
await insertPoble({
|
|
nomPoble: "Guadassuar",
|
|
cp: "",
|
|
comarca: ""
|
|
});
|
|
await insertPoble({
|
|
nomPoble: "Iàtova",
|
|
cp: "",
|
|
comarca: ""
|
|
});
|
|
await insertPoble({
|
|
nomPoble: "L'Alcúdia",
|
|
cp: "",
|
|
comarca: ""
|
|
});
|
|
await insertPoble({
|
|
nomPoble: "Llocnou de la Corona",
|
|
cp: "",
|
|
comarca: ""
|
|
});
|
|
await insertPoble({
|
|
nomPoble: "Massanassa",
|
|
cp: "",
|
|
comarca: ""
|
|
});
|
|
|
|
|
|
|
|
await insertPoble({
|
|
nomPoble: "Paiporta",
|
|
cp: "",
|
|
comarca: ""
|
|
});
|
|
await insertPoble({
|
|
nomPoble: "Parke Alkosa",
|
|
cp: "",
|
|
comarca: ""
|
|
});
|
|
await insertPoble({
|
|
nomPoble: "Picanya",
|
|
cp: "",
|
|
comarca: ""
|
|
});
|
|
await insertPoble({
|
|
nomPoble: "Sedaví",
|
|
cp: "",
|
|
comarca: ""
|
|
});
|
|
await insertPoble({
|
|
nomPoble: "Setaigües",
|
|
cp: "",
|
|
comarca: ""
|
|
});
|
|
await insertPoble({
|
|
nomPoble: "Utiel",
|
|
cp: "",
|
|
comarca: ""
|
|
});
|
|
|
|
await insertPoble({
|
|
nomPoble: "València-Castellar Oliveral",
|
|
cp: "",
|
|
comarca: ""
|
|
});
|
|
await insertPoble({
|
|
nomPoble: "València-Forn d'Alcedo",
|
|
cp: "",
|
|
comarca: ""
|
|
});
|
|
await insertPoble({
|
|
nomPoble: "València-La Torre",
|
|
cp: "",
|
|
comarca: ""
|
|
});
|
|
await insertPoble({
|
|
nomPoble: "Xest",
|
|
cp: "",
|
|
comarca: ""
|
|
});
|
|
await insertPoble({
|
|
nomPoble: "Xiva",
|
|
cp: "",
|
|
comarca: ""
|
|
});
|
|
await insertPoble({
|
|
nomPoble: "Llombai",
|
|
cp: "",
|
|
comarca: ""
|
|
});
|
|
await insertPoble({
|
|
nomPoble: "Tremolar",
|
|
cp: "",
|
|
comarca: ""
|
|
});
|
|
|
|
}
|
|
|
|
// We publish the entire Pobles collection to all clients.
|
|
// In order to be fetched in real-time to the clients
|
|
Meteor.publish("pobles", function () {
|
|
return PoblesCollection.find();
|
|
});
|
|
|
|
Meteor.publish("necessitats", function (ambits) {
|
|
|
|
if (Array.isArray(ambits)) {
|
|
return;
|
|
}
|
|
|
|
if (ambits) {
|
|
return NecessitatsCollection.find({"poble.ambitAssociat": ambits});
|
|
}
|
|
|
|
return NecessitatsCollection.find();
|
|
});
|
|
|
|
Meteor.publish("tipus", function () {
|
|
return TipusCollection.find();
|
|
});
|
|
|
|
Meteor.publish('codis', async function (uid) {
|
|
|
|
const esAdmin = await Roles.userIsInRoleAsync(this.userId, "admin", null);
|
|
const userRoles = await Roles.getRolesForUserAsync(this.userId);
|
|
|
|
// console.log("userRoles: ", userRoles);
|
|
// console.log("esAdmin: ", esAdmin);
|
|
|
|
if (esAdmin) {
|
|
return CodisCollection.find({});
|
|
}
|
|
|
|
return false;
|
|
|
|
// if (uid) {
|
|
// return Meteor.users.find({_id: uid}, {fields: {username: 1, avatarId: 1, avatarLink: 1}});
|
|
// }
|
|
|
|
return Meteor.users.find({},{fields: {username: 1, avatarId: 1, avatarLink: 1}});
|
|
});
|
|
|
|
Meteor.publish('usuaris', async function (uid) {
|
|
|
|
const esAdmin = await Roles.userIsInRoleAsync(this.userId, "admin", null);
|
|
const userRoles = await Roles.getRolesForUserAsync(this.userId);
|
|
|
|
console.log("userRoles: ", userRoles);
|
|
console.log("esAdmin: ", esAdmin);
|
|
|
|
if (esAdmin) {
|
|
return Meteor.users.find({});
|
|
}
|
|
|
|
if (uid) {
|
|
return Meteor.users.find({_id: uid}, {fields: {username: 1, avatarId: 1, avatarLink: 1}});
|
|
}
|
|
|
|
return Meteor.users.find({},{fields: {username: 1, avatarId: 1, avatarLink: 1}});
|
|
});
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
try {
|
|
await Roles.createRoleAsync(ROLS_GLOBALS.ADMINISTRADOR, { unlessExists: true });
|
|
await Roles.createRoleAsync(ROLS_GLOBALS.USUARI, { unlessExists: true });
|
|
|
|
await Roles.createRoleAsync(ROLS_DE_POBLE.MONITOR, { unlessExists: true });
|
|
await Roles.createRoleAsync(ROLS_DE_POBLE.ENCARREGAT, { unlessExists: true });
|
|
await Roles.createRoleAsync(ROLS_DE_POBLE.VOLUNTARI, { unlessExists: true });
|
|
|
|
// await Roles.addRolesToParentAsync([ROLS_DE_POBLE.VOLUNTARI, ROLS_DE_POBLE.ENCARREGAT], ROLS_DE_POBLE.MONITOR);
|
|
// await Roles.addRolesToParentAsync([ROLS_DE_POBLE.VOLUNTARI, ROLS_DE_POBLE.ENCARREGAT, ROLS_DE_POBLE.MONITOR, ROLS_GLOBALS.USUARI], ROLS_GLOBALS.ADMINISTRADOR);
|
|
|
|
} catch (err) {
|
|
console.error(err.message);
|
|
}
|
|
|
|
// Publish user's own roles
|
|
Meteor.publish(null, async function () {
|
|
// if (await Roles.userIsInRoleAsync(this.userId, "admin")) {
|
|
return Meteor.roleAssignment.find();
|
|
// }
|
|
|
|
// if (this.userId) {
|
|
// return Meteor.roleAssignment.find({ "user._id": this.userId });
|
|
// }
|
|
// this.ready();
|
|
});
|
|
|
|
// Publish roles for specific scope
|
|
// Meteor.publish("scopeRoles", function (scope) {
|
|
// if (this.userId) {
|
|
// return Meteor.roleAssignment.find({ scope: scope });
|
|
// }2024-12-19 21:40:26
|
|
// this.ready();
|
|
// });
|
|
Accounts.onCreateUser(async (options, user) => {
|
|
await Roles.addUsersToRolesAsync(user._id, ROLS_GLOBALS.USUARI);
|
|
return user;
|
|
});
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
Meteor.methods({
|
|
'assignaRol': async function (userId, rols, ambits) {
|
|
const esAdmin = await Roles.userIsInRoleAsync(Meteor.userId(), "admin");
|
|
esAdmin && await Roles.addUsersToRolesAsync(userId, rols, ambits);
|
|
console.log(`Rol "${rols}" assignat a l'usuari "${userId}" delimitat amb l'àmbit "${ambits}"`);
|
|
},
|
|
|
|
'retiraRols': async function (rols, userId, ambits) {
|
|
const esAdmin = await Roles.userIsInRoleAsync(Meteor.userId(), "admin");
|
|
esAdmin && await Roles.removeUsersFromRolesAsync(userId, rols, ambits);
|
|
|
|
console.log(`Retirats els rols "${rols}" de l'usuari "${userId}"`);
|
|
},
|
|
|
|
'editaOAfigPoble': async function (poble) {
|
|
const esAdmin = await Roles.userIsInRoleAsync(Meteor.userId(), "admin");
|
|
try {
|
|
console.log(`EDICIÓ DE POBLE sol·licitada per a "${poble.nomPoble}". Comprovant si ${Meteor.userId()} és Admin: `, esAdmin);
|
|
|
|
if (esAdmin && poble.nomPoble) {
|
|
return await PoblesCollection.upsertAsync(
|
|
{
|
|
_id: poble._id
|
|
}, {
|
|
$set: {
|
|
...poble,
|
|
usuari: Meteor.userId(),
|
|
editedAt: new Date()
|
|
}
|
|
}
|
|
);
|
|
} else {
|
|
throw new Error("El nom del poble no és vàlid");
|
|
}
|
|
} catch (e) {
|
|
console.error(e);
|
|
}
|
|
},
|
|
|
|
'afigNecessitat': async function (necessitat) {
|
|
const esAdmin = await Roles.userIsInRoleAsync(Meteor.userId(), "admin");
|
|
try {
|
|
console.log(`CREACIÓ DE NECESSITAT sol·licitada per a "${necessitat.titol}". Comprovant si ${Meteor.userId()} és Admin: `, esAdmin);
|
|
|
|
if (esAdmin && necessitat.titol) {
|
|
return await NecessitatsCollection.upsertAsync(
|
|
{
|
|
_id: necessitat._id
|
|
}, {
|
|
$set: {
|
|
...necessitat,
|
|
usuari: Meteor.userId(),
|
|
editedAt: new Date()
|
|
}
|
|
}
|
|
);
|
|
} else {
|
|
throw new Error("No tens permís o el títol no està correcte");
|
|
}
|
|
} catch (e) {
|
|
console.error(e);
|
|
}
|
|
},
|
|
|
|
'eliminaPoble': async function (pobleId) {
|
|
const esAdmin = await Roles.userIsInRoleAsync(Meteor.userId(), "admin");
|
|
try {
|
|
console.log(`ELIMINACIÓ DE POBLE sol·licitada per a ${pobleId}. Comprovant si ${Meteor.userId()} és Admin: `, esAdmin);
|
|
if (esAdmin && pobleId) {
|
|
return await PoblesCollection.removeAsync(pobleId);
|
|
} else {
|
|
throw new Error("El nom del poble no és vàlid");
|
|
}
|
|
} catch (e) {
|
|
console.error(e);
|
|
}
|
|
},
|
|
|
|
'eliminaUsuari': async function (usuariId) {
|
|
const esAdmin = await Roles.userIsInRoleAsync(Meteor.userId(), "admin");
|
|
try {
|
|
console.log(`ELIMINACIÓ D'USUARI sol·licitada per a ${usuariId}. Comprovant si ${Meteor.userId()} és Admin: `, esAdmin);
|
|
if (esAdmin && usuariId) {
|
|
return await Meteor.users.removeAsync(usuariId);
|
|
} else {
|
|
throw new Error("El nom de l'usuari no és vàlid");
|
|
}
|
|
} catch (e) {
|
|
console.error(e);
|
|
}
|
|
},
|
|
|
|
'afigTipus': async function (tipus) {
|
|
const esAdmin = await Roles.userIsInRoleAsync(Meteor.userId(), "admin");
|
|
try {
|
|
console.log(`CREACIÓ DE TIPUS sol·licitada per a "${tipus.titol}". Comprovant si ${Meteor.userId()} és Admin: `, esAdmin);
|
|
|
|
if (esAdmin && tipus.titol) {
|
|
return await TipusCollection.upsertAsync(
|
|
{
|
|
_id: tipus._id
|
|
}, {
|
|
$set: {
|
|
...tipus,
|
|
usuari: Meteor.userId(),
|
|
editedAt: new Date()
|
|
}
|
|
}
|
|
);
|
|
} else {
|
|
throw new Error("El nom del poble no és vàlid");
|
|
}
|
|
} catch (e) {
|
|
console.error(e);
|
|
}
|
|
},
|
|
|
|
'editaOAfigCodi': async function (codiObj) {
|
|
const esAdmin = await Roles.userIsInRoleAsync(Meteor.userId(), "admin");
|
|
try {
|
|
console.log(`CREACIÓ DE CODI sol·licitada per a "${codiObj.codi}". Comprovant si ${Meteor.userId()} és Admin: `, esAdmin);
|
|
|
|
if (esAdmin && codiObj.codi) {
|
|
return await CodisCollection.upsertAsync(
|
|
{
|
|
_id: codiObj._id
|
|
}, {
|
|
$set: {
|
|
...codiObj,
|
|
usuari: Meteor.userId(),
|
|
editedAt: new Date()
|
|
}
|
|
}
|
|
);
|
|
} else {
|
|
throw new Error("El valor del codi no és vàlid");
|
|
}
|
|
} catch (e) {
|
|
console.error(e);
|
|
}
|
|
|
|
},
|
|
|
|
'creaUsuariAmbCodi': async function (uObj, codi) {
|
|
|
|
const codiObj = await CodisCollection.findOneAsync({codi});
|
|
|
|
|
|
const userId = await Accounts.createUserAsync(uObj);
|
|
|
|
const ara = new Date();
|
|
let dataIni, dataFi;
|
|
|
|
console.log("codiObj.periode_validesa_ini: ", await codiObj.periode_validesa_ini);
|
|
|
|
if (codiObj.act_cond === "on") {
|
|
dataIni = new Date(await codiObj.periode_validesa_ini);
|
|
dataFi = new Date(await codiObj.periode_validesa_fi);
|
|
}
|
|
// const dataIni = codiObj.act_cond === "on" ? new Date(codiObj.periode_validesa_ini) : null;
|
|
// const dataFi = codiObj.act_cond === "on" ? new Date(codiObj.periode_validesa_fi) : null;
|
|
|
|
console.log("codiObj: ", codiObj);
|
|
console.log("ara: ", ara);
|
|
console.log("dataIni: ", dataIni);
|
|
console.log("dataFi: ", dataFi);
|
|
console.log(`codiObj.act_cond === "on"`, codiObj.act_cond === "on");
|
|
|
|
try {
|
|
console.log(`APLICANT CODI "${codi}" sobre "${userId}".`); // Comprovant si ${Meteor.userId()} és Admin: `, esAdmin);
|
|
|
|
if ((codiObj.act_cond==="on" && (ara >= dataIni && ara <= dataFi)) || codiObj.act_abs) {
|
|
Roles.addUsersToRolesAsync(userId, codiObj.rol, codiObj.ambit);
|
|
}
|
|
|
|
} catch (e) {
|
|
console.error(e);
|
|
}
|
|
|
|
},
|
|
|
|
'usaCodiAmbUsuari': async function (userId, codi) {
|
|
// const esAdmin = await Roles.userIsInRoleAsync(Meteor.userId(), "admin");
|
|
const codiObj = await CodisCollection.find({codi}).fetchAsync();
|
|
|
|
const ara = new Date();
|
|
let dataIni, dataFi;
|
|
|
|
if (codiObj.act_cond === "on") {
|
|
dataIni = new Date(codiObj.periode_validesa_ini);
|
|
dataFi = new Date(codiObj.periode_validesa_fi);
|
|
}
|
|
|
|
|
|
|
|
console.log("codiObj: ", codiObj);
|
|
|
|
try {
|
|
console.log(`APLICANT CODI "${codi}" sobre "${userId}".`); // Comprovant si ${Meteor.userId()} és Admin: `, esAdmin);
|
|
|
|
if ((codiObj.act_cond==="on" && (ara >= dataIni || ara <= dataFi)) || codiObj.act_abs) {
|
|
Roles.addUsersToRolesAsync(userId, codiObj.rol, codiObj.ambit);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// if (esAdmin && codiObj.codi) {
|
|
// return await CodisCollection.upsertAsync(
|
|
// {
|
|
// _id: codiObj._id
|
|
// }, {
|
|
// $set: {
|
|
// ...codiObj,
|
|
// usuari: Meteor.userId(),
|
|
// editedAt: new Date()
|
|
// }
|
|
// }
|
|
// );
|
|
// } else {
|
|
// throw new Error("El valor del codi no és vàlid");
|
|
// }
|
|
} catch (e) {
|
|
console.error(e);
|
|
}
|
|
|
|
},
|
|
|
|
'modActivacioAbsolutaCodi': async function (cod) {
|
|
|
|
console.log("modActivacioAbsolutaCodi: ", cod);
|
|
|
|
return await CodisCollection.updateAsync(
|
|
{ _id: cod._id },
|
|
{ $set: {
|
|
...cod,
|
|
'act_abs': cod.act_abs === "on" ? null : "on"
|
|
}}
|
|
)
|
|
}
|
|
});
|
|
|
|
}); |