From 1e302e6c31432542e8c83bb51ea0b4bf20b3cba6 Mon Sep 17 00:00:00 2001 From: pikiou Date: Sat, 22 Jan 2022 00:55:54 +0100 Subject: [PATCH] Remove useless types --- src/server/gsheets/expressAccessors.ts | 4 +- src/server/gsheets/preVolunteers.ts | 4 +- src/server/gsheets/volunteers.ts | 274 ++++++++++++------------- 3 files changed, 136 insertions(+), 146 deletions(-) diff --git a/src/server/gsheets/expressAccessors.ts b/src/server/gsheets/expressAccessors.ts index e03a394..784bfde 100644 --- a/src/server/gsheets/expressAccessors.ts +++ b/src/server/gsheets/expressAccessors.ts @@ -37,7 +37,9 @@ export default class ExpressAccessors< } // custom can be async - get(custom?: (list: Element[], body: Request["body"], id: number) => Promise | any) { + get( + custom?: (list: Element[], body: Request["body"], id: number) => Promise | Ret + ) { return async (request: Request, response: Response, _next: NextFunction): Promise => { try { const list = (await this.sheet.getList()) || [] diff --git a/src/server/gsheets/preVolunteers.ts b/src/server/gsheets/preVolunteers.ts index 7bdf49b..553f6a5 100644 --- a/src/server/gsheets/preVolunteers.ts +++ b/src/server/gsheets/preVolunteers.ts @@ -16,6 +16,4 @@ export const preVolunteerGet = expressAccessor.get() export const preVolunteerAdd = expressAccessor.add() export const preVolunteerSet = expressAccessor.set() -export const preVolunteerCountGet = expressAccessor.get( - (list?: PreVolunteer[]) => (list && list.length) || 0 -) +export const preVolunteerCountGet = expressAccessor.get((list) => list?.length || 0) diff --git a/src/server/gsheets/volunteers.ts b/src/server/gsheets/volunteers.ts index 0e312fe..a3e5b46 100644 --- a/src/server/gsheets/volunteers.ts +++ b/src/server/gsheets/volunteers.ts @@ -2,7 +2,7 @@ import _ from "lodash" import bcrypt from "bcrypt" import sgMail from "@sendgrid/mail" -import ExpressAccessors, { RequestBody } from "./expressAccessors" +import ExpressAccessors from "./expressAccessors" import { Volunteer, VolunteerWithoutId, @@ -25,163 +25,153 @@ export const volunteerListGet = expressAccessor.listGet() export const volunteerAdd = expressAccessor.add() export const volunteerSet = expressAccessor.set() -export const volunteerLogin = expressAccessor.get( - async (list: Volunteer[], bodyArray: RequestBody): Promise => { - const [body] = bodyArray - const volunteer = getByEmail(list, body.email) - if (!volunteer) { - throw Error("Il n'y a aucun bénévole avec cet email") - } +export const volunteerLogin = expressAccessor.get(async (list, bodyArray) => { + const [body] = bodyArray + const volunteer = getByEmail(list, body.email) + if (!volunteer) { + throw Error("Il n'y a aucun bénévole avec cet email") + } - const password = body.password || "" - const password1Match = await bcrypt.compare( + const password = body.password || "" + const password1Match = await bcrypt.compare( + password, + volunteer.password1.replace(/^\$2y/, "$2a") + ) + if (!password1Match) { + const password2Match = await bcrypt.compare( password, - volunteer.password1.replace(/^\$2y/, "$2a") + volunteer.password2.replace(/^\$2y/, "$2a") ) - if (!password1Match) { - const password2Match = await bcrypt.compare( - password, - volunteer.password2.replace(/^\$2y/, "$2a") - ) - if (!password2Match) { - throw Error("Mauvais mot de passe pour cet email") - } - } - - const jwt = await getJwt(volunteer.id) - - return { - id: volunteer.id, - jwt, + if (!password2Match) { + throw Error("Mauvais mot de passe pour cet email") } } -) + + const jwt = await getJwt(volunteer.id) + + return { + id: volunteer.id, + jwt, + } +}) const lastForgot: { [id: string]: number } = {} -export const volunteerForgot = expressAccessor.set( - async (list: Volunteer[], bodyArray: RequestBody) => { - const [body] = bodyArray - const volunteer = getByEmail(list, body.email) - if (!volunteer) { - throw Error("Il n'y a aucun bénévole avec cet email") - } - const newVolunteer = _.cloneDeep(volunteer) - - const now = +new Date() - const timeSinceLastSent = now - lastForgot[volunteer.id] - if (timeSinceLastSent < 2 * 60 * 1000) { - throw Error( - "Un email t'a déjà été envoyé avec un nouveau mot de passe. Es-tu sûr qu'il n'est pas dans tes spams ?" - ) - } - lastForgot[volunteer.id] = now - - const password = generatePassword() - const passwordHash = await bcrypt.hash(password, 10) - newVolunteer.password2 = passwordHash - - await sendForgetEmail(volunteer.email, password) - - return { - toDatabase: newVolunteer, - toCaller: { - message: `Un nouveau mot de passe t'a été envoyé par email. Regarde bien dans les spams, il pourrait y être :/`, - }, - } +export const volunteerForgot = expressAccessor.set(async (list, bodyArray) => { + const [body] = bodyArray + const volunteer = getByEmail(list, body.email) + if (!volunteer) { + throw Error("Il n'y a aucun bénévole avec cet email") } -) + const newVolunteer = _.cloneDeep(volunteer) -export const volunteerNotifsSet = expressAccessor.set( - async (list: Volunteer[], body: RequestBody, id: number) => { - const requestedId = +body[0] || id - if (requestedId !== id && requestedId !== 0) { - throw Error(`On ne peut acceder qu'à ses propres notifs`) - } - const notifChanges = body[1] - const volunteer = list.find((v) => v.id === requestedId) - if (!volunteer) { - throw Error(`Il n'y a aucun bénévole avec cet identifiant ${requestedId}`) - } - const newVolunteer = _.cloneDeep(volunteer) - - _.assign(newVolunteer, _.pick(notifChanges, _.keys(newVolunteer))) - - return { - toDatabase: newVolunteer, - toCaller: { - id: newVolunteer.id, - firstname: newVolunteer.firstname, - adult: newVolunteer.adult, - active: newVolunteer.active, - hiddenNotifs: newVolunteer.hiddenNotifs, - pushNotifSubscription: newVolunteer.pushNotifSubscription, - acceptsNotifs: newVolunteer.acceptsNotifs, - } as VolunteerNotifs, - } + const now = +new Date() + const timeSinceLastSent = now - lastForgot[volunteer.id] + if (timeSinceLastSent < 2 * 60 * 1000) { + throw Error( + "Un email t'a déjà été envoyé avec un nouveau mot de passe. Es-tu sûr qu'il n'est pas dans tes spams ?" + ) } -) + lastForgot[volunteer.id] = now -export const volunteerTeamWishesSet = expressAccessor.set( - async (list: Volunteer[], body: RequestBody, id: number) => { - const requestedId = +body[0] || id - if (requestedId !== id && requestedId !== 0) { - throw Error(`On ne peut acceder qu'à ses propres envies d'équipes`) - } - const wishes = body[1] as VolunteerTeamWishes - const volunteer = list.find((v) => v.id === requestedId) - if (!volunteer) { - throw Error(`Il n'y a aucun bénévole avec cet identifiant ${requestedId}`) - } - const newVolunteer = _.cloneDeep(volunteer) + const password = generatePassword() + const passwordHash = await bcrypt.hash(password, 10) + newVolunteer.password2 = passwordHash - if (wishes.teamWishes !== undefined) { - newVolunteer.teamWishes = wishes.teamWishes - } - if (wishes.teamWishesComment !== undefined) { - newVolunteer.teamWishesComment = wishes.teamWishesComment - } + await sendForgetEmail(volunteer.email, password) - return { - toDatabase: newVolunteer, - toCaller: { - id: newVolunteer.id, - teamWishes: newVolunteer.teamWishes, - teamWishesComment: newVolunteer.teamWishesComment, - } as VolunteerTeamWishes, - } + return { + toDatabase: newVolunteer, + toCaller: { + message: `Un nouveau mot de passe t'a été envoyé par email. Regarde bien dans les spams, il pourrait y être :/`, + }, } -) +}) -export const volunteerDayWishesSet = expressAccessor.set( - async (list: Volunteer[], body: RequestBody, id: number) => { - const requestedId = +body[0] || id - if (requestedId !== id && requestedId !== 0) { - throw Error(`On ne peut acceder qu'à ses propres envies de jours`) - } - const wishes = body[1] as VolunteerDayWishes - const volunteer = list.find((v) => v.id === requestedId) - if (!volunteer) { - throw Error(`Il n'y a aucun bénévole avec cet identifiant ${requestedId}`) - } - const newVolunteer = _.cloneDeep(volunteer) - - if (wishes.dayWishes !== undefined) { - newVolunteer.dayWishes = wishes.dayWishes - } - if (wishes.dayWishesComment !== undefined) { - newVolunteer.dayWishesComment = wishes.dayWishesComment - } - - return { - toDatabase: newVolunteer, - toCaller: { - id: newVolunteer.id, - dayWishes: newVolunteer.dayWishes, - dayWishesComment: newVolunteer.dayWishesComment, - } as VolunteerDayWishes, - } +export const volunteerNotifsSet = expressAccessor.set(async (list, body, id) => { + const requestedId = +body[0] || id + if (requestedId !== id && requestedId !== 0) { + throw Error(`On ne peut acceder qu'à ses propres notifs`) } -) + const notifChanges = body[1] + const volunteer = list.find((v) => v.id === requestedId) + if (!volunteer) { + throw Error(`Il n'y a aucun bénévole avec cet identifiant ${requestedId}`) + } + const newVolunteer = _.cloneDeep(volunteer) + + _.assign(newVolunteer, _.pick(notifChanges, _.keys(newVolunteer))) + + return { + toDatabase: newVolunteer, + toCaller: { + id: newVolunteer.id, + firstname: newVolunteer.firstname, + adult: newVolunteer.adult, + active: newVolunteer.active, + hiddenNotifs: newVolunteer.hiddenNotifs, + pushNotifSubscription: newVolunteer.pushNotifSubscription, + acceptsNotifs: newVolunteer.acceptsNotifs, + } as VolunteerNotifs, + } +}) + +export const volunteerTeamWishesSet = expressAccessor.set(async (list, body, id) => { + const requestedId = +body[0] || id + if (requestedId !== id && requestedId !== 0) { + throw Error(`On ne peut acceder qu'à ses propres envies d'équipes`) + } + const wishes = body[1] as VolunteerTeamWishes + const volunteer = list.find((v) => v.id === requestedId) + if (!volunteer) { + throw Error(`Il n'y a aucun bénévole avec cet identifiant ${requestedId}`) + } + const newVolunteer = _.cloneDeep(volunteer) + + if (wishes.teamWishes !== undefined) { + newVolunteer.teamWishes = wishes.teamWishes + } + if (wishes.teamWishesComment !== undefined) { + newVolunteer.teamWishesComment = wishes.teamWishesComment + } + + return { + toDatabase: newVolunteer, + toCaller: { + id: newVolunteer.id, + teamWishes: newVolunteer.teamWishes, + teamWishesComment: newVolunteer.teamWishesComment, + } as VolunteerTeamWishes, + } +}) + +export const volunteerDayWishesSet = expressAccessor.set(async (list, body, id) => { + const requestedId = +body[0] || id + if (requestedId !== id && requestedId !== 0) { + throw Error(`On ne peut acceder qu'à ses propres envies de jours`) + } + const wishes = body[1] as VolunteerDayWishes + const volunteer = list.find((v) => v.id === requestedId) + if (!volunteer) { + throw Error(`Il n'y a aucun bénévole avec cet identifiant ${requestedId}`) + } + const newVolunteer = _.cloneDeep(volunteer) + + if (wishes.dayWishes !== undefined) { + newVolunteer.dayWishes = wishes.dayWishes + } + if (wishes.dayWishesComment !== undefined) { + newVolunteer.dayWishesComment = wishes.dayWishesComment + } + + return { + toDatabase: newVolunteer, + toCaller: { + id: newVolunteer.id, + dayWishes: newVolunteer.dayWishes, + dayWishesComment: newVolunteer.dayWishesComment, + } as VolunteerDayWishes, + } +}) function getByEmail(list: Volunteer[], rawEmail: string): Volunteer | undefined { const email = canonicalEmail(rawEmail || "") const volunteer = list.find((v) => canonicalEmail(v.email) === email)