diff --git a/CHANGELOG.md b/CHANGELOG.md index 70c1510..daaca58 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,7 @@ Ajouter dans le fichier .env du frontend * fix layout admin * Creation page admin listing bovins * Creation page admin ajout/modification bovins +* [#331] Mettre à jour l'entité Shipment et bovin_shipment ### Changed ### Fixed diff --git a/frontend/components/shipment/shipment-form.vue b/frontend/components/shipment/shipment-form.vue index 0700b9d..a19076e 100644 --- a/frontend/components/shipment/shipment-form.vue +++ b/frontend/components/shipment/shipment-form.vue @@ -152,15 +152,9 @@ import type {ShipmentFormData} from '~/services/dto/shipment-data' import {SUPPLIER_CODE} from "~/utils/constants" import {useAuthStore} from '~/stores/auth' import {useShipmentStore} from '~/stores/shipment' -import { computed, reactive, ref, watch, onMounted } from 'vue' +import {computed, reactive, ref, watch, onMounted} from 'vue' import type {ShipmentTypeData} from "~/services/dto/shipment-type-data"; import {getShipmentTypeList} from "~/services/shipment-type"; -import { - createShipmentBovine, - deleteShipmentBovine, - getBovinShipmentList, - updateShipmentBovine -} from "~/services/bovin-shipment"; const users = ref([]) const customers = ref([]) @@ -332,23 +326,15 @@ watch( form.carrierId = shipment?.carrier?.id ? String(shipment.carrier.id) : '' form.driverId = shipment?.driver?.id ? String(shipment.driver.id) : '' form.vehicleId = shipment?.vehicle?.id ? String(shipment.vehicle.id) : '' - if (!shipment || !shipment.bovinShipments) { - selectedShipmentTypeId.value = '' - shipmentQuantity.value = 0 - } else { - const selectedEntry = shipment.bovinShipments.find((entry) => { - const typeId = entry.shipmentType?.id - return Boolean(typeId) && Number(entry.nbBovinSend ?? 0) > 0 - }) ?? shipment.bovinShipments.find((entry) => Boolean(entry.shipmentType?.id)) - if (!selectedEntry?.shipmentType?.id) { - selectedShipmentTypeId.value = '' - shipmentQuantity.value = 0 - } else { - selectedShipmentTypeId.value = String(selectedEntry.shipmentType.id) - shipmentQuantity.value = selectedEntry.nbBovinSend ?? 0 - } - } + + selectedShipmentTypeId.value = shipment?.shipmentType?.id + ? String(shipment.shipmentType.id) + : '' + + shipmentQuantity.value = shipment?.nbBovinSend ?? 0 + + isHydrating.value = false }, {immediate: true} @@ -474,68 +460,7 @@ watch( } } ) -const buildDesiredBovinShipments = () => { - const typeId = Number(selectedShipmentTypeId.value) - if (!Number.isFinite(typeId)) { - return [] - } - const type = bovineShipment.value.find((entry) => entry.id === typeId) - if (!type) { - return [] - } - const raw = shipmentQuantity.value - const quantity = raw === null || raw === undefined ? 0 : Number(raw) - const normalizedQuantity = Number.isFinite(quantity) ? Math.max(0, Math.trunc(quantity)) : 0 - if (normalizedQuantity <= 0) { - return [] - } - return [{type, quantity: normalizedQuantity}] -} -const syncBovinShipments = async ( - shipmentId: number, - existing: Array<{ id?: number; nbBovinSend: number | null; shipmentType?: unknown }> = [] -) => { - const shipmentIri = `/api/shipments/${shipmentId}` - const desired = buildDesiredBovinShipments() - const desiredByTypeId = new Map() - for (const entry of desired) { - desiredByTypeId.set(entry.type.id, entry.quantity) - } - for (const entry of existing) { - if (!entry.id) { - continue - } - const rawType = entry.shipmentType - let typeId: number | null = null - if (rawType && typeof rawType === 'object' && 'id' in rawType) { - typeId = Number((rawType as { id: number }).id) - } else if (typeof rawType === 'string') { - const match = rawType.match(/\/shipment_types\/(\\d+)$/) - typeId = match ? Number(match[1]) : null - } - if (!typeId) { - continue - } - const desiredQuantity = desiredByTypeId.get(typeId) - if (!desiredQuantity) { - await deleteShipmentBovine(entry.id) - continue - } - if (entry.nbBovinSend !== desiredQuantity) { - await updateShipmentBovine(entry.id, {nbBovinSend: desiredQuantity}) - } - desiredByTypeId.delete(typeId) - } - - for (const [typeId, quantity] of desiredByTypeId.entries()) { - await createShipmentBovine({ - shipment: shipmentIri, - shipmentType: `/api/shipment_types/${typeId}`, - nbBovinSend: quantity - }) - } -} const buildPayload = () => { const normalizedLicensePlate = form.licencePlate.trim() const normalizedShipmentDate = form.shipmentDate.trim() @@ -563,6 +488,14 @@ const buildPayload = () => { const addressIri = normalizedAddressId ? `/api/addresses/${normalizedAddressId}` : null + const normalizedShipmentTypeId = selectedShipmentTypeId.value.trim() + const shipmentTypeIri = normalizedShipmentTypeId + ? `/api/shipment_types/${normalizedShipmentTypeId}` + : null + + const rawQuantity = Number(shipmentQuantity.value ?? 0) + const normalizedQuantity = Number.isFinite(rawQuantity) ? Math.max(0, + Math.trunc(rawQuantity)) : 0 return { licencePlate: normalizedLicensePlate, @@ -572,20 +505,19 @@ const buildPayload = () => { carrier: carrierIri, driver: driverIri, user: userIri, - address: addressIri + address: addressIri, + shipmentType: shipmentTypeIri, + nbBovinSend: normalizedQuantity, } } const saveDraft = async () => { const payload = buildPayload() if (!shipmentStore.current) { - const created = await shipmentStore.createShipment({ + await shipmentStore.createShipment({ currentStep: 0, ...payload }) - if (created) { - await syncBovinShipments(created.id, []) - } return } @@ -593,10 +525,6 @@ const saveDraft = async () => { currentStep: shipmentStore.current.currentStep, ...payload }) - await syncBovinShipments( - shipmentStore.current.id, - shipmentStore.current?.bovinShipments ?? [] - ) } defineExpose({saveDraft}) @@ -610,7 +538,6 @@ const validate = async () => { }) if (created) { await shipmentStore.loadShipment(created.id) - await syncBovinShipments(created.id, shipmentStore.current?.bovinShipments ?? []) await router.push(`/shipment/${created.id}`) } return @@ -621,6 +548,5 @@ const validate = async () => { ...payload }) await shipmentStore.loadShipment(shipmentStore.current.id) - await syncBovinShipments(shipmentStore.current.id, shipmentStore.current?.bovinShipments ?? []) } diff --git a/frontend/components/shipment/shipment-loading.vue b/frontend/components/shipment/shipment-loading.vue index 802e1b7..bdd8595 100644 --- a/frontend/components/shipment/shipment-loading.vue +++ b/frontend/components/shipment/shipment-loading.vue @@ -1,6 +1,6 @@