From e6f08ac5fa8a5ff27a30c89ff8882627f47db501 Mon Sep 17 00:00:00 2001 From: Khaled FERJANI Date: Tue, 9 Jul 2024 14:09:29 +0100 Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=90=9B=20fix:=20update=20existing=20r?= =?UTF-8?q?ecovery=20words?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/vault-api/controllers/vault.ts | 38 +++++++++++++++---- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/packages/tom-server/src/vault-api/controllers/vault.ts b/packages/tom-server/src/vault-api/controllers/vault.ts index 049f0fc2..63393777 100644 --- a/packages/tom-server/src/vault-api/controllers/vault.ts +++ b/packages/tom-server/src/vault-api/controllers/vault.ts @@ -1,4 +1,6 @@ +/* eslint-disable no-useless-return */ import { type TwakeDB } from '../../db' +import type { Collections } from '../../types' import { VaultAPIError, type expressAppHandler } from '../utils' export type VaultController = (db: TwakeDB) => expressAppHandler @@ -9,14 +11,34 @@ export const methodNotAllowed: expressAppHandler = (req, res, next) => { export const saveRecoveryWords = (db: TwakeDB): expressAppHandler => { return (req, res, next) => { - const data: Record = { - userId: req.token.content.sub, - words: req.body.words - } - // @ts-expect-error 'recoveryWords' isn't declared in Collection - db.insert('recoveryWords', data) - .then((_) => { - res.status(201).json({ message: 'Saved recovery words sucessfully' }) + const { words } = req.body + const userId = req.token.content.sub + + db.get('recoveryWords' as Collections, ['words'], { userId }) + .then((data) => { + if (data.length > 0) { + db.update('recoveryWords' as Collections, { words }, 'userId', userId) + .then((_) => { + res + .status(200) + .json({ message: 'Recovery words updated sucessfully' }) + }) + .catch((err) => { + next(err) + return + }) + } else { + db.insert('recoveryWords' as Collections, { userId, words }) + .then((_) => { + res + .status(201) + .json({ message: 'Recovery words saved sucessfully' }) + }) + .catch((err) => { + next(err) + return + }) + } }) .catch((err) => { next(err) From 54d0c548019e6661a3ab82c12c580a7432a11448 Mon Sep 17 00:00:00 2001 From: Khaled FERJANI Date: Tue, 9 Jul 2024 14:09:43 +0100 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=A7=AA=20chore:=20update=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/vault-api/controllers/vault.test.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/tom-server/src/vault-api/controllers/vault.test.ts b/packages/tom-server/src/vault-api/controllers/vault.test.ts index 88ae7dac..177d4ec6 100644 --- a/packages/tom-server/src/vault-api/controllers/vault.test.ts +++ b/packages/tom-server/src/vault-api/controllers/vault.test.ts @@ -14,7 +14,8 @@ describe('Vault controllers', () => { const dbManager: Partial = { get: jest.fn(), insert: jest.fn(), - deleteWhere: jest.fn() + deleteWhere: jest.fn(), + update: jest.fn() } let mockRequest: ITestRequest let mockResponse: Partial @@ -69,6 +70,7 @@ describe('Vault controllers', () => { // Testing saveRecoveryWords it('should return response with status code 201 on save success', async () => { jest.spyOn(dbManager, 'insert').mockResolvedValue([{ words }]) + jest.spyOn(dbManager, 'get').mockResolvedValue([]) const handler: expressAppHandler = saveRecoveryWords(dbManager as TwakeDB) handler(mockRequest as Request, mockResponse as Response, nextFunction) await new Promise(process.nextTick) @@ -78,12 +80,26 @@ describe('Vault controllers', () => { it('should call next function to throw error on saving failed', async () => { const errorMsg = 'Insert failed' jest.spyOn(dbManager, 'insert').mockRejectedValue(new Error(errorMsg)) + jest.spyOn(dbManager, 'get').mockResolvedValue([]) const handler: expressAppHandler = saveRecoveryWords(dbManager as TwakeDB) handler(mockRequest as Request, mockResponse as Response, nextFunction) await new Promise(process.nextTick) expect(nextFunction).toHaveBeenCalledWith(new Error(errorMsg)) }) + it('should update the recoverywords when it already exists', async () => { + jest + .spyOn(dbManager, 'get') + .mockResolvedValue([{ words: 'Another sentence for the same user' }]) + jest.spyOn(dbManager, 'update').mockResolvedValue([{ words }]) + const handler: expressAppHandler = saveRecoveryWords(dbManager as TwakeDB) + handler(mockRequest as Request, mockResponse as Response, nextFunction) + await new Promise(process.nextTick) + expect(mockResponse.statusCode).toEqual(200) + expect(dbManager.update).toHaveBeenCalled() + expect(dbManager.insert).not.toHaveBeenCalled() + }) + // Testing getRecoveryWords it('should return response with status code 200 on get success', async () => { From 7037b9f5ed5acf23d19934b5f9c1d97b8859c9f5 Mon Sep 17 00:00:00 2001 From: Khaled FERJANI Date: Tue, 9 Jul 2024 14:24:22 +0100 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=8E=A8=20chore:=20fix=20typo=20in=20s?= =?UTF-8?q?uccess=20message?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/tom-server/src/vault-api/controllers/vault.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/tom-server/src/vault-api/controllers/vault.ts b/packages/tom-server/src/vault-api/controllers/vault.ts index 63393777..e65433a4 100644 --- a/packages/tom-server/src/vault-api/controllers/vault.ts +++ b/packages/tom-server/src/vault-api/controllers/vault.ts @@ -21,7 +21,7 @@ export const saveRecoveryWords = (db: TwakeDB): expressAppHandler => { .then((_) => { res .status(200) - .json({ message: 'Recovery words updated sucessfully' }) + .json({ message: 'Updated recovery words sucessfully' }) }) .catch((err) => { next(err) @@ -32,7 +32,7 @@ export const saveRecoveryWords = (db: TwakeDB): expressAppHandler => { .then((_) => { res .status(201) - .json({ message: 'Recovery words saved sucessfully' }) + .json({ message: 'Saved recovery words sucessfully' }) }) .catch((err) => { next(err)