funky-framadate-front/src/app/services/config.service.ts

626 lines
20 KiB
TypeScript

import {Injectable} from '@angular/core';
import {PollConfig} from '../config/PollConfig';
import {HttpClient} from "@angular/common/http";
import {environment} from "../../environments/environment";
import {ConfirmationService, MessageService} from 'primeng/api';
import {Router} from "@angular/router";
import {mockMyPolls} from "../config/mocks/mockmypolls";
import {mockPoll3} from "../config/mocks/mock-poll3";
import {mockSuccessVote} from "../config/mocks/mock-success-vote";
import {PollUtilities} from "../config/PollUtilities";
/**
* le service transverse à chaque page qui permet de syncroniser la configuration de sondage souhaitée
*/
@Injectable({
providedIn: 'root'
})
export class ConfigService extends PollConfig {
loading: boolean = false;
baseHref: any = environment.baseApiHref;
constructor(private http: HttpClient,
private messageService: MessageService,
private router: Router,
private utils: PollUtilities,
private confirmationService: ConfirmationService,
) {
super();
this.fillValuesOnDevEnv();
}
set(key, val) {
this[key] = val;
}
// fill in mock values if we are not in production environment
fillValuesOnDevEnv() {
if (!environment.production) {
console.info(' ######### framadate ######### we are not in production env, filling with mock values');
this.currentPoll = mockPoll3;
this.myPolls = mockMyPolls;
}
}
/**
* add some days to a date, to compute intervals
* @param days
* @param date
*/
addDaysToDate(days: number, date: Date) {
date = new Date(date.valueOf());
date.setDate(date.getDate() + days);
return date;
};
/** ==================================
*
* poll public calls to get non authenticated info
*
* ==================================/
/**
* convert current poll config to a payload to send to the backend API
*/
getPollConfig() {
const jsonConfig = {
owner: {
email: this.myEmail,
pseudo: this.myName,
},
title: this.title,
description: this.description,
pollType: this.pollType,
visibility: this.visibility,
voteChoices: this.voteChoices,
allowSeveralHours: this.allowSeveralHours,
expirationDate: this.expirationDate,
passwordAccess: this.passwordAccess,
password: this.password,
customUrl: this.customUrl,
canModifyAnswers: this.canModifyAnswers,
whoModifiesAnswers: this.whoModifiesAnswers,
dateList: this.dateList,
timeList: this.timeList,
answers: this.answers,
expiracyDateDefaultInDays: this.expiracyDateDefaultInDays,
deletionDateAfterLastModification: this.deletionDateAfterLastModification,
};
return jsonConfig
}
checkIfSlugIsUniqueInDatabase(slug: string = '') {
this.customUrlIsUnique = null;
if (!slug) {
slug = this.utils.makeSlug(this);
}
this.loading = true;
// TODO
this.todo('check slug is unique');
this.http.get(`${this.baseHref}/check-slug-is-uniq/${slug}`,
this.utils.makeHeaders({slug: this.customUrl}),
)
.subscribe((res: any) => {
this.customUrlIsUnique = res.poll.isUnique;
this.loading = false;
},
(e) => this.handleError(e))
;
}
/**
* search in localstorage, fallback asking the backend to send an email to the owner if it exists
* @param email
*/
findPollsByEmail(email: string) {
this.findLocalStorageData();
// If no key is found in the localstorage, ask the backend to send an email to the user
this.myEmail = email;
this.todo('send email for real : TODO');
this.loading = true;
this.http.get(`${this.baseHref}/send-polls-to-user/${this.myEmail}`,
this.utils.makeHeaders(),
)
.subscribe(res => {
// message: 'Trouvé! Allez voir votre boite email',
this.myPolls = res;
this.loading = false;
this.messageService.add({
severity: 'success',
summary: 'Succès',
detail: `Vos infos de sondages vous ont été transmises. Allez voir votre boite email ${this.myEmail}`
});
}, (e) => {
this.handleError(e)
}
)
}
/**
* display error message depending on the response of the backend
* @param err
*/
handleError(err: any) {
console.error('err', err);
this.loading = false;
this.messageService.add({severity: 'warning', summary: "Erreur lors de l'appel ", detail: err.message});
}
findLocalStorageData() {
// TODO check if the person has a key to retrieve her polls
console.log('localStorage', localStorage);
if (localStorage) {
console.log('localStorage', localStorage)
}
}
/**
* get one poll by its slug name
* @param url
*/
getPollByURL(url: string) {
this.todo();
return this.http.get(`${this.baseHref}/poll/slug/${url}`, this.utils.makeHeaders())
}
/**
* GET
* api/v1/poll/{id}
* @param id
*/
getPollById(id: string, password?: string) {
return this.http
.get(`${this.baseHref}/poll/${id}`,
this.utils.makeHeaders({body: password}))
}
fetchPollFromRoute(event) {
console.log('time to fetch poll', event)
}
/**
* GET
* api/v1/my-polls
* @param ownerEmail
*/
getMyPolls(ownerEmail: string) {
this.http
.get(`${this.baseHref}/my-polls`,
this.utils.makeHeaders({ownerEmail: ownerEmail})
)
.subscribe(
(res: any) => {
// this.myPolls = res.poll;
}, (e) => {
this.handleError(e)
}
);
}
/**
* launch creation call to the api
*/
createPoll() {
this.loading = true;
this.createPollFromConfig(this.getPollConfig())
}
updateCurrentPollFromResponse(res: any) {
console.log('update res', res);
this.currentPoll = res;
this.pollId = res.poll.id;
this.owner_modifier_token = res.owner_modifier_token;
this.urlPublic = this.baseHref + '#/vote/poll/id/' + res.poll.id;
this.urlSlugPublic = this.baseHref + '#/vote/poll/slug/' + res.poll.id;
if (res.poll.customUrl) {
this.urlSlugPublic = this.baseHref + '#/vote/poll/id/' + res.poll.customUrl;
}
if (res.voteStack) {
this.loadVoteStack(res.voteStack);
}
this.adminKey = res.admin_key;
this.urlAdmin = this.baseHref + '#/admin/' + res.admin_key;
}
resetCurrentChoicesAnswers() {
this.currentPoll.choices.map(choice => {
choice.answer = null;
})
}
/**
* update current answers with a previous vote
* @param voteStack
*/
loadVoteStack(voteStack: any) {
// load the pseudo and email
this.myName = voteStack.pseudo;
this.myEmail = voteStack.email;
this.voteStackId = voteStack.id;
this.myVoteStack = voteStack;
let keys = Object.keys(voteStack.votes);
console.log('voteStack', voteStack);
this.resetCurrentChoicesAnswers();
keys.forEach((id: any) => {
let voteItem = voteStack.votes[id];
/**
* the display of the poll uses the choices data, so we update the choices answers of the current poll to reflect the vote stack we have taken
*/
if (voteItem.choice_id && voteItem.value) {
let foundChoiceToModify = this.currentPoll.choices.find(choicesItem => {
return voteItem.choice_id == choicesItem.id
});
console.log('foundChoiceToModify', foundChoiceToModify);
if (foundChoiceToModify) {
foundChoiceToModify.answer = voteItem.value;
}
}
})
}
/**
* POST
* /api/v1/poll/{id}/poll
* @param config
*/
createPollFromConfig(config: any) {
this.loading = true;
console.log('config', config);
return this.http.post(`${this.baseHref}/poll`,
config,
this.utils.makeHeaders())
.subscribe((res: any) => {
// redirect to the page to administrate the new poll
this.messageService.add({severity: 'success', summary: 'Sondage Créé',});
this.updateCurrentPollFromResponse(res);
this.loading = false;
if (!this.myPolls) {
this.myPolls = [];
}
this.myPolls.push(res);
this.router.navigate(['step/end']);
// TODO save new poll to localstorage
// reset all fields in current config
this.resetConfig();
}, (e) => {
this.handleError(e)
}
);
}
/**
* conversion to send to back
* @param choiceList
*/
convertChoicesAnsweredToSend(choiceList) {
choiceList = choiceList.filter(c => c.answer ? c : null); // remove choices where we did not answer
const converted = choiceList.map(elem => {
if (elem.answer) {
return {
choice_id: elem.id,
value: elem.answer
}
}
});
console.log('converted', converted);
return converted;
}
/**
* POST
* /api/v1/poll/{id}/vote
* @param voteStack
*/
addVote(voteStack?: any) {
if (!voteStack) {
voteStack = {
pseudo: this.myName,
email: this.myEmail,
votes: this.convertChoicesAnsweredToSend(this.currentPoll.choices),
}
}
this.myVoteStack = voteStack;
if (!environment.production) {
this.handleVoteAdded(mockSuccessVote);
return;
}
this.http.post(
`${this.baseHref}/poll/${this.pollId}/vote`,
voteStack,
this.utils.makeHeaders())
.subscribe((res: any) => {
this.handleVoteAdded(res);
}, (e) => {
this.handleError(e)
}
);
}
handleVoteAdded(res) {
if (this.isAdmin) {
this.displayConfirmVoteModalAdmin = true;
}
// save modifier token
this.myVoteStack['modifier_token'] = res.modifier_token;
this.myVoteStack['id'] = res.vote_stack.id;
this.updateCurrentPollFromResponse(res);
}
/**
* UPDATE
* /api/v1/poll/{id}/vote
* @param voteStack
*/
updateVote(voteStack?: any) {
if (!this.myVoteStack) {
return;
} else {
voteStack = this.myVoteStack;
}
this.http.patch(
`${this.baseHref}/vote-stack/${voteStack.id}/token/${this.owner_modifier_token}`,
voteStack,
this.utils.makeHeaders())
.subscribe((res: any) => {
this.messageService.add({severity: 'success', summary: 'Vote mis à jour'});
this.updateCurrentPollFromResponse(res);
}, (e) => {
this.handleError(e)
}
);
}
/**
* POST
* /api/v1/poll/{id}/comment
* @param comment
*/
addComment(comment?: any) {
if (!comment && this.myComment) {
comment = {
name: this.myName,
pseudo: this.myName,
email: this.myEmail,
date: new Date(),
text: this.myComment,
}
}
console.log('comment', comment);
this.http.post(
`${this.baseHref}/poll/${this.pollId}/comment`,
comment,
this.utils.makeHeaders())
.subscribe((res: any) => {
this.messageService.add({
severity: 'success',
summary: 'Commentaire Créé',
detail: comment.text
});
// empty comment after success
this.myComment = '';
comment.date = {
date: comment.date
};
this.currentPoll.comments.push(comment);
}, (e) => {
this.handleError(e);
}
);
}
/**
* administrator calls
*/
deleteComments() {
// prompt for confirmation
this.confirmationService.confirm({
message: 'Are you sure that you want to completely delete the comments of this poll (' + this.title + ') permanentely?',
accept: () => {
this.http.delete(
`${this.baseHref}/poll/${this.pollId}/comments`,
this.utils.makeHeaders())
.subscribe((res: any) => {
this.messageService.add({
severity: 'success',
summary: 'Commentaires bien supprimés',
detail: 'Commentaires du sondage "' + this.title + '" supprimé'
});
}, (e) => {
this.handleError(e)
}
);
}
});
}
deleteVotes() {
// prompt for confirmation
this.confirmationService.confirm({
message: 'Are you sure that you want to completely delete the votes of this poll (' + this.title + ') permanentely?',
accept: () => {
this.http.delete(
`${this.baseHref}/poll/${this.pollId}/votes`,
this.utils.makeHeaders())
.subscribe((res: any) => {
this.messageService.add({
severity: 'success',
summary: 'Votes bien supprimés',
detail: 'Votes du sondage "' + this.title + '" supprimé'
});
}, (e) => {
this.handleError(e)
}
);
}
});
}
deletePoll() {
if (!this.pollId) {
this.messageService.add({
summary: 'this poll is not administrable, it has no ID',
severity: 'warning'
});
return;
}
let self = this;
// prompt for confirmation
this.confirmationService.confirm({
message: 'Are you sure that you want to completely delete this poll (' + self.title + ') and all is data permanentely?',
accept: () => {
this.http.delete(
`${this.baseHref}/poll/${this.pollId}`,
this.utils.makeHeaders())
.subscribe((res: any) => {
this.messageService.add({
severity: 'success',
summary: 'Sondage bien supprimé',
detail: 'sondage "' + this.title + '" supprimé'
});
this.router.navigate(['home']);
}, (e) => {
this.handleError(e)
}
);
}
});
}
/**
* UPDATE
* /api/v1/poll/{id}/vote
* @param voteStack
* TODO
*/
updatePoll(voteStack: any) {
this.http.put(
`${this.baseHref}/poll/${this.pollId}`,
voteStack,
this.utils.makeHeaders()
)
.subscribe((res: any) => {
this.messageService.add({
severity: 'success',
summary: 'Sondage mis à jour',
});
this.updateCurrentPollFromResponse(res);
}, (e) => {
this.handleError(e)
}
);
}
/**
* TODO
* export all the poll data available to the public as a CSV single file
*/
exportCSV() {
let rows = [];
let now = new Date();
const headers = [
['export de sondage Framadate ', this.customUrl],
['le', now.toISOString()],
[this.currentPoll.pollId, this.currentPoll.title, this.customUrl, this.creationDate],
['pseudo']];
let listOfChoices = ['choices : '];
this.currentPoll.choices.map(choice => {
listOfChoices.push(choice.text)
});
listOfChoices.push('pseudo');
this.currentPoll.stacks.map(voteStack => {
let voteStackInArray = [voteStack.pseudo];
let keysVotes = Object.keys(voteStack.votes);
keysVotes.map(id => {
voteStackInArray.push(voteStack.votes[id].value ? voteStack.votes[id].value : "")
});
rows.push(
voteStackInArray
);
});
const headersComments = [
['comments'],
['pseudo', 'text', 'creation_date'],
];
const comments = [];
this.currentPoll.comments.map(item => {
comments.push(
[item.pseudo,
item.text,
item.date.date,
'\n']
)
});
headers.push(listOfChoices);
rows = [headers, listOfChoices, rows, headersComments, comments];
let convertedCsv = rows.map(elem => {
console.log('elem', elem);
return elem.map(item => {
console.log('item', item);
if (typeof item === typeof Array) {
return item.join('\n')
}
return item;
}).join('\n')
}).join('\n');
console.log('rows', rows);
console.log('convertedCsv', convertedCsv);
let csvContent = "data:text/csv;charset=utf-8,"
+ convertedCsv;
console.log('csvContent', csvContent);
var encodedUri = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
let exportFileName = (this.urlPublic ? this.urlPublic : this.utils.makeSlug(this)) + "_export_" + new Date() + ".csv";
link.setAttribute("download", exportFileName);
document.body.appendChild(link); // Required for FF
link.click(); // This will download the data file named "my_data.csv".
}
print() {
alert('TODO');
}
todo(message = '') {
this.messageService.add({
severity: 'info' + message,
detail: "cette fonctionnalité n'est pas encore disponible. Venez en discuter sur framateam.org / Ux et design libre / Framasoft",
summary: "Work in progress",
});
}
}