XaSuMu/server/main.js
2024-12-27 18:22:06 +01:00

384 lines
10 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';
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('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);
}
},
});
});