From 58a8ea97206ce4c1777c8a9b34ffa49d821765b7 Mon Sep 17 00:00:00 2001 From: David Vitor Antonio Date: Mon, 25 Sep 2023 14:51:54 -0300 Subject: [PATCH] fix(channel-web): fix duplicated mapping error (#1770) * first try * remove log * improve --- modules/channel-web/src/backend/db.ts | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/modules/channel-web/src/backend/db.ts b/modules/channel-web/src/backend/db.ts index e1ca9f0fde..ac1e4ed877 100644 --- a/modules/channel-web/src/backend/db.ts +++ b/modules/channel-web/src/backend/db.ts @@ -106,18 +106,24 @@ export default class WebchatDb { } async createUserMapping(botId: string, visitorId: string, userId: string): Promise { - const mapping = { botId, visitorId, userId } + let mapping: UserMapping | undefined = { botId, visitorId, userId } try { - await this.knex('web_user_map').insert(mapping) - this.cacheByVisitor.set(`${botId}_${visitorId}`, mapping) - - return mapping + try { + await this.knex('web_user_map').insert(mapping) + this.cacheByVisitor.set(`${botId}_${visitorId}`, mapping) + } catch (err) { + // If the mapping already exists, we return it + mapping = await this.getMappingFromUser(userId) + if (!mapping) { + throw err + } + } } catch (err) { - this.bp.logger.error('An error occurred while creating a user mapping.', err) - - return undefined + this.bp.logger.forBot(botId).error('An error occurred while creating a user mapping.', err) } + + return mapping } async getFeedbackInfoForMessageIds(_target: string, messageIds: string[]) {