Use ArenServices for sondage

This commit is contained in:
Henintsoa 2021-12-12 11:09:43 +03:00
parent 8c26b56e91
commit 85c001950a
3 changed files with 166 additions and 110 deletions

View file

@ -317,6 +317,8 @@ User.prototype.oneToMany = {
comments: [Comment, "owner"], comments: [Comment, "owner"],
createdDebates: [Debate, "owner"], createdDebates: [Debate, "owner"],
notifications: [Notification, "owner"], notifications: [Notification, "owner"],
sondage: [Sondage, "author"],
vote: [Vote, "authorId"],
}; };
User.prototype.manyToOne = { User.prototype.manyToOne = {
institution: [Institution, "users"], institution: [Institution, "users"],
@ -336,6 +338,69 @@ User.prototype.is = function (authority) {
return Authority._value[this.authority] >= Authority._value[authority]; return Authority._value[this.authority] >= Authority._value[authority];
}; };
function Vote(obj = {}) {
Entity.call(this, obj);
}
Vote.prototype.attrs = {
id: Number,
opinion: String,
createdAt: Date,
};
Vote.prototype.manyToOne = {
authorId: [User, "vote"],
subThemeId: [Sondage, "sondage"],
vote: [Choice, "votes"]
};
function Choice(obj = {}) {
Entity.call(this, obj);
}
Choice.prototype.attrs = {
id: Number,
title: String,
description: String,
url: String,
img: String,
neutral: Number,
against: Number,
for: Number,
createdAt: Date,
};
Choice.prototype.manyToOne = {
themeId: [Sondage, "choices"],
};
Choice.prototype.oneToMany = {
votes: [Vote, "vote"],
};
function Agenda(obj = {}) {
Entity.call(this, obj);
}
Agenda.prototype.attrs = {
id: Number,
title: String,
description: String,
url: String,
date: Date,
};
function Sondage(obj = {}) {
Entity.call(this, obj);
}
Sondage.prototype.attrs = {
id: Number,
title: String,
description: String,
url: String,
createdAt: Date,
expiracyDate: Date,
};
Sondage.prototype.manyToOne = {
author: [User, "sondage"],
};
Sondage.prototype.oneToMany = {
sondage: [Vote, "subThemeId"],
choices: [Choice, "themeId"],
};
const ApiService = function (anUrl, locale) { const ApiService = function (anUrl, locale) {
let self = this; let self = this;
@ -349,6 +414,10 @@ const ApiService = function (anUrl, locale) {
this.Notification = []; this.Notification = [];
this.Team = []; this.Team = [];
this.User = []; this.User = [];
this.Sondage = [];
this.Agenda = [];
this.Choice = [];
this.Vote = [];
}, },
detach(obj) { detach(obj) {
this[obj.constructor.name].remove(obj); this[obj.constructor.name].remove(obj);
@ -443,8 +512,8 @@ const ApiService = function (anUrl, locale) {
); );
if (manyToOne[foreignKey][1]) { if (manyToOne[foreignKey][1]) {
let foreignCollection = that[foreignKey][manyToOne[foreignKey][1]]; let foreignCollection = that[foreignKey][manyToOne[foreignKey][1]];
if (!foreignCollection.includes(that)) { if (!(foreignCollection?.includes(that))) {
foreignCollection.push(that); foreignCollection?.push(that);
} }
} }
} }
@ -789,6 +858,10 @@ const ApiService = function (anUrl, locale) {
this.Notifications = new EntityProcessor("notifications", Notification); this.Notifications = new EntityProcessor("notifications", Notification);
this.CommentListener = new Listener("comments", Comment); this.CommentListener = new Listener("comments", Comment);
this.NotificationListener = new Listener("notifications", Notification); this.NotificationListener = new Listener("notifications", Notification);
this.Agenda = new EntityProcessor("agenda/calendars", Agenda);
this.Sondage = new EntityProcessor("themes", Sondage);
this.Choice = new EntityProcessor("choices", Choice);
this.Vote = new EntityProcessor("votes", Vote);
this.Users.getLoged = function (params = {}) { this.Users.getLoged = function (params = {}) {
params.method = "GET"; params.method = "GET";
@ -976,4 +1049,16 @@ const ApiService = function (anUrl, locale) {
params.method = "PUT"; params.method = "PUT";
this.call(params); this.call(params);
}; };
this.Sondage.removeCurrent = function (params = {}) {
params.method = "DELETE";
params.path = `/delete/${params.data.id}`;
let onSuccess = params.onSuccess;
params.onSuccess = function () {
self.Store.remove(params.data.sondage);
if(onSuccess){
onSuccess();
}
}
this.call(params);
}
}; };

View file

@ -114,7 +114,8 @@ module.exports = {
return { return {
categories: false, categories: false,
agenda: [], agenda: [],
sondages: [], // sondages: [],
sondages: false,
}; };
}, },
created() { created() {
@ -141,13 +142,19 @@ module.exports = {
}, },
//Fetch sondages //Fetch sondages
async fetchSondages() { // async fetchSondages() {
try { // try {
let agenda = await axios.get(`${baseUrl}/ws/themes`); // let agenda = await axios.get(`${baseUrl}/ws/themes`);
this.sondages.push(...agenda.data); // this.sondages.push(...agenda.data);
} catch (expetion) { // } catch (expetion) {
console.error(expetion); // console.error(expetion);
} // }
// },
fetchSondages() {
ArenService.Sondage.getAll({
onSuccess: (sondages)=>(this.sondages = sondages),
})
}, },
}, },
}; };

View file

@ -3,7 +3,7 @@
<base-layout id="voting"> <base-layout id="voting">
<template v-slot:title> <template v-slot:title>
<h1>Sondage</h1> <h1>Sondage</h1>
<div v-if="loggedUser.is('ADMIN') || loggedUser.is('SUPERADMIN')"> <div v-if="user.is('ADMIN') || user.is('SUPERADMIN')">
<button <button
title="Supprimer le sondage" title="Supprimer le sondage"
class="delete-btn space-top" class="delete-btn space-top"
@ -238,10 +238,6 @@
</style> </style>
<script> <script>
const getUrl = window.location; const getUrl = window.location;
// let baseUrl = getUrl.protocol + "//" + getUrl.host + "/" + getUrl.pathname.split("/")[1];
// if (baseUrl.endsWith("/")) {
// baseUrl = baseUrl.slice(0, -1);
// }
let baseUrl = getUrl.protocol + "//" + getUrl.host; let baseUrl = getUrl.protocol + "//" + getUrl.host;
module.exports = { module.exports = {
data() { data() {
@ -249,117 +245,85 @@ module.exports = {
user: false, user: false,
theme: false, theme: false,
choices: [], choices: [],
votes: [], votes: false,
loggedUser: false,
}; };
}, },
created() { created() {
this.fetchTheme(); if (!this.$root.user.is("USER")) {
this.fetchUser(); this.$router.push("/404");
this.fetchVotes(); return;
}
this.getCurrentUser(); this.getCurrentUser();
this.fetchTheme();
this.fetchVotes();
}, },
methods: { methods: {
getCurrentUser: function () { getCurrentUser() {
ArenService.Users.getLoged({ ArenService.Users.getLoged({
onSuccess: (logedUser) => { onSuccess: (logedUser) => {
this.loggedUser = logedUser; this.user = logedUser;
console.log(logedUser)
ArenService.NotificationListener.listen({
onMessage: (notif) => {
this.user.notifications.push(notif);
let message = this.$t(
"notification." + notif.content.message,
notif.content.details
);
this.$toast(message);
new BrowserNotification("AREN", {
body: message,
});
}, },
}); });
}, },
onError: (e) => { fetchTheme() {
this.logout(); ArenService.Sondage.get({
id: this.$route.query.id,
onSuccess: (myTheme) => {
this.theme = myTheme;
this.choices = [...myTheme.choices];
}, },
}); });
}, },
fetchUser: async function () { vote(subtheme, opinion) {
const _user = await axios.get(`${baseUrl}/ws/users/me`); ArenService.Vote.create({
this.user = _user.data;
console.log(this.user);
},
fetchTheme: async function () {
try {
const myTheme = await axios.get(
`${baseUrl}/ws/themes/${this.$route.query.id}`
);
this.theme = myTheme.data;
this.choices = [...myTheme.data.choices];
} catch (error) {
console.error(error);
}
},
vote: async function (subtheme, opinion) {
try {
const makeVote = await axios({
method: "post",
url: `${baseUrl}/ws/votes`,
data: { data: {
subThemeId: subtheme, subThemeId: subtheme,
authorId: this.user, authorId: this.user,
opinion: opinion, opinion: opinion,
}, },
}); onSuccess: () => {
// console.log(makeVote);
location.reload(); location.reload();
this.fetchTheme();
} catch (error) {
console.error(error);
}
}, },
fetchVotes: async function () { });
const _votes = await axios.get(`${baseUrl}/ws/votes`);
this.votes = [..._votes.data];
// console.log(this.votes.filter(el => el.authorId == this.user.id));
}, },
checkVoteAvalaibility: function (_subthemeId) { checkVoteAvalaibility(_subthemeId) {
const userId = this.user.id; const userId = this.user.id;
const filteredVotes = this.votes.filter((el) => el.authorId == userId); if (this.votes) {
const filteredVotes = this.votes?.filter(
(el) => el.authorId?.id == userId
);
const filteredBySubTheme = filteredVotes.map( const filteredBySubTheme = filteredVotes.map(
(el) => el.subThemeId.id || el.subThemeId (el) => el.subThemeId?.id ?? el.subThemeId
); );
const arr = filteredBySubTheme.filter((el) => el == _subthemeId); const arr = filteredBySubTheme.filter((el) => el == _subthemeId);
return arr.length <= 0; return arr.length <= 0;
}
return false;
}, },
deleteCurrentSondage: function () { fetchVotes() {
try { ArenService.Vote.getAll({
swal({ onSuccess: (f_votes) => (this.votes = f_votes),
});
},
deleteCurrentSondage() {
this.$confirm({
title: "Êtes-vous sûr?", title: "Êtes-vous sûr?",
text: "Le sondage sera supprimé avec toutes ses informations", message: "Le sondage sera supprimé avec toutes ses informations",
icon: "warning", callback: (returnValue) => {
buttons: ["Annuler", true], if (returnValue) {
dangerMode: true, ArenService.Sondage.removeCurrent({
}).then(async (willDelete) => { data: { id: this.$route.query.id, sondage: this.theme },
if (willDelete) { onSuccess: () =>
let _ = await axios.delete(
`${baseUrl}/ws/themes/delete/${this.$route.query.id}`
);
swal("Succès!", "Le sondage a été supprimé", "success").then( swal("Succès!", "Le sondage a été supprimé", "success").then(
(value) => { (value) => {
location.href = baseUrl; this.$router.push("/");
}
);
} }
),
}); });
} catch (error) {
swal("Erreur!", `${error}`, "error");
console.log(error);
} }
}, },
});
},
}, },
}; };
</script> </script>