mirror of
https://github.com/ArenMg/aren.git
synced 2024-11-25 18:08:02 +00:00
Use ArenServices for sondage
This commit is contained in:
parent
8c26b56e91
commit
85c001950a
3 changed files with 166 additions and 110 deletions
|
@ -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);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -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),
|
||||||
|
})
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -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,116 +245,84 @@ 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) => {
|
|
||||||
this.logout();
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
fetchUser: async function () {
|
fetchTheme() {
|
||||||
const _user = await axios.get(`${baseUrl}/ws/users/me`);
|
ArenService.Sondage.get({
|
||||||
this.user = _user.data;
|
id: this.$route.query.id,
|
||||||
console.log(this.user);
|
onSuccess: (myTheme) => {
|
||||||
|
this.theme = myTheme;
|
||||||
|
this.choices = [...myTheme.choices];
|
||||||
|
},
|
||||||
|
});
|
||||||
},
|
},
|
||||||
fetchTheme: async function () {
|
vote(subtheme, opinion) {
|
||||||
try {
|
ArenService.Vote.create({
|
||||||
const myTheme = await axios.get(
|
data: {
|
||||||
`${baseUrl}/ws/themes/${this.$route.query.id}`
|
subThemeId: subtheme,
|
||||||
);
|
authorId: this.user,
|
||||||
this.theme = myTheme.data;
|
opinion: opinion,
|
||||||
this.choices = [...myTheme.data.choices];
|
},
|
||||||
} catch (error) {
|
onSuccess: () => {
|
||||||
console.error(error);
|
location.reload();
|
||||||
}
|
},
|
||||||
|
});
|
||||||
},
|
},
|
||||||
vote: async function (subtheme, opinion) {
|
checkVoteAvalaibility(_subthemeId) {
|
||||||
try {
|
|
||||||
const makeVote = await axios({
|
|
||||||
method: "post",
|
|
||||||
url: `${baseUrl}/ws/votes`,
|
|
||||||
data: {
|
|
||||||
subThemeId: subtheme,
|
|
||||||
authorId: this.user,
|
|
||||||
opinion: opinion,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
// console.log(makeVote);
|
|
||||||
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) {
|
|
||||||
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(
|
||||||
const filteredBySubTheme = filteredVotes.map(
|
(el) => el.authorId?.id == userId
|
||||||
(el) => el.subThemeId.id || el.subThemeId
|
);
|
||||||
);
|
const filteredBySubTheme = filteredVotes.map(
|
||||||
|
(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;
|
||||||
},
|
|
||||||
deleteCurrentSondage: function () {
|
|
||||||
try {
|
|
||||||
swal({
|
|
||||||
title: "Êtes-vous sûr?",
|
|
||||||
text: "Le sondage sera supprimé avec toutes ses informations",
|
|
||||||
icon: "warning",
|
|
||||||
buttons: ["Annuler", true],
|
|
||||||
dangerMode: true,
|
|
||||||
}).then(async (willDelete) => {
|
|
||||||
if (willDelete) {
|
|
||||||
let _ = await axios.delete(
|
|
||||||
`${baseUrl}/ws/themes/delete/${this.$route.query.id}`
|
|
||||||
);
|
|
||||||
swal("Succès!", "Le sondage a été supprimé", "success").then(
|
|
||||||
(value) => {
|
|
||||||
location.href = baseUrl;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
swal("Erreur!", `${error}`, "error");
|
|
||||||
console.log(error);
|
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
fetchVotes() {
|
||||||
|
ArenService.Vote.getAll({
|
||||||
|
onSuccess: (f_votes) => (this.votes = f_votes),
|
||||||
|
});
|
||||||
|
},
|
||||||
|
deleteCurrentSondage() {
|
||||||
|
this.$confirm({
|
||||||
|
title: "Êtes-vous sûr?",
|
||||||
|
message: "Le sondage sera supprimé avec toutes ses informations",
|
||||||
|
callback: (returnValue) => {
|
||||||
|
if (returnValue) {
|
||||||
|
ArenService.Sondage.removeCurrent({
|
||||||
|
data: { id: this.$route.query.id, sondage: this.theme },
|
||||||
|
onSuccess: () =>
|
||||||
|
swal("Succès!", "Le sondage a été supprimé", "success").then(
|
||||||
|
(value) => {
|
||||||
|
this.$router.push("/");
|
||||||
|
}
|
||||||
|
),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue