Remove useless types

This commit is contained in:
pikiou 2022-01-22 00:55:54 +01:00
parent 3fd6741a78
commit 1e302e6c31
3 changed files with 136 additions and 146 deletions

View File

@ -37,7 +37,9 @@ export default class ExpressAccessors<
} }
// custom can be async // custom can be async
get(custom?: (list: Element[], body: Request["body"], id: number) => Promise<any> | any) { get<Ret = Element>(
custom?: (list: Element[], body: Request["body"], id: number) => Promise<Ret> | Ret
) {
return async (request: Request, response: Response, _next: NextFunction): Promise<void> => { return async (request: Request, response: Response, _next: NextFunction): Promise<void> => {
try { try {
const list = (await this.sheet.getList()) || [] const list = (await this.sheet.getList()) || []

View File

@ -16,6 +16,4 @@ export const preVolunteerGet = expressAccessor.get()
export const preVolunteerAdd = expressAccessor.add() export const preVolunteerAdd = expressAccessor.add()
export const preVolunteerSet = expressAccessor.set() export const preVolunteerSet = expressAccessor.set()
export const preVolunteerCountGet = expressAccessor.get( export const preVolunteerCountGet = expressAccessor.get((list) => list?.length || 0)
(list?: PreVolunteer[]) => (list && list.length) || 0
)

View File

@ -2,7 +2,7 @@ import _ from "lodash"
import bcrypt from "bcrypt" import bcrypt from "bcrypt"
import sgMail from "@sendgrid/mail" import sgMail from "@sendgrid/mail"
import ExpressAccessors, { RequestBody } from "./expressAccessors" import ExpressAccessors from "./expressAccessors"
import { import {
Volunteer, Volunteer,
VolunteerWithoutId, VolunteerWithoutId,
@ -25,163 +25,153 @@ export const volunteerListGet = expressAccessor.listGet()
export const volunteerAdd = expressAccessor.add() export const volunteerAdd = expressAccessor.add()
export const volunteerSet = expressAccessor.set() export const volunteerSet = expressAccessor.set()
export const volunteerLogin = expressAccessor.get( export const volunteerLogin = expressAccessor.get<VolunteerLogin>(async (list, bodyArray) => {
async (list: Volunteer[], bodyArray: RequestBody): Promise<VolunteerLogin> => { const [body] = bodyArray
const [body] = bodyArray const volunteer = getByEmail(list, body.email)
const volunteer = getByEmail(list, body.email) if (!volunteer) {
if (!volunteer) { throw Error("Il n'y a aucun bénévole avec cet email")
throw Error("Il n'y a aucun bénévole avec cet email") }
}
const password = body.password || "" const password = body.password || ""
const password1Match = await bcrypt.compare( const password1Match = await bcrypt.compare(
password,
volunteer.password1.replace(/^\$2y/, "$2a")
)
if (!password1Match) {
const password2Match = await bcrypt.compare(
password, password,
volunteer.password1.replace(/^\$2y/, "$2a") volunteer.password2.replace(/^\$2y/, "$2a")
) )
if (!password1Match) { if (!password2Match) {
const password2Match = await bcrypt.compare( throw Error("Mauvais mot de passe pour cet email")
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,
} }
} }
)
const jwt = await getJwt(volunteer.id)
return {
id: volunteer.id,
jwt,
}
})
const lastForgot: { [id: string]: number } = {} const lastForgot: { [id: string]: number } = {}
export const volunteerForgot = expressAccessor.set( export const volunteerForgot = expressAccessor.set(async (list, bodyArray) => {
async (list: Volunteer[], bodyArray: RequestBody) => { const [body] = bodyArray
const [body] = bodyArray const volunteer = getByEmail(list, body.email)
const volunteer = getByEmail(list, body.email) if (!volunteer) {
if (!volunteer) { throw Error("Il n'y a aucun bénévole avec cet email")
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 :/`,
},
}
} }
) const newVolunteer = _.cloneDeep(volunteer)
export const volunteerNotifsSet = expressAccessor.set( const now = +new Date()
async (list: Volunteer[], body: RequestBody, id: number) => { const timeSinceLastSent = now - lastForgot[volunteer.id]
const requestedId = +body[0] || id if (timeSinceLastSent < 2 * 60 * 1000) {
if (requestedId !== id && requestedId !== 0) { throw Error(
throw Error(`On ne peut acceder qu'à ses propres notifs`) "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 ?"
} )
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,
}
} }
) lastForgot[volunteer.id] = now
export const volunteerTeamWishesSet = expressAccessor.set( const password = generatePassword()
async (list: Volunteer[], body: RequestBody, id: number) => { const passwordHash = await bcrypt.hash(password, 10)
const requestedId = +body[0] || id newVolunteer.password2 = passwordHash
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) { await sendForgetEmail(volunteer.email, password)
newVolunteer.teamWishes = wishes.teamWishes
}
if (wishes.teamWishesComment !== undefined) {
newVolunteer.teamWishesComment = wishes.teamWishesComment
}
return { return {
toDatabase: newVolunteer, toDatabase: newVolunteer,
toCaller: { toCaller: {
id: newVolunteer.id, message: `Un nouveau mot de passe t'a été envoyé par email. Regarde bien dans les spams, il pourrait y être :/`,
teamWishes: newVolunteer.teamWishes, },
teamWishesComment: newVolunteer.teamWishesComment,
} as VolunteerTeamWishes,
}
} }
) })
export const volunteerDayWishesSet = expressAccessor.set( export const volunteerNotifsSet = expressAccessor.set(async (list, body, id) => {
async (list: Volunteer[], body: RequestBody, id: number) => { const requestedId = +body[0] || id
const requestedId = +body[0] || id if (requestedId !== id && requestedId !== 0) {
if (requestedId !== id && requestedId !== 0) { throw Error(`On ne peut acceder qu'à ses propres notifs`)
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,
}
} }
) 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 { function getByEmail(list: Volunteer[], rawEmail: string): Volunteer | undefined {
const email = canonicalEmail(rawEmail || "") const email = canonicalEmail(rawEmail || "")
const volunteer = list.find((v) => canonicalEmail(v.email) === email) const volunteer = list.find((v) => canonicalEmail(v.email) === email)