-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
137 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,95 @@ | ||
import { unit_of_work } from "$lib/prisma-enums"; | ||
import type { scm_article } from "@prisma/client"; | ||
import { redirect, type Actions } from "@sveltejs/kit"; | ||
import { fail, redirect, type Actions } from "@sveltejs/kit"; | ||
|
||
export const actions: Actions = { | ||
import: async ({ request, locals }) => { | ||
|
||
const warnings = []; | ||
|
||
try | ||
{ | ||
// TODO: Check this | ||
const form = await request.formData(); | ||
const file = form.get("file"); | ||
const columnOrder = form.get("columnOrder"); | ||
const columnOrder = form.get("column_order"); | ||
|
||
const colSplitter = form.get("col_splitter")?.toString(); | ||
const rowSplitter = form.get("row_splitter")?.toString(); | ||
|
||
if(!(file instanceof File)) | ||
throw "article.import.file.missing"; | ||
return fail(400, { import: { error: "errors.article.import.missing_file" }}); | ||
|
||
if(columnOrder === null) | ||
throw "article.import.columnOrder.missing"; | ||
return fail(500, { import: { error: "errors.article.import.column_order_invalid" }}); | ||
|
||
if(colSplitter === undefined || !([",", ";"].includes(colSplitter))) | ||
return fail(400, { import: { error : "errors.article.import.col_splitter_invalid" }}); | ||
|
||
if(rowSplitter === undefined || !(["n", "rn"].includes(rowSplitter))) | ||
return fail(400, { import: { error: "errors.article.import.row_splitter_invalid" }}); | ||
|
||
const fileContent = await file.text(); | ||
const columns = JSON.parse(columnOrder.toString()) as (string | null)[]; | ||
|
||
const lines = fileContent.split("\r\n").map(line => line.split(",")); | ||
console.log(columns); | ||
|
||
for(const line of lines) | ||
const lineSplitter = rowSplitter === "rn" ? "\r\n" : "\n"; | ||
|
||
const lines = fileContent.split(lineSplitter).map(line => line.split(colSplitter)); | ||
|
||
for(const line of lines.slice(1)) | ||
{ | ||
const article = { | ||
|
||
const nonPhysicalValue = line[columns.indexOf("non_physical")]; | ||
const consumableValue = line[columns.indexOf("consumable")]; | ||
const internalValue = line[columns.indexOf("internal")]; | ||
|
||
const article: Partial<scm_article> = { | ||
name: line[columns.indexOf("name")], | ||
reference: line[columns.indexOf("reference")], | ||
brand: line[columns.indexOf("manufacturer")], | ||
|
||
} satisfies Partial<scm_article>; | ||
reference: line[columns.indexOf("sku")], | ||
brand: line[columns.indexOf("brand")], | ||
|
||
order_quantity: Number(line[columns.indexOf("order_quantity")]) || undefined, | ||
critical_quantity: Number(line[columns.indexOf("critical_quantity")]) || undefined, | ||
|
||
non_physical: nonPhysicalValue === "true" || nonPhysicalValue === "1", | ||
consumable: consumableValue === "true" || consumableValue === "1", | ||
internal: internalValue === "true" || internalValue === "1", | ||
}; | ||
|
||
// Parse article unit | ||
const articleUnit = line[columns.indexOf("unit_of_work")]; | ||
if(articleUnit !== "") | ||
{ | ||
if(Object.keys(unit_of_work).includes(articleUnit)) | ||
{ | ||
article.unit = articleUnit as unit_of_work; | ||
article.unit_quantity = Number(line[columns.indexOf("unit_of_work_quantity")]) || null; | ||
} | ||
else | ||
{ | ||
warnings.push(`Found unit ${articleUnit} which is not compatible with ${Object.keys(unit_of_work).join(",")} for article ${article.reference}`); | ||
} | ||
} | ||
|
||
if(article.brand === undefined || article.name === undefined || article.reference === undefined) // skip rows with missing data | ||
continue; | ||
|
||
console.log("dry-run", article); | ||
|
||
await locals.prisma.scm_article.create({ data: article }); | ||
} | ||
|
||
throw redirect(303, "/app/scm/articles"); | ||
|
||
if(warnings.length > 0) | ||
return { import: { success: true, warnings }}; | ||
} | ||
catch(ex) | ||
{ | ||
console.log(ex); | ||
return { error: true } | ||
} | ||
|
||
return redirect(303, "/app/scm/articles"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters