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

fix: FORMS-904 user insert race condition #1129

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions app/src/forms/auth/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,23 @@ const service = {
idpCode: data.idp,
};

await User.query(trx).insert(obj);
await User.query(trx).insert(obj).onConflict('keycloakId').ignore();
await trx.commit();
const result = await service.readUser(obj.id);
const result = await service.readUser(obj.keycloakId);
return result;
} catch (err) {
if (trx) await trx.rollback();
throw err;
}
},

readUser: async (id) => {
return User.query().findById(id).throwIfNotFound();
readUser: async (keycloakId) => {
return User.query().modify('filterKeycloakId', keycloakId).first().throwIfNotFound();
},

updateUser: async (id, data) => {
let trx;
try {
const obj = await service.readUser(id);
trx = await User.startTransaction();

const update = {
Expand All @@ -54,9 +53,9 @@ const service = {
idpCode: data.idp,
};

await User.query(trx).patchAndFetchById(obj.id, update);
await User.query(trx).patchAndFetchById(id, update);
Comment on lines -57 to +56
Copy link
Collaborator

Choose a reason for hiding this comment

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

This patchAndFetchById could probably be streamlined to just be a patch (since the return value is being ignored) but I didn't want to mess too much with this for now.

await trx.commit();
const result = await service.readUser(id);
const result = await service.readUser(update.keycloakId);
return result;
} catch (err) {
if (trx) await trx.rollback();
Expand Down
Loading