-
Notifications
You must be signed in to change notification settings - Fork 56
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
Conversation
There was a problem hiding this 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 ☮️
- also added the export for syncState function to be used in the import commitments.
@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.
|
There was a problem hiding this 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 */ |
There was a problem hiding this comment.
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??
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
(?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be. Changed.
* @returns if all the commitments in the list already exists in the database | ||
* throw an error, else return a success message. |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added.
/** | ||
* 1. listOfCommitments => get only the ids | ||
*/ |
There was a problem hiding this comment.
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
/** | |
* 1. listOfCommitments => get only the ids | |
*/ | |
// listOfCommitments => get only the ids |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const commitmentsIds = listOfCommitments.map(commitment => commitment._id); | |
const commitmentIds = listOfCommitments.map(commitment => commitment._id); |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const commitmentsFound = await db | |
const commitmentsFromDb = await db |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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
if (onlyNewCommitments.length > 0) { | |
if (onlyNewCommitments.length) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be! Changed!
const onlyNewCommitments = listOfCommitments.filter( | ||
commitments => | ||
commitmentsFound.find(commitmentFound => commitmentFound.id === commitments.id) === undefined, | ||
); |
There was a problem hiding this comment.
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 😊 👏
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, | |
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed!!
router.post('/saveAll', async (req, res, next) => { | ||
logger.debug('commitment/saveAll endpoint received POST'); |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to save
There was a problem hiding this 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
We already applied the improvements.
This PR add an endpoint to saveCommitments functionality and also a function in the service to do the data manupulation.