mirror of
https://github.com/Paris-est-Ludique/intranet.git
synced 2025-06-08 08:34:20 +02:00
Remove useless types
This commit is contained in:
parent
3fd6741a78
commit
1e302e6c31
@ -37,7 +37,9 @@ export default class ExpressAccessors<
|
||||
}
|
||||
|
||||
// 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> => {
|
||||
try {
|
||||
const list = (await this.sheet.getList()) || []
|
||||
|
@ -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)
|
||||
|
@ -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<VolunteerLogin> => {
|
||||
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<VolunteerLogin>(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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user