Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Luizoamorim/feat save commitments #867

Merged
merged 7 commits into from
Aug 24, 2022

Conversation

luizoamorim
Copy link
Contributor

This PR add an endpoint to saveCommitments functionality and also a function in the service to do the data manupulation.

@imagobea imagobea linked an issue Aug 10, 2022 that may be closed by this pull request
4 tasks
nightfall-client/src/services/commitment-storage.mjs Outdated Show resolved Hide resolved
Copy link
Contributor

@imagobea imagobea left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @luizoamorim

Apologies for the late feedback. As discussed:

  • We have implemented a number of validations in the SDK, but perhaps they belong in the API in case other requests are sent from different services

  • In standup today we confirmed that an invalid commitment can't be spent, this means we might want to bulk insert all commitments (after proving that the "format" of the commitment is correct). At the same time, I'm not familiar with MongoDB, so might be good to check what would happen in the event of trying to insert an existing id.

  • Once we have addressed the above and the logic is clear, we should double-check the route (we prob don't need lines 103 - 105)

Many thanks ☮️

@luizoamorim
Copy link
Contributor Author

@daveroga @imagobea @israelboudoux

I decided to follow the logic of remove the existent commitments found in database. The other using upsert I didn't had success.
So I'm following the next steps:

/**
   * 1. listOfCommitments => get only the ids
   */
  const commitmentsIds = listOfCommitments.map(commitment => commitment._id);
  /**
   * 2. Find commitments that already exists in DB
   */
  const commitmentsFound = await db
    .collection(COMMITMENTS_COLLECTION)
    .find({ _id: { $in: commitmentsIds } })
    .toArray();

  /**
   * 3. remove the commitments found in the database from the list
   */
  const onlyNewCommitments = listOfCommitments.filter(
    commitments =>
      commitmentsFound.find(commitmentFound => commitmentFound.id === commitments.id) === undefined,
  );

  if (onlyNewCommitments.length > 0) {
    /**
     * 4. Insert all
     */
    await db.collection(COMMITMENTS_COLLECTION).insertMany(onlyNewCommitments);

    /**
     * 5. Sycronize from beggining
     */
    await syncState();

    return { successMessage: 'Commitments have been saved successfully!' };

Copy link
Contributor

@imagobea imagobea left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good @luizoamorim 💃
I have added some thoughts and recommended some changes, but to me this is exactly what we agreed. Well played!! 🪘

@@ -1,3 +1,4 @@
/* eslint-disable import/no-cycle */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀 I can see this rule has been added to several files in the PR, is this needed? What was the error??

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This problem started to appear. I don't know exactly why.

@@ -753,14 +755,61 @@ export async function findUsableCommitmentsMutex(
);
}

/**
*
* @function saveCommitments save a list of commitments in the database
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to rename the function as e.g. insertCommitmentsAndResync (?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be. Changed.

Comment on lines 762 to 763
* @returns if all the commitments in the list already exists in the database
* throw an error, else return a success message.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use the @throws {Error} to explain the "throw error" bit

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

Comment on lines 769 to 771
/**
* 1. listOfCommitments => get only the ids
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make the comments less verbose

Suggested change
/**
* 1. listOfCommitments => get only the ids
*/
// listOfCommitments => get only the ids

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed for just 1 line.

/**
* 1. listOfCommitments => get only the ids
*/
const commitmentsIds = listOfCommitments.map(commitment => commitment._id);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const commitmentsIds = listOfCommitments.map(commitment => commitment._id);
const commitmentIds = listOfCommitments.map(commitment => commitment._id);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case I prefer leave in plural. Because a list of ids for several commitments not just one.

/**
* 2. Find commitments that already exists in DB
*/
const commitmentsFound = await db
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const commitmentsFound = await db
const commitmentsFromDb = await db

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sense! Changed.

commitmentsFound.find(commitmentFound => commitmentFound.id === commitments.id) === undefined,
);

if (onlyNewCommitments.length > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Length 0 is falsy already

Suggested change
if (onlyNewCommitments.length > 0) {
if (onlyNewCommitments.length) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be! Changed!

Comment on lines 784 to 787
const onlyNewCommitments = listOfCommitments.filter(
commitments =>
commitmentsFound.find(commitmentFound => commitmentFound.id === commitments.id) === undefined,
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Singular!
Very nice chain of array methods 😊 👏

Suggested change
const onlyNewCommitments = listOfCommitments.filter(
commitments =>
commitmentsFound.find(commitmentFound => commitmentFound.id === commitments.id) === undefined,
);
const onlyNewCommitments = listOfCommitments.filter(
commitment =>
commitmentsFound.find(commitmentFound => commitmentFound.id === commitment.id) === undefined,
);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed!!

Comment on lines 97 to 98
router.post('/saveAll', async (req, res, next) => {
logger.debug('commitment/saveAll endpoint received POST');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we had a standard API, POST /commitments would already signal insertion.

I assume the above is not possible, therefore perhaps a "one-word" route should suffice, e.g. /save. This could also be /insert or /create.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to save

@imagobea imagobea added One more approval needed One reviewer has approved this PR but another is needed and removed Changes requested labels Aug 22, 2022
Copy link
Contributor

@daveroga daveroga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

…e database

changed commitment.id to commiment._id that is the correct field
@luizoamorim luizoamorim dismissed druiz0992’s stale review August 24, 2022 01:49

We already applied the improvements.

@imagobea imagobea removed the One more approval needed One reviewer has approved this PR but another is needed label Aug 24, 2022
@luizoamorim luizoamorim merged commit 7f722a2 into master Aug 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add an endpoint to nightfall-client API for importing data
5 participants