Move database accessors in separate files to separate them from database definitions

This commit is contained in:
pikiou 2022-01-26 22:37:19 +01:00
parent bbe4f9b1d1
commit 110f3505ab
28 changed files with 130 additions and 94 deletions

View File

@ -106,7 +106,7 @@ async function sendForgetEmail(email: string, password: string): Promise<void> {
const msg = {
to: email,
from: "contact@parisestludique.fr",
subject: "Nouveau mot de passe pour le site de Paris est Ludique",
subject: "Nouveau mot de passe pour le site bénévole de Paris est Ludique",
text: `Voici le nouveau mot de passe : ${password}\nL'ancien fonctionne encore, si tu t'en rappelles.`,
html: `Voici le nouveau mot de passe : <strong>${password}</strong><br />L'ancien fonctionne encore, si tu t'en rappelles.`,
}

View File

@ -1,5 +1,3 @@
import ServiceAccessors from "./accessors"
export class JavGame {
id = 0
@ -50,13 +48,6 @@ export const translationJavGame: { [k in keyof JavGame]: string } = {
bggPhoto: "bggPhoto",
}
const elementName = "JavGame"
export const elementName = "JavGame"
export type JavGameWithoutId = Omit<JavGame, "id">
const serviceAccessors = new ServiceAccessors<JavGameWithoutId, JavGame>(elementName)
export const javGameListGet = serviceAccessors.listGet()
export const javGameGet = serviceAccessors.get()
export const javGameAdd = serviceAccessors.add()
export const javGameSet = serviceAccessors.set()

View File

@ -0,0 +1,9 @@
import ServiceAccessors from "./accessors"
import { elementName, JavGame, JavGameWithoutId } from "./javGames"
const serviceAccessors = new ServiceAccessors<JavGameWithoutId, JavGame>(elementName)
export const javGameListGet = serviceAccessors.listGet()
export const javGameGet = serviceAccessors.get()
export const javGameAdd = serviceAccessors.add()
export const javGameSet = serviceAccessors.set()

View File

@ -1,5 +1,3 @@
import ServiceAccessors from "./accessors"
export class PreVolunteer {
id = 0
@ -26,18 +24,10 @@ export const translationPreVolunteer: { [k in keyof PreVolunteer]: string } = {
comment: "commentaire",
}
const elementName = "PreVolunteer"
export const elementName = "PreVolunteer"
export const emailRegexp =
/^(([^<>()[\].,;:\s@"]+(\.[^<>()[\].,;:\s@"]+)*)|(".+"))@(([^<>()[\].,;:\s@"]+\.)+[^<>()[\].,;:\s@"]{2,})$/i
export const passwordMinLength = 4
export type PreVolunteerWithoutId = Omit<PreVolunteer, "id">
const serviceAccessors = new ServiceAccessors<PreVolunteerWithoutId, PreVolunteer>(elementName)
export const preVolunteerListGet = serviceAccessors.listGet()
export const preVolunteerGet = serviceAccessors.get()
export const preVolunteerAdd = serviceAccessors.add()
export const preVolunteerSet = serviceAccessors.set()
export const preVolunteerCountGet = serviceAccessors.countGet()

View File

@ -0,0 +1,10 @@
import ServiceAccessors from "./accessors"
import { elementName, PreVolunteer, PreVolunteerWithoutId } from "./preVolunteers"
const serviceAccessors = new ServiceAccessors<PreVolunteerWithoutId, PreVolunteer>(elementName)
export const preVolunteerListGet = serviceAccessors.listGet()
export const preVolunteerGet = serviceAccessors.get()
export const preVolunteerAdd = serviceAccessors.add()
export const preVolunteerSet = serviceAccessors.set()
export const preVolunteerCountGet = serviceAccessors.countGet()

View File

@ -1,5 +1,3 @@
import ServiceAccessors from "./accessors"
export class Team {
id = 0
@ -29,11 +27,6 @@ export const translationTeam: { [k in keyof Team]: string } = {
after: "après",
}
const elementName = "Team"
export const elementName = "Team"
export type TeamWithoutId = Omit<Team, "id">
const serviceAccessors = new ServiceAccessors<TeamWithoutId, Team>(elementName)
export const teamListGet = serviceAccessors.listGet()
export const teamGet = serviceAccessors.get()

View File

@ -0,0 +1,7 @@
import ServiceAccessors from "./accessors"
import { Team, TeamWithoutId, elementName } from "./teams"
const serviceAccessors = new ServiceAccessors<TeamWithoutId, Team>(elementName)
export const teamListGet = serviceAccessors.listGet()
export const teamGet = serviceAccessors.get()

View File

@ -1,5 +1,3 @@
import ServiceAccessors from "./accessors"
export class Volunteer {
id = 0
@ -19,6 +17,8 @@ export class Volunteer {
active = ""
discordId = ""
dayWishes: string[] = []
dayWishesComment = ""
@ -56,6 +56,7 @@ export const translationVolunteer: { [k in keyof Volunteer]: string } = {
adult: "majeur",
privileges: "privilege",
active: "actif",
discordId: "discordId",
dayWishes: "enviesJours",
dayWishesComment: "commentaireEnviesJours",
age: "age",
@ -71,7 +72,7 @@ export const translationVolunteer: { [k in keyof Volunteer]: string } = {
acceptsNotifs: "accepteLesNotifs",
}
const elementName = "Volunteer"
export const elementName = "Volunteer"
export const volunteerExample: Volunteer = {
id: 1,
@ -83,6 +84,7 @@ export const volunteerExample: Volunteer = {
adult: 1,
privileges: 0,
active: "inconnu",
discordId: "",
dayWishes: [],
dayWishesComment: "",
age: 33,
@ -104,24 +106,14 @@ export const passwordMinLength = 4
export type VolunteerWithoutId = Omit<Volunteer, "id">
const serviceAccessors = new ServiceAccessors<VolunteerWithoutId, Volunteer>(elementName)
export const volunteerListGet = serviceAccessors.listGet()
export const volunteerGet = serviceAccessors.get()
export const volunteerAdd = serviceAccessors.add()
export const volunteerSet = serviceAccessors.set()
export interface VolunteerLogin {
id: number
jwt: string
}
export const volunteerLogin =
serviceAccessors.customPost<[{ email: string; password: string }]>("Login")
export interface VolunteerForgot {
message: string
}
export const volunteerForgot = serviceAccessors.customPost<[{ email: string }]>("Forgot")
export interface VolunteerNotifs {
id: Volunteer["id"]
@ -132,24 +124,18 @@ export interface VolunteerNotifs {
pushNotifSubscription: Volunteer["pushNotifSubscription"]
acceptsNotifs: Volunteer["acceptsNotifs"]
}
export const volunteerNotifsSet =
serviceAccessors.securedCustomPost<[number, Partial<VolunteerNotifs>]>("NotifsSet")
export interface VolunteerTeamWishes {
id: Volunteer["id"]
teamWishes: Volunteer["teamWishes"]
teamWishesComment: Volunteer["teamWishesComment"]
}
export const volunteerTeamWishesSet =
serviceAccessors.securedCustomPost<[number, Partial<VolunteerTeamWishes>]>("TeamWishesSet")
export interface VolunteerDayWishes {
id: Volunteer["id"]
dayWishes: Volunteer["dayWishes"]
dayWishesComment: Volunteer["dayWishesComment"]
}
export const volunteerDayWishesSet =
serviceAccessors.securedCustomPost<[number, Partial<VolunteerDayWishes>]>("DayWishesSet")
export interface VolunteerParticipationDetails {
id: Volunteer["id"]
@ -157,7 +143,3 @@ export interface VolunteerParticipationDetails {
teeshirtSize: Volunteer["teeshirtSize"]
food: Volunteer["food"]
}
export const volunteerParticipationDetailsSet =
serviceAccessors.securedCustomPost<[number, Partial<VolunteerParticipationDetails>]>(
"ParticipationDetailsSet"
)

View File

@ -0,0 +1,36 @@
import ServiceAccessors from "./accessors"
import {
elementName,
Volunteer,
VolunteerDayWishes,
VolunteerNotifs,
VolunteerParticipationDetails,
VolunteerTeamWishes,
VolunteerWithoutId,
} from "./volunteers"
const serviceAccessors = new ServiceAccessors<VolunteerWithoutId, Volunteer>(elementName)
export const volunteerListGet = serviceAccessors.listGet()
export const volunteerGet = serviceAccessors.get()
export const volunteerAdd = serviceAccessors.add()
export const volunteerSet = serviceAccessors.set()
export const volunteerLogin =
serviceAccessors.customPost<[{ email: string; password: string }]>("Login")
export const volunteerForgot = serviceAccessors.customPost<[{ email: string }]>("Forgot")
export const volunteerNotifsSet =
serviceAccessors.securedCustomPost<[number, Partial<VolunteerNotifs>]>("NotifsSet")
export const volunteerTeamWishesSet =
serviceAccessors.securedCustomPost<[number, Partial<VolunteerTeamWishes>]>("TeamWishesSet")
export const volunteerDayWishesSet =
serviceAccessors.securedCustomPost<[number, Partial<VolunteerDayWishes>]>("DayWishesSet")
export const volunteerParticipationDetailsSet =
serviceAccessors.securedCustomPost<[number, Partial<VolunteerParticipationDetails>]>(
"ParticipationDetailsSet"
)

View File

@ -1,5 +1,3 @@
import ServiceAccessors from "./accessors"
export class Wish {
id = 0
@ -23,13 +21,6 @@ export const translationWish: { [k in keyof Wish]: string } = {
addedDate: "dateAjout",
}
const elementName = "Wish"
export const elementName = "Wish"
export type WishWithoutId = Omit<Wish, "id">
const serviceAccessors = new ServiceAccessors<WishWithoutId, Wish>(elementName)
export const wishListGet = serviceAccessors.listGet()
export const wishGet = serviceAccessors.get()
export const wishAdd = serviceAccessors.add()
export const wishSet = serviceAccessors.set()

View File

@ -0,0 +1,9 @@
import ServiceAccessors from "./accessors"
import { elementName, Wish, WishWithoutId } from "./wishes"
const serviceAccessors = new ServiceAccessors<WishWithoutId, Wish>(elementName)
export const wishListGet = serviceAccessors.listGet()
export const wishGet = serviceAccessors.get()
export const wishAdd = serviceAccessors.add()
export const wishSet = serviceAccessors.set()

View File

@ -1,8 +1,9 @@
import { PayloadAction, createSlice, createEntityAdapter } from "@reduxjs/toolkit"
import { StateRequest, toastError, elementListFetch } from "./utils"
import { JavGame, javGameListGet } from "../services/javGames"
import { JavGame } from "../services/javGames"
import { AppThunk, AppState } from "."
import { javGameListGet } from "../services/javGamesAccessors"
const javGameAdapter = createEntityAdapter<JavGame>()

View File

@ -1,7 +1,8 @@
import { PayloadAction, createSlice, createEntityAdapter } from "@reduxjs/toolkit"
import { StateRequest, elementAddFetch } from "./utils"
import { PreVolunteer, preVolunteerAdd } from "../services/preVolunteers"
import { PreVolunteer } from "../services/preVolunteers"
import { preVolunteerAdd } from "../services/preVolunteersAccessors"
const preVolunteerAdapter = createEntityAdapter<PreVolunteer>()

View File

@ -1,7 +1,7 @@
import { PayloadAction, createSlice } from "@reduxjs/toolkit"
import { StateRequest, toastError, elementValueFetch } from "./utils"
import { preVolunteerCountGet } from "../services/preVolunteers"
import { preVolunteerCountGet } from "../services/preVolunteersAccessors"
import { AppThunk, AppState } from "."
export const initialState: StateRequest & { value?: number } = { readyStatus: "idle" }

View File

@ -1,8 +1,9 @@
import { PayloadAction, createSlice, createEntityAdapter, createSelector } from "@reduxjs/toolkit"
import { StateRequest, toastError, elementListFetch } from "./utils"
import { Team, teamListGet } from "../services/teams"
import { Team } from "../services/teams"
import { AppThunk, AppState, EntitiesRequest } from "."
import { teamListGet } from "../services/teamsAccessors"
const teamAdapter = createEntityAdapter<Team>()

View File

@ -1,8 +1,9 @@
import { PayloadAction, createSlice } from "@reduxjs/toolkit"
import { StateRequest, toastError, elementFetch } from "./utils"
import { Volunteer, volunteerGet } from "../services/volunteers"
import { Volunteer } from "../services/volunteers"
import { AppThunk, AppState } from "."
import { volunteerGet } from "../services/volunteersAccessors"
type StateVolunteer = { entity?: Volunteer } & StateRequest

View File

@ -1,7 +1,8 @@
import { PayloadAction, createSlice, createEntityAdapter } from "@reduxjs/toolkit"
import { StateRequest, toastError, toastSuccess, elementAddFetch } from "./utils"
import { Volunteer, volunteerAdd } from "../services/volunteers"
import { Volunteer } from "../services/volunteers"
import { volunteerAdd } from "../services/volunteersAccessors"
const volunteerAdapter = createEntityAdapter<Volunteer>()

View File

@ -1,8 +1,9 @@
import { PayloadAction, createSlice } from "@reduxjs/toolkit"
import { StateRequest, toastError, elementFetch } from "./utils"
import { VolunteerDayWishes, volunteerDayWishesSet } from "../services/volunteers"
import { VolunteerDayWishes } from "../services/volunteers"
import { AppThunk, AppState } from "."
import { volunteerDayWishesSet } from "../services/volunteersAccessors"
type StateVolunteerDayWishesSet = { entity?: VolunteerDayWishes } & StateRequest

View File

@ -1,7 +1,8 @@
import { PayloadAction, createSlice } from "@reduxjs/toolkit"
import { StateRequest, elementFetch } from "./utils"
import { VolunteerForgot, volunteerForgot } from "../services/volunteers"
import { VolunteerForgot } from "../services/volunteers"
import { volunteerForgot } from "../services/volunteersAccessors"
type StateVolunteer = { entity?: VolunteerForgot } & StateRequest

View File

@ -1,8 +1,9 @@
import { PayloadAction, createSlice, createEntityAdapter } from "@reduxjs/toolkit"
import { StateRequest, toastError, elementListFetch } from "./utils"
import { Volunteer, volunteerListGet } from "../services/volunteers"
import { Volunteer } from "../services/volunteers"
import { AppThunk, AppState } from "."
import { volunteerListGet } from "../services/volunteersAccessors"
const volunteerAdapter = createEntityAdapter<Volunteer>()

View File

@ -1,11 +1,12 @@
import { PayloadAction, createSlice } from "@reduxjs/toolkit"
import { StateRequest, elementFetch } from "./utils"
import { VolunteerLogin, volunteerLogin } from "../services/volunteers"
import { VolunteerLogin } from "../services/volunteers"
import { setJWT } from "../services/auth"
import { AppDispatch } from "."
import { setCurrentUser } from "./auth"
import { fetchVolunteerNotifsSet } from "./volunteerNotifsSet"
import { volunteerLogin } from "../services/volunteersAccessors"
type StateVolunteer = { entity?: VolunteerLogin } & StateRequest

View File

@ -1,8 +1,9 @@
import { PayloadAction, createSlice } from "@reduxjs/toolkit"
import { StateRequest, toastError, elementFetch } from "./utils"
import { VolunteerNotifs, volunteerNotifsSet } from "../services/volunteers"
import { VolunteerNotifs } from "../services/volunteers"
import { AppThunk, AppState } from "."
import { volunteerNotifsSet } from "../services/volunteersAccessors"
type StateVolunteerNotifsSet = { entity?: VolunteerNotifs } & StateRequest

View File

@ -1,11 +1,9 @@
import { PayloadAction, createSlice } from "@reduxjs/toolkit"
import { StateRequest, toastError, elementFetch } from "./utils"
import {
VolunteerParticipationDetails,
volunteerParticipationDetailsSet,
} from "../services/volunteers"
import { VolunteerParticipationDetails } from "../services/volunteers"
import { AppThunk, AppState } from "."
import { volunteerParticipationDetailsSet } from "../services/volunteersAccessors"
type StateVolunteerParticipationDetailsSet = {
entity?: VolunteerParticipationDetails

View File

@ -1,7 +1,8 @@
import { PayloadAction, createSlice, createEntityAdapter } from "@reduxjs/toolkit"
import { StateRequest, toastError, toastSuccess, elementSet } from "./utils"
import { Volunteer, volunteerSet } from "../services/volunteers"
import { Volunteer } from "../services/volunteers"
import { volunteerSet } from "../services/volunteersAccessors"
const volunteerAdapter = createEntityAdapter<Volunteer>()

View File

@ -1,8 +1,9 @@
import { PayloadAction, createSlice } from "@reduxjs/toolkit"
import { StateRequest, toastError, elementFetch } from "./utils"
import { VolunteerTeamWishes, volunteerTeamWishesSet } from "../services/volunteers"
import { VolunteerTeamWishes } from "../services/volunteers"
import { AppThunk, AppState } from "."
import { volunteerTeamWishesSet } from "../services/volunteersAccessors"
type StateVolunteerTeamWishesSet = { entity?: VolunteerTeamWishes } & StateRequest

View File

@ -1,7 +1,8 @@
import { PayloadAction, createSlice, createEntityAdapter } from "@reduxjs/toolkit"
import { StateRequest, toastError, toastSuccess, elementAddFetch } from "./utils"
import { Wish, wishAdd } from "../services/wishes"
import { Wish } from "../services/wishes"
import { wishAdd } from "../services/wishesAccessors"
const wishAdapter = createEntityAdapter<Wish>()

View File

@ -1,8 +1,9 @@
import { PayloadAction, createSlice, createEntityAdapter } from "@reduxjs/toolkit"
import { StateRequest, toastError, elementListFetch } from "./utils"
import { Wish, wishListGet } from "../services/wishes"
import { Wish } from "../services/wishes"
import { AppThunk, AppState } from "."
import { wishListGet } from "../services/wishesAccessors"
const wishAdapter = createEntityAdapter<Wish>()

View File

@ -45,6 +45,22 @@ export function trim(src: string): string {
return typeof src !== "string" ? "" : src.replace(/^\s*/, "").replace(/\s*$/, "")
}
export function canonicalFirstname(firstname: string): string {
return trim(firstname)
.toLowerCase()
.replace(/(?<=^|[\s'-])([a-zA-Z]|[à-ú]|[À-Ú])/gi, (s) => s.toUpperCase())
.replace(/\b(de|d'|du|le|la)\b/gi, (s) => s.toLowerCase())
.replace(/\b(d'|l')/gi, (s) => s.toLowerCase())
}
export function canonicalLastname(lastname: string): string {
return trim(lastname)
.toLowerCase()
.replace(/(?<=^|[\s'-])([a-zA-Z]|[à-ú]|[À-Ú])/gi, (s) => s.toUpperCase())
.replace(/\b(de|d'|du|le|la)\b/gi, (s) => s.toLowerCase())
.replace(/\b(d'|l')/gi, (s) => s.toLowerCase())
}
// eslint-disable-next-line @typescript-eslint/ban-types
export function doCleanVolunteer<
Element extends { firstname: string; lastname: string; email: string; mobile: string }
@ -55,19 +71,9 @@ export function doCleanVolunteer<
vol.mobile = canonicalMobile(vol.mobile)
}
vol.firstname = trim(vol.firstname)
vol.firstname = vol.firstname
.toLowerCase()
.replace(/(?<=^|[\s'-])([a-zA-Z]|[à-ú]|[À-Ú])/gi, (s) => s.toUpperCase())
.replace(/\b(de|d'|du|le|la)\b/gi, (s) => s.toLowerCase())
.replace(/\b(d'|l')/gi, (s) => s.toLowerCase())
vol.firstname = canonicalFirstname(vol.firstname)
vol.lastname = trim(vol.lastname)
vol.lastname = vol.lastname
.toLowerCase()
.replace(/(?<=^|[\s'-])([a-zA-Z]|[à-ú]|[À-Ú])/gi, (s) => s.toUpperCase())
.replace(/\b(de|d'|du|le|la)\b/gi, (s) => s.toLowerCase())
.replace(/\b(d'|l')/gi, (s) => s.toLowerCase())
vol.lastname = canonicalLastname(vol.lastname)
vol.email = canonicalEmail(vol.email)
}