diff --git a/backend/src/controller/chatController.ts b/backend/src/controller/chatController.ts index 37d89208a..ad8b05435 100644 --- a/backend/src/controller/chatController.ts +++ b/backend/src/controller/chatController.ts @@ -8,7 +8,6 @@ import { } from '@src/defence'; import { OpenAiAddInfoToChatHistoryRequest } from '@src/models/api/OpenAiAddInfoToChatHistoryRequest'; import { OpenAiChatRequest } from '@src/models/api/OpenAiChatRequest'; -import { OpenAiClearRequest } from '@src/models/api/OpenAiClearRequest'; import { DefenceReport, ChatHttpResponse, @@ -412,16 +411,4 @@ function handleAddInfoToChatHistory( } } -function handleClearChatHistory(req: OpenAiClearRequest, res: Response) { - const level = req.body.level; - if (level !== undefined && level >= LEVEL_NAMES.LEVEL_1) { - req.session.levelState[level].chatHistory = []; - console.debug('ChatGPT messages cleared'); - res.send(); - } else { - res.status(400); - res.send(); - } -} - -export { handleChatToGPT, handleAddInfoToChatHistory, handleClearChatHistory }; +export { handleChatToGPT, handleAddInfoToChatHistory }; diff --git a/backend/src/controller/emailController.ts b/backend/src/controller/emailController.ts deleted file mode 100644 index 1c277a3be..000000000 --- a/backend/src/controller/emailController.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Response } from 'express'; - -import { EmailClearRequest } from '@src/models/api/EmailClearRequest'; -import { LEVEL_NAMES } from '@src/models/level'; - -function handleClearEmails(req: EmailClearRequest, res: Response) { - const level = req.body.level; - if (level !== undefined && level >= LEVEL_NAMES.LEVEL_1) { - req.session.levelState[level].sentEmails = []; - console.debug('Emails cleared'); - res.send(); - } else { - res.status(400); - res.send(); - } -} - -export { handleClearEmails }; diff --git a/backend/src/controller/resetController.ts b/backend/src/controller/resetController.ts index 70931136d..602522f42 100644 --- a/backend/src/controller/resetController.ts +++ b/backend/src/controller/resetController.ts @@ -1,7 +1,9 @@ import { Response } from 'express'; import { LevelGetRequest } from '@src/models/api/LevelGetRequest'; +import { LevelResetRequest } from '@src/models/api/LevelResetRequest'; import { defaultChatModel } from '@src/models/chat'; +import { ChatInfoMessage } from '@src/models/chatMessage'; import { LEVEL_NAMES, getInitialLevelStates, @@ -33,4 +35,25 @@ function handleResetProgress(req: LevelGetRequest, res: Response) { }); } -export { handleResetProgress }; +function handleResetLevel(req: LevelResetRequest, res: Response) { + const { level: levelStr } = req.params; + + const level = Number(levelStr) as LEVEL_NAMES; + + if (!isValidLevel(level)) { + res.status(400).send('Invalid level'); + return; + } + + console.debug('Resetting progress for level ', level); + req.session.levelState[level].chatHistory = []; + req.session.levelState[level].sentEmails = []; + res.send({ + chatInfoMessage: { + infoMessage: `Level progress reset`, + chatMessageType: 'RESET_LEVEL', + } as ChatInfoMessage, + }); +} + +export { handleResetProgress, handleResetLevel }; diff --git a/backend/src/models/api/EmailClearRequest.ts b/backend/src/models/api/EmailClearRequest.ts deleted file mode 100644 index 2b1704807..000000000 --- a/backend/src/models/api/EmailClearRequest.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Request } from 'express'; - -import { LEVEL_NAMES } from '@src/models/level'; - -export type EmailClearRequest = Request< - never, - never, - { - level?: LEVEL_NAMES; - }, - never ->; diff --git a/backend/src/models/api/LevelResetRequest.ts b/backend/src/models/api/LevelResetRequest.ts new file mode 100644 index 000000000..df22aaebd --- /dev/null +++ b/backend/src/models/api/LevelResetRequest.ts @@ -0,0 +1,12 @@ +import { Request } from 'express'; + +import { ChatMessage } from '@src/models/chatMessage'; + +export type LevelResetRequest = Request< + { level: string }, + { + chatInfoMessage?: ChatMessage; + }, + never, + never +>; diff --git a/backend/src/models/api/OpenAiClearRequest.ts b/backend/src/models/api/OpenAiClearRequest.ts deleted file mode 100644 index a13fca379..000000000 --- a/backend/src/models/api/OpenAiClearRequest.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Request } from 'express'; - -import { LEVEL_NAMES } from '@src/models/level'; - -export type OpenAiClearRequest = Request< - never, - never, - { - level?: LEVEL_NAMES; - }, - never ->; diff --git a/backend/src/sessionRoutes.ts b/backend/src/sessionRoutes.ts index 5821f0a4b..9f07414d4 100644 --- a/backend/src/sessionRoutes.ts +++ b/backend/src/sessionRoutes.ts @@ -6,7 +6,6 @@ import memoryStoreFactory from 'memorystore'; import { handleChatToGPT, handleAddInfoToChatHistory, - handleClearChatHistory, } from './controller/chatController'; import { handleConfigureDefence, @@ -14,13 +13,15 @@ import { handleDefenceDeactivation, handleResetDefenceConfigItem, } from './controller/defenceController'; -import { handleClearEmails } from './controller/emailController'; import { handleLoadLevel } from './controller/levelController'; import { handleConfigureModel, handleSetModel, } from './controller/modelController'; -import { handleResetProgress } from './controller/resetController'; +import { + handleResetLevel, + handleResetProgress, +} from './controller/resetController'; import { handleStart } from './controller/startController'; import { handleTest } from './controller/testController'; import { defaultChatModel } from './models/chat'; @@ -92,20 +93,17 @@ router.post('/defence/deactivate', handleDefenceDeactivation); router.post('/defence/configure', handleConfigureDefence); router.post('/defence/resetConfig', handleResetDefenceConfigItem); -// emails -router.post('/email/clear', handleClearEmails); - // chat router.post('/openai/chat', handleChatToGPT); router.post('/openai/addInfoToHistory', handleAddInfoToChatHistory); -router.post('/openai/clear', handleClearChatHistory); // model configurations router.post('/openai/model', handleSetModel); router.post('/openai/model/configure', handleConfigureModel); -// reset progress for all levels -router.post('/reset', handleResetProgress); +// reset +router.post('/reset/all', handleResetProgress); +router.post('/reset/:level', handleResetLevel); // Testing endpoints router.post('/test/load', handleTest); diff --git a/backend/test/unit/controller/chatController.test.ts b/backend/test/unit/controller/chatController.test.ts index 21dcbd924..079f25624 100644 --- a/backend/test/unit/controller/chatController.test.ts +++ b/backend/test/unit/controller/chatController.test.ts @@ -11,12 +11,10 @@ import { Response } from 'express'; import { handleAddInfoToChatHistory, handleChatToGPT, - handleClearChatHistory, } from '@src/controller/chatController'; import { detectTriggeredInputDefences, transformMessage } from '@src/defence'; import { OpenAiAddInfoToChatHistoryRequest } from '@src/models/api/OpenAiAddInfoToChatHistoryRequest'; import { OpenAiChatRequest } from '@src/models/api/OpenAiChatRequest'; -import { OpenAiClearRequest } from '@src/models/api/OpenAiClearRequest'; import { DefenceReport, ChatResponse, @@ -953,50 +951,3 @@ describe('handleAddInfoToChatHistory', () => { expect(req.session.levelState[0].chatHistory).toEqual(chatHistory); }); }); - -describe('handleClearChatHistory', () => { - function openAiClearRequestMock( - level?: LEVEL_NAMES, - chatHistory?: ChatMessage[] - ) { - return { - body: { - level: level ?? undefined, - }, - session: { - levelState: [ - { - chatHistory: chatHistory ?? [], - }, - ], - }, - } as OpenAiClearRequest; - } - - const chatHistory: ChatMessage[] = [ - { - completion: { role: 'system', content: 'You are a helpful chatbot' }, - chatMessageType: 'SYSTEM', - }, - { - completion: { role: 'assistant', content: 'Hello human' }, - chatMessageType: 'BOT', - }, - ]; - test('GIVEN valid level WHEN handleClearChatHistory called THEN it sets chatHistory to empty', () => { - const req = openAiClearRequestMock(LEVEL_NAMES.LEVEL_1, chatHistory); - const res = responseMock(); - handleClearChatHistory(req, res); - expect(req.session.levelState[0].chatHistory.length).toEqual(0); - }); - - test('GIVEN invalid level WHEN handleClearChatHistory called THEN returns 400 ', () => { - const req = openAiClearRequestMock(undefined, chatHistory); - - const res = responseMock(); - - handleClearChatHistory(req, res); - - expect(res.status).toHaveBeenCalledWith(400); - }); -}); diff --git a/backend/test/unit/controller/emailController.test.ts b/backend/test/unit/controller/emailController.test.ts deleted file mode 100644 index 2131b5b7f..000000000 --- a/backend/test/unit/controller/emailController.test.ts +++ /dev/null @@ -1,62 +0,0 @@ -import { expect, test, jest, describe } from '@jest/globals'; -import { Response } from 'express'; - -import { handleClearEmails } from '@src/controller/emailController'; -import { EmailClearRequest } from '@src/models/api/EmailClearRequest'; -import { EmailInfo } from '@src/models/email'; - -function responseMock() { - return { - send: jest.fn(), - status: jest.fn(), - } as unknown as Response; -} - -const emails: EmailInfo[] = [ - { - address: 'bob@scottlogic.com', - subject: 'Welcome to Scott Logic!', - body: 'Hi Bob, welcome to Scott Logic!', - }, - { - address: 'jane@scottlogic.com', - subject: 'Hello', - body: 'Hi Jane, welcome to Scott Logic!', - }, -]; - -describe('handleClearEmails', () => { - test('GIVEN valid level WHEN handleClearEmails called THEN sets emails to empty', () => { - const req = { - body: { - level: 0, - }, - session: { - levelState: [ - { - sentEmails: emails, - }, - ], - }, - } as EmailClearRequest; - - const res = responseMock(); - - handleClearEmails(req, res); - - expect(req.session.levelState[0].sentEmails).toEqual([]); - }); - - test('GIVEN level is missing WHEN handleClearEmails called THEN returns 400', () => { - const req = { - body: {}, - } as EmailClearRequest; - - const res = responseMock(); - - handleClearEmails(req, res); - - expect(res.status).toHaveBeenCalledWith(400); - expect(res.send).toHaveBeenCalled(); - }); -}); diff --git a/backend/test/unit/controller/resetController.test.ts b/backend/test/unit/controller/resetController.test.ts index edea05851..9d0c13e4b 100644 --- a/backend/test/unit/controller/resetController.test.ts +++ b/backend/test/unit/controller/resetController.test.ts @@ -1,9 +1,13 @@ -import { expect, test, jest, afterEach } from '@jest/globals'; +import { expect, test, jest, afterEach, describe } from '@jest/globals'; import { Response } from 'express'; -import { handleResetProgress } from '@src/controller/resetController'; +import { + handleResetLevel, + handleResetProgress, +} from '@src/controller/resetController'; import { defaultDefences } from '@src/defaultDefences'; import { LevelGetRequest } from '@src/models/api/LevelGetRequest'; +import { LevelResetRequest } from '@src/models/api/LevelResetRequest'; import { defaultChatModel } from '@src/models/chat'; import { LEVEL_NAMES } from '@src/models/level'; @@ -16,141 +20,218 @@ function responseMock() { } as unknown as Response; } -jest.mock('@src/defaultDefences', () => ({ - defaultDefences: 'DEFAULT_DEFENCES', -})); - -jest.mock('@src/models/chat', () => ({ - defaultChatModel: 'DEFAULT_CHAT_MODEL', -})); - -const mockGetInitialLevelStates = jest.fn(); - -jest.mock('@src/models/level', () => { - const originalModule = - jest.requireActual('@src/models/level'); - return { - ...originalModule, - getInitialLevelStates: () => mockGetInitialLevelStates(), - }; -}); - afterEach(() => { mockSend.mockClear(); }); -test.each(Object.values(LEVEL_NAMES))( - `GIVEN level [%s] WHEN client asks to reset all progress THEN game state is cleared AND the backend sends the correct level information`, - (level) => { +describe('reset progress', () => { + const mockGetInitialLevelStates = jest.fn(); + + jest.mock('@src/models/level', () => { + const originalModule = + jest.requireActual( + '@src/models/level' + ); + return { + ...originalModule, + getInitialLevelStates: () => mockGetInitialLevelStates(), + }; + }); + + jest.mock('@src/defaultDefences', () => ({ + defaultDefences: 'DEFAULT_DEFENCES', + })); + + jest.mock('@src/models/chat', () => ({ + defaultChatModel: 'DEFAULT_CHAT_MODEL', + })); + + test.each(Object.values(LEVEL_NAMES))( + `GIVEN level [%s] WHEN client asks to reset all progress THEN game state is cleared AND the backend sends the correct level information`, + (level) => { + const req = { + query: { + level, + }, + session: { + levelState: [ + { + sentEmails: 'level 1 emails', + chatHistory: 'level 1 chat history', + defences: 'level 1 defences', + }, + { + sentEmails: 'level 2 emails', + chatHistory: 'level 2 chat history', + defences: 'level 2 defences', + }, + { + sentEmails: 'level 3 emails', + chatHistory: 'level 3 chat history', + defences: 'level 3 defences', + }, + { + sentEmails: 'level 4 emails', + chatHistory: 'level 4 chat history', + defences: 'level 4 defences', + }, + ], + chatModel: 'chat model', + }, + } as unknown as LevelGetRequest; + const res = responseMock(); + + mockGetInitialLevelStates.mockReturnValue([ + { + sentEmails: [], + chatHistory: [], + defences: undefined, + }, + { + sentEmails: [], + chatHistory: [], + defences: undefined, + }, + { + sentEmails: [], + chatHistory: [], + defences: defaultDefences, + }, + { + sentEmails: [], + chatHistory: [], + defences: defaultDefences, + }, + ]); + + handleResetProgress(req, res); + + expect(mockSend).toHaveBeenCalledWith({ + emails: [], + chatHistory: [], + defences: + level === LEVEL_NAMES.LEVEL_1 || level === LEVEL_NAMES.LEVEL_2 + ? undefined + : defaultDefences, + chatModel: level === LEVEL_NAMES.SANDBOX ? defaultChatModel : undefined, + }); + } + ); + + test('WHEN client does not provide a level THEN the backend responds with BadRequest', () => { const req = { - query: { - level, - }, + query: {}, session: { levelState: [ + {}, { - sentEmails: 'level 1 emails', - chatHistory: 'level 1 chat history', - defences: 'level 1 defences', - }, - { - sentEmails: 'level 2 emails', - chatHistory: 'level 2 chat history', - defences: 'level 2 defences', - }, - { - sentEmails: 'level 3 emails', - chatHistory: 'level 3 chat history', - defences: 'level 3 defences', - }, - { - sentEmails: 'level 4 emails', - chatHistory: 'level 4 chat history', - defences: 'level 4 defences', + sentEmails: [], + chatHistory: [], + defences: [], }, ], - chatModel: 'chat model', }, } as unknown as LevelGetRequest; const res = responseMock(); - mockGetInitialLevelStates.mockReturnValue([ - { - sentEmails: [], - chatHistory: [], - defences: undefined, - }, - { - sentEmails: [], - chatHistory: [], - defences: undefined, - }, - { - sentEmails: [], - chatHistory: [], - defences: defaultDefences, - }, - { - sentEmails: [], - chatHistory: [], - defences: defaultDefences, + handleResetProgress(req, res); + + expect(res.status).toHaveBeenCalledWith(400); + expect(mockSend).toHaveBeenCalledWith('Level not provided'); + }); + + test('WHEN client provides an invalid level THEN the backend responds with BadRequest', () => { + const req = { + query: { level: 5 }, + session: { + levelState: [ + {}, + { + sentEmails: [], + chatHistory: [], + defences: [], + }, + ], }, - ]); + } as unknown as LevelGetRequest; + const res = responseMock(); handleResetProgress(req, res); - expect(mockSend).toHaveBeenCalledWith({ - emails: [], - chatHistory: [], - defences: - level === LEVEL_NAMES.LEVEL_1 || level === LEVEL_NAMES.LEVEL_2 - ? undefined - : defaultDefences, - chatModel: level === LEVEL_NAMES.SANDBOX ? defaultChatModel : undefined, - }); - } -); - -test('WHEN client does not provide a level THEN the backend responds with BadRequest', () => { - const req = { - query: {}, - session: { - levelState: [ - {}, - { - sentEmails: [], - chatHistory: [], - defences: [], + expect(res.status).toHaveBeenCalledWith(400); + expect(mockSend).toHaveBeenCalledWith('Invalid level'); + }); +}); + +describe('reset level', () => { + test.each(Object.values(LEVEL_NAMES))( + `GIVEN level [%s] WHEN client asks to reset level progress THEN sent messages and emails are cleared AND the backend sends an info message`, + (level) => { + const req = { + params: { + level, + }, + session: { + levelState: [ + { + sentEmails: 'level 1 emails', + chatHistory: 'level 1 chat history', + defences: 'level 1 defences', + }, + { + sentEmails: 'level 2 emails', + chatHistory: 'level 2 chat history', + defences: 'level 2 defences', + }, + { + sentEmails: 'level 3 emails', + chatHistory: 'level 3 chat history', + defences: 'level 3 defences', + }, + { + sentEmails: 'level 4 emails', + chatHistory: 'level 4 chat history', + defences: 'level 4 defences', + }, + ], + chatModel: 'chat model', }, - ], - }, - } as unknown as LevelGetRequest; - const res = responseMock(); + } as unknown as LevelResetRequest; + const res = responseMock(); - handleResetProgress(req, res); + handleResetLevel(req, res); - expect(res.status).toHaveBeenCalledWith(400); - expect(mockSend).toHaveBeenCalledWith('Level not provided'); -}); + expect(req.session.levelState[level].chatHistory).toEqual([]); + expect(req.session.levelState[level].sentEmails).toEqual([]); -test('WHEN client provides an invalid level THEN the backend responds with BadRequest', () => { - const req = { - query: { level: 5 }, - session: { - levelState: [ - {}, - { - sentEmails: [], - chatHistory: [], - defences: [], + expect(mockSend).toHaveBeenCalledWith({ + chatInfoMessage: { + infoMessage: 'Level progress reset', + chatMessageType: 'RESET_LEVEL', }, - ], - }, - } as unknown as LevelGetRequest; - const res = responseMock(); + }); + } + ); + + test('WHEN client provides an invalid level THEN the backend responds with BadRequest', () => { + const req = { + params: { level: 5 }, + session: { + levelState: [ + {}, + { + sentEmails: [], + chatHistory: [], + defences: [], + }, + ], + }, + } as unknown as LevelResetRequest; + const res = responseMock(); - handleResetProgress(req, res); + handleResetLevel(req, res); - expect(res.status).toHaveBeenCalledWith(400); - expect(mockSend).toHaveBeenCalledWith('Invalid level'); + expect(res.status).toHaveBeenCalledWith(400); + expect(mockSend).toHaveBeenCalledWith('Invalid level'); + }); }); diff --git a/frontend/src/components/MainComponent/MainComponent.tsx b/frontend/src/components/MainComponent/MainComponent.tsx index b8b162970..54cf4298e 100644 --- a/frontend/src/components/MainComponent/MainComponent.tsx +++ b/frontend/src/components/MainComponent/MainComponent.tsx @@ -17,7 +17,6 @@ import { LEVEL_NAMES, LevelSystemRole } from '@src/models/level'; import { chatService, defenceService, - emailService, levelService, resetService, startService, @@ -155,16 +154,11 @@ function MainComponent({ } } - // for clearing single level progress async function resetLevel() { - // reset on the backend - await Promise.all([ - chatService.clearChat(currentLevel), - emailService.clearEmails(currentLevel), - ]); + const chatInfoMessage = await resetService.resetLevelProgress(currentLevel); resetFrontendState(); - addResetMessage(); + setMessages((messages: ChatMessage[]) => [chatInfoMessage, ...messages]); } // for going switching level without clearing progress @@ -296,19 +290,6 @@ function MainComponent({ } } - function addResetMessage() { - const resetMessage: ChatMessage = { - message: `Level progress reset`, - type: 'RESET_LEVEL', - }; - void chatService.addInfoMessageToChatHistory( - resetMessage.message, - resetMessage.type, - currentLevel - ); - setMessages((messages: ChatMessage[]) => [resetMessage, ...messages]); - } - function setChatModelId(modelId: string) { if (!chatModel) { console.error( diff --git a/frontend/src/models/combined.ts b/frontend/src/models/apiResponse.ts similarity index 81% rename from frontend/src/models/combined.ts rename to frontend/src/models/apiResponse.ts index 0d303f8dd..4ec7ca582 100644 --- a/frontend/src/models/combined.ts +++ b/frontend/src/models/apiResponse.ts @@ -3,7 +3,7 @@ import { DefenceDTO } from './defence'; import { EmailInfo } from './email'; import { LevelSystemRole } from './level'; -type StartReponse = { +type StartResponse = { emails: EmailInfo[]; chatHistory: ChatMessageDTO[]; defences?: DefenceDTO[]; @@ -19,8 +19,8 @@ type LoadLevelResponse = { chatModel?: ChatModel; }; -type ConfigureDefenceResponse = { +type ChatInfoMessageResponse = { chatInfoMessage: ChatMessageDTO; }; -export type { StartReponse, LoadLevelResponse, ConfigureDefenceResponse }; +export type { StartResponse, LoadLevelResponse, ChatInfoMessageResponse }; diff --git a/frontend/src/service/chatService.ts b/frontend/src/service/chatService.ts index 7137ef0fe..becbfe5c9 100644 --- a/frontend/src/service/chatService.ts +++ b/frontend/src/service/chatService.ts @@ -11,17 +11,6 @@ import { sendRequest } from './backendService'; const PATH = 'openai/'; -async function clearChat(level: number) { - const response = await sendRequest(`${PATH}clear`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ level }), - }); - return response.status === 200; -} - async function sendMessage(message: string, currentLevel: LEVEL_NAMES) { const response = await sendRequest(`${PATH}chat`, { method: 'POST', @@ -104,7 +93,6 @@ async function addInfoMessageToChatHistory( } export { - clearChat, sendMessage, configureGptModel, setGptModel, diff --git a/frontend/src/service/defenceService.ts b/frontend/src/service/defenceService.ts index b867edf3b..2e58d5a23 100644 --- a/frontend/src/service/defenceService.ts +++ b/frontend/src/service/defenceService.ts @@ -1,6 +1,6 @@ import { DEFAULT_DEFENCES } from '@src/Defences'; +import { ChatInfoMessageResponse } from '@src/models/apiResponse'; import { ChatMessage } from '@src/models/chat'; -import { ConfigureDefenceResponse } from '@src/models/combined'; import { DEFENCE_ID, DefenceConfigItem, @@ -69,7 +69,7 @@ async function configureDefence( if (response.status !== 200) return null; const { chatInfoMessage } = - (await response.json()) as ConfigureDefenceResponse; + (await response.json()) as ChatInfoMessageResponse; return makeChatMessageFromDTO(chatInfoMessage); } diff --git a/frontend/src/service/emailService.ts b/frontend/src/service/emailService.ts deleted file mode 100644 index 29ccc8197..000000000 --- a/frontend/src/service/emailService.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { sendRequest } from './backendService'; - -const PATH = 'email/'; - -async function clearEmails(level: number): Promise { - const response = await sendRequest(`${PATH}clear`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ level }), - }); - return response.status === 200; -} - -export { clearEmails }; diff --git a/frontend/src/service/index.ts b/frontend/src/service/index.ts index 02b9ed9a4..7b17968f7 100644 --- a/frontend/src/service/index.ts +++ b/frontend/src/service/index.ts @@ -1,7 +1,6 @@ import * as chatService from './chatService'; import * as defenceService from './defenceService'; import * as documentService from './documentService'; -import * as emailService from './emailService'; import * as levelService from './levelService'; import * as resetService from './resetService'; import * as startService from './startService'; @@ -10,7 +9,6 @@ export { chatService, defenceService, documentService, - emailService, resetService, startService, levelService, diff --git a/frontend/src/service/levelService.ts b/frontend/src/service/levelService.ts index 4b8619759..325359edb 100644 --- a/frontend/src/service/levelService.ts +++ b/frontend/src/service/levelService.ts @@ -1,4 +1,4 @@ -import { LoadLevelResponse } from '@src/models/combined'; +import { LoadLevelResponse } from '@src/models/apiResponse'; import { sendRequest } from './backendService'; import { getChatMessagesFromDTOResponse } from './chatService'; diff --git a/frontend/src/service/resetService.ts b/frontend/src/service/resetService.ts index 46f87d212..440296052 100644 --- a/frontend/src/service/resetService.ts +++ b/frontend/src/service/resetService.ts @@ -1,13 +1,19 @@ -import { LoadLevelResponse } from '@src/models/combined'; +import { + LoadLevelResponse, + ChatInfoMessageResponse, +} from '@src/models/apiResponse'; import { sendRequest } from './backendService'; -import { getChatMessagesFromDTOResponse } from './chatService'; +import { + getChatMessagesFromDTOResponse, + makeChatMessageFromDTO, +} from './chatService'; import { getDefencesFromDTOs } from './defenceService'; const PATH = 'reset'; async function resetAllProgress(level: number) { - const response = await sendRequest(`${PATH}?level=${level}`, { + const response = await sendRequest(`${PATH}/all?level=${level}`, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -24,4 +30,17 @@ async function resetAllProgress(level: number) { }; } -export { resetAllProgress }; +async function resetLevelProgress(level: number) { + const response = await sendRequest(`${PATH}/${level}`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + }); + const { chatInfoMessage } = + (await response.json()) as ChatInfoMessageResponse; + + return makeChatMessageFromDTO(chatInfoMessage); +} + +export { resetAllProgress, resetLevelProgress }; diff --git a/frontend/src/service/startService.ts b/frontend/src/service/startService.ts index 55e5fb718..f6e61a420 100644 --- a/frontend/src/service/startService.ts +++ b/frontend/src/service/startService.ts @@ -1,4 +1,4 @@ -import { StartReponse } from '@src/models/combined'; +import { StartResponse } from '@src/models/apiResponse'; import { sendRequest } from './backendService'; import { getChatMessagesFromDTOResponse } from './chatService'; @@ -17,7 +17,7 @@ async function start(level: number) { chatHistory, systemRoles, chatModel, - } = (await response.json()) as StartReponse; + } = (await response.json()) as StartResponse; return { emails,