diff --git a/pages/api/user/index.ts b/pages/api/user/index.ts index c26c687e4..8d7e75a2f 100644 --- a/pages/api/user/index.ts +++ b/pages/api/user/index.ts @@ -286,6 +286,8 @@ export default withAuth({ GraphModel.deleteMany({ $or: [{ source: req.user._id }, { target: req.user._id }] }, { session: session }), // delete in keyvaluemodel where key contains userId KeyValueModel.deleteMany({ key: { $regex: `.*${req.user._id}.*` } }, { session: session }), + // delete draft levels + LevelModel.updateMany({ userId: req.user._id, isDraft: true }, { $set: { isDeleted: true } }, { session: session }), NotificationModel.deleteMany({ $or: [ { source: req.user._id }, { target: req.user._id }, diff --git a/tests/pages/api/user/user-delete.test.ts b/tests/pages/api/user/user-delete.test.ts index 7983e899f..99a524e3c 100644 --- a/tests/pages/api/user/user-delete.test.ts +++ b/tests/pages/api/user/user-delete.test.ts @@ -2,7 +2,7 @@ import TestId from '@root/constants/testId'; import dbConnect, { dbDisconnect } from '@root/lib/dbConnect'; import { getTokenCookieValue } from '@root/lib/getTokenCookie'; import { NextApiRequestWithAuth } from '@root/lib/withAuth'; -import { CommentModel, UserModel } from '@root/models/mongoose'; +import { CommentModel, LevelModel, UserModel } from '@root/models/mongoose'; import { cancelSubscription, stripe } from '@root/pages/api/subscription'; import { enableFetchMocks } from 'jest-fetch-mock'; import mongoose, { Types } from 'mongoose'; @@ -240,4 +240,57 @@ describe('pages/api/collection/index.ts', () => { }, }); }); + test('Deleting levels', async () => { + // draft levels should be marked as deleted + // published levels should be moved to archive + + const levels = await LevelModel.find({ userId: TestId.USER }, {}, { sort: { _id: 1 } }); + + expect(levels.length).toBe(4); + expect(levels[0].isDraft).toBe(false); + expect(levels[1].isDraft).toBe(true); + expect(levels[2].isDraft).toBe(false); + expect(levels[3].isDeleted).toBe(true); + + const archiveLevels = await LevelModel.find({ userId: TestId.ARCHIVE }, {}, { sort: { _id: 1 } }); + + expect(archiveLevels.length).toBe(0); + + await testApiHandler({ + pagesHandler: async (_, res) => { + const req: NextApiRequestWithAuth = { + method: 'DELETE', + cookies: { + token: cookie, + }, + headers: { + 'content-type': 'application/json', + }, + } as unknown as NextApiRequestWithAuth; + + await modifyUserHandler(req, res); + }, + test: async ({ fetch }) => { + const res = await fetch(); + const response = await res.json(); + + expect(response.error).toBeUndefined(); + expect(response.updated).toBe(true); + expect(res.status).toBe(200); + + const userLevels = await LevelModel.find({ userId: TestId.USER }, {}, { sort: { _id: 1 } }); + + expect(userLevels.length).toBe(2); + expect(userLevels[0].isDraft).toBeTruthy(); + expect(userLevels[0].isDeleted).toBeTruthy(); + expect(userLevels[1].isDeleted).toBeTruthy(); + + const archiveLevels = await LevelModel.find({ userId: TestId.ARCHIVE }, {}, { sort: { _id: 1 } }); + + expect(archiveLevels.length).toBe(2); + expect(archiveLevels[0].slug).toBe('archive/test-level-1'); + expect(archiveLevels[1].slug).toBe('archive/x'); + }, + }); + }); });