65 lines
1.8 KiB
JavaScript
65 lines
1.8 KiB
JavaScript
|
import { Directus, EmptyParamError } from '@directus/sdk';
|
||
|
|
||
|
const directus = new Directus('https://formulaire.p4pillon.org');
|
||
|
const mspInfo = directus.items('MSP_INFO');
|
||
|
|
||
|
export async function login() {
|
||
|
// AUTHENTICATION
|
||
|
|
||
|
let authenticated = false;
|
||
|
|
||
|
// Try to authenticate with token if exists
|
||
|
await directus.auth
|
||
|
.refresh()
|
||
|
.then(() => {
|
||
|
authenticated = true;
|
||
|
})
|
||
|
.catch(() => {});
|
||
|
|
||
|
// Let's login in case we don't have token or it is invalid / expired
|
||
|
while (!authenticated) {
|
||
|
const email = window.prompt('Email:');
|
||
|
const password = window.prompt('Password:');
|
||
|
|
||
|
await directus.auth
|
||
|
.login({ email, password })
|
||
|
.then(() => {
|
||
|
authenticated = true;
|
||
|
})
|
||
|
.catch(() => {
|
||
|
window.alert('Invalid credentials');
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export async function createOrUpdateMspInfo(formData) {
|
||
|
try {
|
||
|
return await mspInfo.createOne({
|
||
|
nofinesset: formData.get("nofinesset"),
|
||
|
nom: formData.get("nom"),
|
||
|
prenom_leader: formData.get("prenom_leader"),
|
||
|
nom_leader: formData.get("nom_leader"),
|
||
|
avec_sante: formData.get("avec_sante"),
|
||
|
accord_conventionnel_interprofessionnel: formData.get("accord_conventionnel_interprofessionnel")
|
||
|
})
|
||
|
} catch (error) {
|
||
|
console.log(error)
|
||
|
}
|
||
|
const nofinesset = formData.get("nofinesset")
|
||
|
console.log(nofinesset)
|
||
|
try {
|
||
|
const msp = await mspInfo.readOne(nofinesset)
|
||
|
return await mspInfo.updateOne(nofinesset, formData)
|
||
|
} catch (error) {
|
||
|
if (error instanceof EmptyParamError) {
|
||
|
console.error("Le nofinesset est obligatoire.")
|
||
|
throw error
|
||
|
}
|
||
|
if (error.message == "You don't have permission to access this.") {
|
||
|
console.log("createOne")
|
||
|
console.log(formData)
|
||
|
return await mspInfo.createOne(formData)
|
||
|
}
|
||
|
}
|
||
|
}
|