mirror of
https://github.com/Paris-est-Ludique/intranet.git
synced 2025-09-11 13:56:29 +02:00
More robust gSheet API connection
This commit is contained in:
@@ -9,7 +9,7 @@ import { GoogleSpreadsheet, GoogleSpreadsheetWorksheet } from "google-spreadshee
|
||||
const CRED_PATH = path.resolve(process.cwd(), "access/gsheets.json")
|
||||
|
||||
const REMOTE_UPDATE_DELAY = 40000
|
||||
const DELAY_AFTER_QUERY = 1000
|
||||
const DELAY_AFTER_QUERY = 2000
|
||||
|
||||
export type ElementWithId<ElementNoId> = { id: number } & ElementNoId
|
||||
|
||||
@@ -77,7 +77,7 @@ export class Sheet<
|
||||
readonly translation: { [k in keyof Element]: string }
|
||||
) {
|
||||
this.invertedTranslation = _.invert(this.translation)
|
||||
this.sheetName = sheetNames[name]
|
||||
this.sheetName = sheetNames[name] || name
|
||||
this.frenchSpecimen = _.mapValues(
|
||||
_.invert(translation),
|
||||
(englishProp: string) => (specimen as any)[englishProp]
|
||||
@@ -150,20 +150,28 @@ export class Sheet<
|
||||
}
|
||||
|
||||
dbSave(): void {
|
||||
this.modifiedSinceSave = false
|
||||
this.saveTimestamp = +new Date()
|
||||
|
||||
this.dbSaveAsync()
|
||||
try {
|
||||
this.dbSaveAsync()
|
||||
this.modifiedSinceSave = false
|
||||
} catch (e) {
|
||||
console.error("Error in dbSave: ", e)
|
||||
}
|
||||
}
|
||||
|
||||
dbLoad(): void {
|
||||
this.toRunAfterLoad = []
|
||||
this.dbLoadAsync().then(() => {
|
||||
if (this.toRunAfterLoad) {
|
||||
this.toRunAfterLoad.map((func) => func())
|
||||
this.toRunAfterLoad = undefined
|
||||
}
|
||||
})
|
||||
try {
|
||||
this.toRunAfterLoad = []
|
||||
this.dbLoadAsync().then(() => {
|
||||
if (this.toRunAfterLoad) {
|
||||
this.toRunAfterLoad.map((func) => func())
|
||||
this.toRunAfterLoad = undefined
|
||||
}
|
||||
})
|
||||
} catch (e) {
|
||||
console.error("Error in dbLoad: ", e)
|
||||
}
|
||||
}
|
||||
|
||||
private async dbSaveAsync(): Promise<void> {
|
||||
@@ -174,6 +182,7 @@ export class Sheet<
|
||||
|
||||
// Load sheet into an array of objects
|
||||
const rows = await sheet.getRows()
|
||||
await delayDBAccess()
|
||||
if (!rows[0]) {
|
||||
throw new Error(`No column types defined in sheet ${this.name}`)
|
||||
}
|
||||
@@ -234,7 +243,6 @@ export class Sheet<
|
||||
|
||||
private async dbLoadAsync(): Promise<void> {
|
||||
type StringifiedElement = Record<keyof Element, string>
|
||||
|
||||
const sheet = await this.getGSheet()
|
||||
|
||||
// Load sheet into an array of objects
|
||||
|
@@ -1,5 +1,5 @@
|
||||
export function canonicalEmail(email: string): string {
|
||||
email = email.replace(/^\s+|\s+$/g, "")
|
||||
email = email.replace(/^\s+|\s+$/g, "").replace(/\+[^@]+/, "") // Remove +pel in pierre+pel@gmail.com
|
||||
if (/@gmail.com$/.test(email)) {
|
||||
let domain = email.replace(/^.*@/, "")
|
||||
domain = domain.replace(/^googlemail%.com$/, "gmail.com")
|
||||
@@ -28,7 +28,7 @@ export function canonicalMobile(mobile: string): string {
|
||||
if (!validMobile(mobile)) {
|
||||
return ""
|
||||
}
|
||||
let clean = trim(mobile).replace(/[-0-9. ()+]$/g, "")
|
||||
let clean = trim(mobile).replace(/[-. ()+]/g, "")
|
||||
if (clean.length === 11) {
|
||||
clean = clean.replace(/^33/, "0")
|
||||
}
|
||||
@@ -44,3 +44,30 @@ export function canonicalMobile(mobile: string): string {
|
||||
export function trim(src: string): string {
|
||||
return typeof src !== "string" ? "" : src.replace(/^\s*/, "").replace(/\s*$/, "")
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
export function doCleanVolunteer<
|
||||
Element extends { firstname: string; lastname: string; email: string; mobile: string }
|
||||
>(vol: Element): void {
|
||||
if (!validMobile(vol.mobile)) {
|
||||
vol.mobile = ""
|
||||
} else {
|
||||
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.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.email = canonicalEmail(vol.email)
|
||||
}
|
||||
|
Reference in New Issue
Block a user