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 #1126

Closed
wants to merge 1 commit into from
Closed
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
21 changes: 10 additions & 11 deletions app/src/forms/auth/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { queryUtils } = require('../common/utils');
const FORM_SUBMITTER = require('../common/constants').Permissions.FORM_SUBMITTER;

const service = {
createUser: async (data) => {
_createUser: async (data) => {
let trx;
try {
trx = await User.startTransaction();
Expand All @@ -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).throwIfNotFound();
},

updateUser: async (id, data) => {
_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);
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 Expand Up @@ -118,10 +117,10 @@ const service = {

if (!user) {
// add to the system.
user = await service.createUser(obj);
user = await service._createUser(obj);
} else {
// what if name or email changed?
user = await service.updateUser(user.id, obj);
user = await service._updateUser(user.id, obj);
}

// return with the db id...
Expand Down
Loading