Integrates the Dispatch PWA (Vue/Quasar) into the gigafibre-fsm monorepo. Full git history accessible via `git log -- apps/dispatch/`. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
75 lines
2.7 KiB
JavaScript
75 lines
2.7 KiB
JavaScript
// ── Contractor API — inscrit un sous-traitant dans ERPNext ────────────────────
|
|
import { BASE_URL } from 'src/config/erpnext'
|
|
import { getCSRF } from './auth'
|
|
|
|
function buildNotes (data) {
|
|
const services = data.services
|
|
.map(s => ` • ${s.label}: ${s.rate}$ / ${s.rateType === 'hourly' ? 'heure' : 'forfait'}`)
|
|
.join('\n')
|
|
|
|
const days = data.availability.days
|
|
.map(d => ({ mon: 'Lun', tue: 'Mar', wed: 'Mer', thu: 'Jeu', fri: 'Ven', sat: 'Sam', sun: 'Dim' }[d]))
|
|
.join(', ')
|
|
|
|
return [
|
|
`SERVICES OFFERTS:`,
|
|
services,
|
|
``,
|
|
`ZONE: ${data.availability.city} — rayon ${data.availability.radius}`,
|
|
`DISPONIBILITÉ: ${days}`,
|
|
data.availability.urgent ? 'Disponible pour urgences' : '',
|
|
``,
|
|
data.profile.license ? `Licence/RBQ: ${data.profile.license}` : '',
|
|
data.profile.company ? `Entreprise: ${data.profile.company}` : '',
|
|
].filter(s => s !== undefined).join('\n')
|
|
}
|
|
|
|
function localRef () {
|
|
return 'TECH-' + Date.now().toString(36).toUpperCase().slice(-6)
|
|
}
|
|
|
|
export async function registerContractor (data) {
|
|
const csrf = await getCSRF().catch(() => '')
|
|
|
|
// Try ERPNext Supplier (standard ERPNext)
|
|
try {
|
|
const supplierName = data.profile.company
|
|
|| `${data.profile.firstname} ${data.profile.lastname}`
|
|
|
|
const r = await fetch(`${BASE_URL}/api/resource/Supplier`, {
|
|
method: 'POST',
|
|
credentials: 'include',
|
|
headers: { 'Content-Type': 'application/json', 'X-Frappe-CSRF-Token': csrf },
|
|
body: JSON.stringify({
|
|
supplier_name: supplierName,
|
|
supplier_type: 'Individual',
|
|
supplier_group: 'Services',
|
|
}),
|
|
})
|
|
const body = await r.json().catch(() => ({}))
|
|
if (r.ok && body.data?.name) {
|
|
// Try to create a Contact linked to the supplier
|
|
await fetch(`${BASE_URL}/api/resource/Contact`, {
|
|
method: 'POST',
|
|
credentials: 'include',
|
|
headers: { 'Content-Type': 'application/json', 'X-Frappe-CSRF-Token': csrf },
|
|
body: JSON.stringify({
|
|
first_name: data.profile.firstname,
|
|
last_name: data.profile.lastname,
|
|
email_ids: [{ email_id: data.profile.email, is_primary: 1 }],
|
|
phone_nos: [{ phone: data.profile.phone, is_primary_phone: 1 }],
|
|
links: [{ link_doctype: 'Supplier', link_name: body.data.name }],
|
|
}),
|
|
}).catch(() => {})
|
|
return body.data.name
|
|
}
|
|
} catch (_) { /* fall through */ }
|
|
|
|
// Fallback: localStorage + generated ref
|
|
const ref = localRef()
|
|
const list = JSON.parse(localStorage.getItem('dispatch_contractors') || '[]')
|
|
list.push({ ref, ...data, created: new Date().toISOString(), status: 'pending_review' })
|
|
localStorage.setItem('dispatch_contractors', JSON.stringify(list))
|
|
return ref
|
|
}
|