0

I am working on an element that given a csv file from a user, takes rows of commands (adding, renaming, updating, removing different items) and executes them into a database on the basis that the commands are correct and legal.

Currently I try to validate each command before it is executed, but I can't catch every mistake like them having a typo when they add an ingredient, and then trying to reference that ingredient by it's correct name in a later row. Because of this, some commands will attempt to execute and fail. Is there a way to "mock-call" every command to ensure they work before actually executing each command?

Currently all commands are executed in this function, and I return true or false if any of the commands failed to execute (for now) which sets the state to either show a success message or not.

try {
    const { categories } = await fetchCategories();
    let results = await sheetData.reduce(async (accum: any, ingredient: any) => {
        let updateArray = await accum;
        // console.log(`>>> ${ingredient.command} ${ingredient.name}`)
        updateArray.push(await buildExecute(ingredient, categories));
        // console.log(`<<< ${ingredient.command} ${ingredient.name}`)
        return updateArray;
    }, Promise.resolve([]));

    const result = await Promise.all(results);
    return !result.some(val => val === false)
} catch (err) {
    return false;
}

inside buildExecute, I simply build up the endpoints to call, the data to include in the body, and the method for the fetch as such

try {
    const { result, error } = await (await fetch(endpoint, {
        method: method,
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify(data),
    })).json();
    if (error) {
        console.error(error);
        return false;
    }
    return true;
} catch (err) {
    console.error(err);
    return false;
}

If there's a library that does what I'm looking for, I'm game to try it out, or if there's a built in method, that works too.

  • 2
    Most databases have "transactions" that do exactly that. – georg Aug 10 '21 at 22:22
  • Yeah, I can see in the console tab whenever a fetch fails, however, I want to present it to the user through something on screen, not through the console tab. – Scoobidy-Bop Aug 10 '21 at 22:32
  • Why are you talking to your database through the fetch api? – Bergi Aug 10 '21 at 22:32
  • For the product I'm working on, the company developed an API that acts as an in-between for database interaction. Possibly for a level of "security" or something along those lines, and the endpoints also determine which parts of the database you interact with. I'm not too certain since I'm still new, and a new dev in general. – Scoobidy-Bop Aug 10 '21 at 22:37
  • Btw, I recommend to [avoid using `reduce` with an `async` callback](https://stackoverflow.com/a/57501330/1048572). Just make that a plain `for` loop. Also `const result = await Promise.all(results);` is pointless, as `results` currently already is an array of plain values, not of promises. – Bergi Aug 10 '21 at 22:39
  • @Scoobidy-Bop Ask them to also provide an API that lets you run multiple interactions at once. Like georg said, this is a prime use case for transactions; also making only a single request to the backend would be more efficient. – Bergi Aug 10 '21 at 22:40
  • @Ernesto He has. He uses them in the code. – Bergi Aug 10 '21 at 22:43
  • @Scoobidy-Bop "*I want to present [the error] to the user through something on screen, not through the console tab*" - then don't have your `buildExecute` method swallow the error by logging to the console and only returning `false`, but have it return (or even throw) the actual error. Handle the error only by displaying it to the user, in the part where your UI actually calls this method. – Bergi Aug 10 '21 at 22:46
  • @Bergi I will talk to them about adding that as an endpoint. They've mentioned that they want customers to have an "all" or "nothing" experience when trying to upload a spreadsheet of commands. – Scoobidy-Bop Aug 10 '21 at 22:49
  • Thank you @georg for the suggestion, I will make a push for that! – Scoobidy-Bop Aug 10 '21 at 22:49
  • @Ernesto I will look into some of those methods. I've never worked with promises before, so this is all a learning experience for me – Scoobidy-Bop Aug 10 '21 at 22:52
  • Here https://javascript.info/promise-api – Ernesto Aug 11 '21 at 01:16

0 Answers0