diff --git a/src/database/queries/userCredential.js b/src/database/queries/userCredential.js index 64a724af..d21f20fa 100644 --- a/src/database/queries/userCredential.js +++ b/src/database/queries/userCredential.js @@ -2,21 +2,21 @@ const UserCredential = require('@database/models/index').UserCredential const { UniqueConstraintError, ValidationError } = require('sequelize') -exports.create = async (data) => { +exports.create = async (data, transaction = null) => { try { - const res = await UserCredential.create(data) + const res = await UserCredential.create(data, { transaction }) return res.get({ plain: true }) } catch (error) { if (error instanceof UniqueConstraintError) { - return 'User already exist' + throw new Error('User already exists') } else if (error instanceof ValidationError) { let message error.errors.forEach((err) => { message = `${err.path} cannot be null.` }) - return message + throw new Error(message) } else { - return error.message + throw error } } } @@ -30,20 +30,25 @@ exports.findOne = async (filter, options = {}) => { }) } catch (error) { console.log(error) - return error + throw error } } -exports.updateUser = async (filter, update, options = {}) => { +exports.updateUser = async (filter, update, options = {}, transaction = null) => { try { - return await UserCredential.update(update, { + // Add the transaction to the options if it's provided + const updateOptions = { where: filter, ...options, individualHooks: true, - }) + transaction, // Add transaction if it's passed + } + + // Perform the update + return await UserCredential.update(update, updateOptions) } catch (error) { console.log(error) - return error + throw error } } diff --git a/src/database/queries/users.js b/src/database/queries/users.js index 12f91633..ff2ef576 100644 --- a/src/database/queries/users.js +++ b/src/database/queries/users.js @@ -10,23 +10,23 @@ exports.getColumns = async () => { try { return await Object.keys(database.User.rawAttributes) } catch (error) { - return error + throw error } } exports.getModelName = async () => { try { return await database.User.name } catch (error) { - return error + throw error } } -exports.create = async (data) => { + +exports.create = async (data, transaction = null) => { try { - console.log('REACHED CREATE FUNCTION') - return await database.User.create(data) + return await database.User.create(data, { transaction }) } catch (error) { console.log(error) - return error + throw error } } @@ -38,7 +38,7 @@ exports.findOne = async (filter, options = {}) => { raw: true, }) } catch (error) { - return error + throw error } } @@ -50,7 +50,7 @@ exports.updateUser = async (filter, update, options = {}) => { individualHooks: true, }) } catch (error) { - return error + throw error } } @@ -58,7 +58,7 @@ exports.findByPk = async (id) => { try { return await database.User.findByPk(id, { raw: true }) } catch (error) { - return error + throw error } } @@ -70,7 +70,7 @@ exports.findAll = async (filter, options = {}) => { raw: true, }) } catch (error) { - return error + throw error } } @@ -140,7 +140,7 @@ exports.findAllUserWithOrganization = async (filter, options = {}) => { nest: true, }) } catch (error) { - return error + throw error } } @@ -164,7 +164,7 @@ exports.findUserWithOrganization = async (filter, options = {}) => { nest: true, }) } catch (error) { - return error + throw error } } exports.listUsersFromView = async ( diff --git a/src/services/account.js b/src/services/account.js index ec93526e..d07b68d4 100644 --- a/src/services/account.js +++ b/src/services/account.js @@ -24,6 +24,7 @@ const userInviteQueries = require('@database/queries/orgUserInvite') const entityTypeQueries = require('@database/queries/entityType') const utils = require('@generics/utils') const { Op } = require('sequelize') +const sequelize = require('@database/models/index').sequelize const { removeDefaultOrgEntityTypes } = require('@generics/utils') const UserCredentialQueries = require('@database/queries/userCredential') const emailEncryption = require('@utils/emailEncryption') @@ -46,6 +47,7 @@ module.exports = class AccountHelper { static async create(bodyData, deviceInfo) { const projection = ['password'] + const transaction = await sequelize.transaction() try { const plaintextEmailId = bodyData.email.toLowerCase() @@ -199,7 +201,7 @@ module.exports = class AccountHelper { delete bodyData.role bodyData.email = encryptedEmailId - const insertedUser = await userQueries.create(bodyData) + const insertedUser = await userQueries.create(bodyData, transaction) const userCredentialsBody = { email: encryptedEmailId, @@ -216,11 +218,14 @@ module.exports = class AccountHelper { { user_id: insertedUser.id, password: bodyData.password }, { raw: true, - } + }, + transaction ) } else { - userCredentials = await UserCredentialQueries.create(userCredentialsBody) + userCredentials = await UserCredentialQueries.create(userCredentialsBody, transaction) } + await transaction.commit() + /* FLOW STARTED: user login after registration */ user = await userQueries.findUserWithOrganization( { id: insertedUser.id, organization_id: insertedUser.organization_id }, @@ -373,6 +378,7 @@ module.exports = class AccountHelper { }) } catch (error) { console.log(error) + await transaction.rollback() throw error } }