feat(constructeur) : add ConstructeurLinkEntry type, useConstructeurLinks composable, and ConstructeurLinksTable component
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
102
app/composables/useConstructeurLinks.ts
Normal file
102
app/composables/useConstructeurLinks.ts
Normal file
@@ -0,0 +1,102 @@
|
||||
import { useApi } from '~/composables/useApi'
|
||||
import type { ConstructeurLinkEntry } from '~/shared/constructeurUtils'
|
||||
|
||||
type EntityType = 'machine' | 'piece' | 'composant' | 'product'
|
||||
|
||||
const ENDPOINTS: Record<EntityType, string> = {
|
||||
machine: '/machine_constructeur_links',
|
||||
piece: '/piece_constructeur_links',
|
||||
composant: '/composant_constructeur_links',
|
||||
product: '/product_constructeur_links',
|
||||
}
|
||||
|
||||
const ENTITY_KEYS: Record<EntityType, string> = {
|
||||
machine: 'machine',
|
||||
piece: 'piece',
|
||||
composant: 'composant',
|
||||
product: 'product',
|
||||
}
|
||||
|
||||
const ENTITY_PLURALS: Record<EntityType, string> = {
|
||||
machine: 'machines',
|
||||
piece: 'pieces',
|
||||
composant: 'composants',
|
||||
product: 'products',
|
||||
}
|
||||
|
||||
export function useConstructeurLinks() {
|
||||
const { get, post, patch, del } = useApi()
|
||||
|
||||
const fetchLinks = async (
|
||||
entityType: EntityType,
|
||||
entityId: string,
|
||||
): Promise<ConstructeurLinkEntry[]> => {
|
||||
const endpoint = ENDPOINTS[entityType]
|
||||
const key = ENTITY_KEYS[entityType]
|
||||
const plural = ENTITY_PLURALS[entityType]
|
||||
const result = await get(`${endpoint}?${key}=/api/${plural}/${entityId}`)
|
||||
if (!result.success || !result.data) return []
|
||||
|
||||
const data = result.data as Record<string, any>
|
||||
const members = data['hydra:member'] ?? (Array.isArray(data) ? data : [])
|
||||
if (!Array.isArray(members)) return []
|
||||
|
||||
return members.map((link: any) => ({
|
||||
linkId: link.id ?? (typeof link['@id'] === 'string' ? link['@id'].split('/').pop() : undefined),
|
||||
constructeurId: typeof link.constructeur === 'string'
|
||||
? link.constructeur.split('/').pop()!
|
||||
: link.constructeur?.id ?? '',
|
||||
constructeur: typeof link.constructeur === 'object' ? link.constructeur : null,
|
||||
supplierReference: link.supplierReference ?? null,
|
||||
}))
|
||||
}
|
||||
|
||||
const syncLinks = async (
|
||||
entityType: EntityType,
|
||||
entityId: string,
|
||||
originalLinks: ConstructeurLinkEntry[],
|
||||
formLinks: ConstructeurLinkEntry[],
|
||||
): Promise<void> => {
|
||||
const endpoint = ENDPOINTS[entityType]
|
||||
const key = ENTITY_KEYS[entityType]
|
||||
const plural = ENTITY_PLURALS[entityType]
|
||||
const entityIri = `/api/${plural}/${entityId}`
|
||||
|
||||
const originalMap = new Map(originalLinks.map(l => [l.constructeurId, l]))
|
||||
const formMap = new Map(formLinks.map(l => [l.constructeurId, l]))
|
||||
|
||||
const promises: Promise<any>[] = []
|
||||
|
||||
// Delete removed links
|
||||
for (const [cId, orig] of originalMap) {
|
||||
if (!formMap.has(cId) && orig.linkId) {
|
||||
promises.push(del(`${endpoint}/${orig.linkId}`))
|
||||
}
|
||||
}
|
||||
|
||||
// Create new links
|
||||
for (const [cId, form] of formMap) {
|
||||
if (!originalMap.has(cId)) {
|
||||
promises.push(post(endpoint, {
|
||||
[key]: entityIri,
|
||||
constructeur: `/api/constructeurs/${cId}`,
|
||||
supplierReference: form.supplierReference || null,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
// Patch modified supplierReference
|
||||
for (const [cId, form] of formMap) {
|
||||
const orig = originalMap.get(cId)
|
||||
if (orig?.linkId && (orig.supplierReference ?? null) !== (form.supplierReference ?? null)) {
|
||||
promises.push(patch(`${endpoint}/${orig.linkId}`, {
|
||||
supplierReference: form.supplierReference || null,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
await Promise.allSettled(promises)
|
||||
}
|
||||
|
||||
return { fetchLinks, syncLinks }
|
||||
}
|
||||
Reference in New Issue
Block a user