Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

fix: sendtopic after error happened #101

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
82 changes: 47 additions & 35 deletions src/modules/user/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,21 @@ const uniqueFields = [['handle']]
async function create (entity, auth) {
await dbHelper.makeSureUnique(User, entity, uniqueFields)

const result = await sequelize.transaction(async (t) => {
const userEntity = await dbHelper.create(User, entity, auth, t)
await serviceHelper.createRecordInEs(resource, userEntity.dataValues, true)
try {
await helper.postEvent(config.UBAHN_CREATE_USER_TOPIC, userEntity.dataValues)
} catch (err) {
logger.logFullError(err)
let payload
try {
const result = await sequelize.transaction(async (t) => {
const userEntity = await dbHelper.create(User, entity, auth, t)
payload = userEntity.dataValues
await serviceHelper.createRecordInEs(resource, userEntity.dataValues, true)
return userEntity
})
return result
} catch (e) {
if (payload) {
helper.postEvent(config.UBAHN_CREATE_USER_TOPIC, payload)
}
})

return result
throw e
}
}

create.schema = {
Expand All @@ -65,19 +69,24 @@ create.schema = {
async function patch (id, entity, auth, params) {
await dbHelper.makeSureUnique(User, entity, uniqueFields)

const result = await sequelize.transaction(async (t) => {
const newEntity = await dbHelper.update(User, id, entity, auth, null, t)
await serviceHelper.patchRecordInEs(resource, newEntity.dataValues, true)
let payload
try {
const result = await sequelize.transaction(async (t) => {
const newEntity = await dbHelper.update(User, id, entity, auth, null, t)
payload = newEntity.dataValues

try {
await helper.postEvent(config.UBAHN_UPDATE_USER_TOPIC, newEntity.dataValues)
} catch (err) {
logger.logFullError(err)
}
return newEntity
})
await serviceHelper.patchRecordInEs(resource, newEntity.dataValues, true)

return newEntity
})

return result
return result
} catch (e) {
if (payload) {
helper.postEvent(config.UBAHN_UPDATE_USER_TOPIC, payload)
}
throw e
}
}

patch.schema = {
Expand Down Expand Up @@ -180,20 +189,23 @@ async function remove (id, auth, params) {
* @param params the path params
*/
async function beginCascadeDelete (id, params) {
await sequelize.transaction(async (t) => {
await serviceHelper.deleteChild(Achievement, id, ['userId', 'achievementsProviderId'], 'Achievement', t)
await serviceHelper.deleteChild(ExternalProfile, id, ['userId', 'organizationId'], 'ExternalProfile', t)
await serviceHelper.deleteChild(UserAttribute, id, ['userId', 'attributeId'], 'UserAttribute', t)
await serviceHelper.deleteChild(UsersRole, id, ['userId', 'roleId'], 'UsersRole', t)
await serviceHelper.deleteChild(UsersSkill, id, ['userId', 'skillId'], 'UsersSkill', t)
await dbHelper.remove(User, id, null, t)
await serviceHelper.deleteRecordFromEs(id, params, resource, true)
try {
await helper.postEvent(config.UBAHN_DELETE_USER_TOPIC, {id})
} catch (err) {
logger.logFullError(err)
}
})
let payload = {id}
try {
await sequelize.transaction(async (t) => {
await serviceHelper.deleteChild(Achievement, id, ['userId', 'achievementsProviderId'], 'Achievement', t)
aa.bb
await serviceHelper.deleteChild(ExternalProfile, id, ['userId', 'organizationId'], 'ExternalProfile', t)
await serviceHelper.deleteChild(UserAttribute, id, ['userId', 'attributeId'], 'UserAttribute', t)
await serviceHelper.deleteChild(UsersRole, id, ['userId', 'roleId'], 'UsersRole', t)
await serviceHelper.deleteChild(UsersSkill, id, ['userId', 'skillId'], 'UsersSkill', t)
await dbHelper.remove(User, id, null, t)
await serviceHelper.deleteRecordFromEs(id, params, resource, true)
})

} catch (e) {
helper.postEvent(config.UBAHN_DELETE_USER_TOPIC, payload)
throw e
}
}

module.exports = {
Expand Down