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

feat: update account creation flow to use transaction for consistency #586

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
25 changes: 15 additions & 10 deletions src/database/queries/userCredential.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand All @@ -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
}
}

Expand Down
24 changes: 12 additions & 12 deletions src/database/queries/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -38,7 +38,7 @@ exports.findOne = async (filter, options = {}) => {
raw: true,
})
} catch (error) {
return error
throw error
}
}

Expand All @@ -50,15 +50,15 @@ exports.updateUser = async (filter, update, options = {}) => {
individualHooks: true,
})
} catch (error) {
return error
throw error
}
}

exports.findByPk = async (id) => {
try {
return await database.User.findByPk(id, { raw: true })
} catch (error) {
return error
throw error
}
}

Expand All @@ -70,7 +70,7 @@ exports.findAll = async (filter, options = {}) => {
raw: true,
})
} catch (error) {
return error
throw error
}
}

Expand Down Expand Up @@ -140,7 +140,7 @@ exports.findAllUserWithOrganization = async (filter, options = {}) => {
nest: true,
})
} catch (error) {
return error
throw error
}
}

Expand All @@ -164,7 +164,7 @@ exports.findUserWithOrganization = async (filter, options = {}) => {
nest: true,
})
} catch (error) {
return error
throw error
}
}
exports.listUsersFromView = async (
Expand Down
12 changes: 9 additions & 3 deletions src/services/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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()
Expand Down Expand Up @@ -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,
Expand All @@ -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 },
Expand Down Expand Up @@ -373,6 +378,7 @@ module.exports = class AccountHelper {
})
} catch (error) {
console.log(error)
await transaction.rollback()
throw error
}
}
Expand Down