Skip to content

Commit 4ba01f6

Browse files
committed
Put failed bulk-imports into the drafts table instead of offering JSON
to download.
1 parent 892854f commit 4ba01f6

File tree

1 file changed

+22
-37
lines changed

1 file changed

+22
-37
lines changed

webserver/src/pages/importMany.astro

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ type ImportResult =
7373
| {
7474
result: "some-recipes-malformed" | "success";
7575
importedRecipes: Recipe[];
76+
numDuplicates: number;
7677
malformedRecipes: JsonRecipe[];
78+
numMalformedCreatedAsDrafts: number;
7779
};
7880
7981
async function importPostedRecipes(): Promise<ImportResult> {
@@ -100,11 +102,13 @@ async function importPostedRecipes(): Promise<ImportResult> {
100102
let importResult: ImportResult = {
101103
result: "success",
102104
importedRecipes: [],
105+
numDuplicates: 0,
103106
malformedRecipes: [],
107+
numMalformedCreatedAsDrafts: 0,
104108
};
105109
let i = -1;
106110
for (const recipeResult of await Promise.allSettled(
107-
recipes.map((recipe) => createRecipe(recipe, user))
111+
recipes.map((recipe) => createRecipe(recipe, user)),
108112
)) {
109113
i++;
110114
switch (recipeResult.status) {
@@ -117,6 +121,7 @@ async function importPostedRecipes(): Promise<ImportResult> {
117121
recipeResult.reason.code === "P2002"
118122
) {
119123
// The recipe was just already inserted.
124+
importResult.numDuplicates++;
120125
continue;
121126
}
122127
importResult.result = "some-recipes-malformed";
@@ -126,6 +131,16 @@ async function importPostedRecipes(): Promise<ImportResult> {
126131
}
127132
}
128133
134+
if (importResult.result === "some-recipes-malformed") {
135+
const draftCreateResult = await prisma.draftRecipe.createMany({
136+
data: importResult.malformedRecipes.map((recipe) => ({
137+
userId: user.id,
138+
data: recipe,
139+
})),
140+
});
141+
importResult.numMalformedCreatedAsDrafts = draftCreateResult.count;
142+
}
143+
129144
return importResult;
130145
}
131146
@@ -136,27 +151,6 @@ if (Astro.request.method === "POST") {
136151
}
137152
---
138153

139-
<script>
140-
window.addEventListener(
141-
"DOMContentLoaded",
142-
() => {
143-
const invalidRecipesAnchor = document.getElementById("invalid-recipes");
144-
if (invalidRecipesAnchor) {
145-
if (!(invalidRecipesAnchor instanceof HTMLAnchorElement))
146-
throw new Error("Invalid recipes anchor is not an anchor");
147-
const invalidRecipesFile = new File(
148-
[invalidRecipesAnchor.dataset.invalidRecipes!],
149-
"invalid-recipes.json",
150-
{ type: "application/json" }
151-
);
152-
invalidRecipesAnchor.href = URL.createObjectURL(invalidRecipesFile);
153-
delete invalidRecipesAnchor.dataset.invalidRecipes;
154-
}
155-
},
156-
{ once: true }
157-
);
158-
</script>
159-
160154
<Layout title="Import recipes" user={user} needLogin>
161155
<h1>Import recipes</h1>
162156

@@ -188,21 +182,12 @@ if (Astro.request.method === "POST") {
188182
</>
189183
) : importResult.result === "some-recipes-malformed" ? (
190184
<p>
191-
{importResult.importedRecipes.length} recipes imported. Some of the
192-
recipes you tried to import weren't valid. Look through the
193-
<a
194-
id="invalid-recipes"
195-
data-invalid-recipes={JSON.stringify(
196-
importResult.malformedRecipes,
197-
undefined,
198-
2
199-
)}
200-
download="invalid-recipes.json"
201-
>
202-
invalid recipes,
203-
</a>
204-
fix the errors in their
205-
<code>error</code> fields, and then re-import them.
185+
{importResult.importedRecipes.length} recipes imported.{" "}
186+
{importResult.numDuplicates} were skipped because they have the same
187+
titles as your other recipes. {importResult.numMalformedCreatedAsDrafts}{" "}
188+
of the recipes you tried to import weren't valid and have been saved as
189+
drafts in
190+
<a href={`${import.meta.env.BASE_URL}account`}>your accounts page</a>.
206191
</p>
207192
) : null
208193
}

0 commit comments

Comments
 (0)