From cb0d1c55436de6c3b215ed44e30558f35495325b Mon Sep 17 00:00:00 2001 From: Francis Pion Date: Tue, 16 Apr 2024 15:24:51 -0400 Subject: [PATCH] Fixed store/article save bug. (#136) --- frontend/src/api/products.ts | 3 ++ frontend/src/i18n/en/products.en.json | 4 +++ frontend/src/i18n/fr/products.fr.json | 4 +++ frontend/src/views/products/ProductEdit.vue | 32 +++++++++++++++++++-- 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/frontend/src/api/products.ts b/frontend/src/api/products.ts index e1b40d13..d9fa4238 100644 --- a/frontend/src/api/products.ts +++ b/frontend/src/api/products.ts @@ -14,6 +14,9 @@ export async function deleteProduct(id: string): Promise { export async function readProduct(id: string): Promise { return (await get(`/products/${id}`)).data; } +export async function readProductByArticle(storeId: string, articleId: string): Promise { + return (await get(`/stores/${storeId}/products/${articleId}`)).data; +} export async function searchProducts(payload: SearchProductsPayload): Promise> { const params: string[] = []; diff --git a/frontend/src/i18n/en/products.en.json b/frontend/src/i18n/en/products.en.json index eb7b9813..b5a90a6a 100644 --- a/frontend/src/i18n/en/products.en.json +++ b/frontend/src/i18n/en/products.en.json @@ -1,4 +1,8 @@ { + "alreadyExists": { + "help": "Please select a different article.", + "lead": "The article is already used!" + }, "created": "The product has been created.", "delete": { "confirm": "Do you really want to delete the following product?", diff --git a/frontend/src/i18n/fr/products.fr.json b/frontend/src/i18n/fr/products.fr.json index 6969bb01..12699668 100644 --- a/frontend/src/i18n/fr/products.fr.json +++ b/frontend/src/i18n/fr/products.fr.json @@ -1,4 +1,8 @@ { + "alreadyExists": { + "help": "Veuillez sélectionner un autre article.", + "lead": "L’article est déjà utilisé !" + }, "created": "Le produit a été créé.", "delete": { "confirm": "Désirez-vous vraiment supprimer le produit suivant ?", diff --git a/frontend/src/views/products/ProductEdit.vue b/frontend/src/views/products/ProductEdit.vue index dd64ed9f..f67469ae 100644 --- a/frontend/src/views/products/ProductEdit.vue +++ b/frontend/src/views/products/ProductEdit.vue @@ -24,7 +24,7 @@ import type { Article } from "@/types/articles"; import type { Department } from "@/types/departments"; import type { Product, UnitType } from "@/types/products"; import type { Store } from "@/types/stores"; -import { createOrReplaceProduct, deleteProduct, readProduct } from "@/api/products"; +import { createOrReplaceProduct, deleteProduct, readProduct, readProductByArticle } from "@/api/products"; import { handleErrorKey } from "@/inject/App"; import { readStore } from "@/api/stores"; import { useToastStore } from "@/stores/toast"; @@ -44,6 +44,7 @@ const flags = ref(""); const hasLoaded = ref(false); const isDeleting = ref(false); const product = ref(); +const productAlreadyExists = ref(false); const sku = ref(""); const skuAlreadyUsed = ref(false); const store = ref(); @@ -79,9 +80,31 @@ async function onDelete(hideModal: () => void): Promise { } } +async function checkAlreadyExists(): Promise { + if (store.value && article.value) { + try { + await readProductByArticle(store.value.id, article.value.id); + productAlreadyExists.value = true; + } catch (e: unknown) { + const { status } = e as ApiError; + if (status === 404) { + productAlreadyExists.value = false; + } else { + handleError(e); + } + } + } else { + productAlreadyExists.value = false; + } +} +function onArticleSelected(selected?: Article): void { + article.value = selected; + checkAlreadyExists(); +} function onStoreSelected(selected?: Store): void { store.value = selected; department.value = undefined; + checkAlreadyExists(); } function setModel(model: Product): void { @@ -168,13 +191,16 @@ onMounted(async () => {