Skip to content

Commit

Permalink
Corrige le payload envoyé dans les inventaires de retour d'événement …
Browse files Browse the repository at this point in the history
…et de réservation
  • Loading branch information
Donov4n committed Jul 28, 2023
1 parent a67059e commit 482a352
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const InventoryItemUnitsItem = defineComponent({
},

state() {
if (this.isLost) {
if (this.isLost || this.isStateLocked) {
return this.unit.state;
}
return this.values?.state ?? this.unit.state;
Expand Down Expand Up @@ -76,13 +76,19 @@ const InventoryItemUnitsItem = defineComponent({
},
methods: {
handleFoundChange(isFound) {
const isBroken = isFound ? this.isBroken : false;
this.$emit('change', this.id, { isLost: !isFound, isBroken, state: this.state });
this.$emit('change', this.id, {
isLost: !isFound,
isBroken: isFound ? this.isBroken : false,
...(!this.isStateLocked ? { state: this.state } : {}),
});
},

handleBrokenChange(isBroken) {
const isLost = isBroken ? false : this.isLost;
this.$emit('change', this.id, { isLost, isBroken, state: this.state });
this.$emit('change', this.id, {
isBroken,
isLost: isBroken ? false : this.isLost,
...(!this.isStateLocked ? { state: this.state } : {}),
});
},

handleStateChange(state) {
Expand Down
27 changes: 14 additions & 13 deletions client/src/themes/default/components/Inventory/Item/Units/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,23 @@ const InventoryItemUnits = defineComponent({
},
},
methods: {
getValues(unitId) {
const originalValues = this.awaitedUnits.find(({ id }) => id === unitId);
invariant(originalValues, `L'unité demandée ne fait pas partie du matériel de l'inventaire.`);
getValues(id) {
const originalValues = this.awaitedUnits.find(({ id: _id }) => _id === id);
invariant(originalValues, 'The requested unit is not part of the inventory.');
const originalState = originalValues.state;

const existingValues = this.units.find(({ id }) => id === unitId);

const existingValues = this.units.find(({ id: _id }) => _id === id);
const isBroken = existingValues?.isBroken ?? false;
const isLost = isBroken ? false : (existingValues?.isLost ?? true);
const state = isLost ? originalState : (existingValues?.state ?? originalState);

return { id: unitId, state, isLost, isBroken };
const state = !isLost && !this.isStateLocked
? (existingValues?.state ?? originalState)
: originalState;

return {
// eslint-disable-next-line unicorn/no-useless-spread
...{ id, isLost, isBroken },
...(!this.isStateLocked ? { state } : {}),
};
},

handleChange(id, values) {
Expand All @@ -67,11 +72,7 @@ const InventoryItemUnits = defineComponent({
broken += !values.isBroken ? -1 : 1;
}

// - Si les états des unités sont verrouillés, on force la précédente valeur.
// eslint-disable-next-line prefer-destructuring
const state = (!this.isStateLocked ? values : prevValues).state;

return { id: unit.id, ...values, state };
return { id: unit.id, ...values };
});

this.$emit('change', { actual, broken, units });
Expand Down
47 changes: 39 additions & 8 deletions client/src/themes/default/components/Inventory/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import observeBarcodeScan from '@/utils/observeBarcodeScan';
import { groupByCategories /* groupByParks */ } from './_utils';
import Item from './Item';

import type { PropType } from '@vue/composition-api';
import type { AwaitedMaterial, AwaitedMaterialGroup } from './_utils';
import type { MaterialUnit } from '@/stores/api/material-units';

Expand Down Expand Up @@ -99,15 +100,31 @@ type Props = {
};

// @vue/component
const Inventory = defineComponent<Required<Props>>({
const Inventory = defineComponent({
name: 'Inventory',
props: {
materials: { type: Array, required: true },
inventory: { type: Array, required: true },
errors: { type: Array, default: () => [] },
locked: { type: [Boolean, Array], default: false },
strict: { type: Boolean, default: false },
materials: {
type: Array as PropType<Props['materials']>,
required: true,
},
inventory: {
type: Array as PropType<Props['inventory']>,
required: true,
},
errors: {
type: Array as PropType<Required<Props>['errors']>,
default: () => [],
},
locked: {
type: [Boolean, Array] as PropType<Required<Props>['locked']>,
default: false,
},
strict: {
type: Boolean as PropType<Required<Props>['strict']>,
default: false,
},
displayGroup: {
type: String as PropType<Required<Props>['displayGroup']>,
default: DisplayGroup.CATEGORIES,
validator: (displayGroup: unknown): boolean => (
typeof displayGroup === 'string' &&
Expand All @@ -116,6 +133,13 @@ const Inventory = defineComponent<Required<Props>>({
},
},
computed: {
isStateLocked() {
if (Array.isArray(this.locked)) {
return this.locked.includes('unit-state');
}
return this.locked;
},

list(): AwaitedMaterialGroup[] {
switch (this.displayGroup) {
case DisplayGroup.CATEGORIES: {
Expand Down Expand Up @@ -222,8 +246,15 @@ const Inventory = defineComponent<Required<Props>>({

const isBroken = inventoriedUnit?.isBroken ?? false;
const isLost = isBroken ? false : (inventoriedUnit?.isLost ?? true);
const state = isLost ? originalState : (inventoriedUnit?.state ?? originalState);
return { id, isBroken, isLost, state };
const state = !isLost && !this.isStateLocked
? (inventoriedUnit?.state ?? originalState)
: originalState;

return {
// eslint-disable-next-line unicorn/no-useless-spread
...{ id, isLost, isBroken },
...(!this.isStateLocked ? { state } : {}),
};
},
);
}
Expand Down
3 changes: 1 addition & 2 deletions client/src/themes/default/pages/EventReturn/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,10 @@ const EventReturn = defineComponent({
);

this.inventory = event.materials.map(
({ id, is_unitary: isUnitary, pivot }) => ({
({ id, pivot }) => ({
id,
actual: getActualQuantity(pivot),
broken: pivot.quantity_returned_broken ?? 0,
is_unitary: isUnitary,
units: pivot.units_with_return.map((unit) => ({
id: unit.id,
isLost: getIsLostUnit(unit),
Expand Down

0 comments on commit 482a352

Please sign in to comment.