From 06f89c8fa37f1cf0d0b9a59d412316994eb747b7 Mon Sep 17 00:00:00 2001 From: Spencer Spenst Date: Mon, 2 Oct 2023 22:11:45 -0700 Subject: [PATCH] update calc_records on archive --- lib/initializeLocalDb.ts | 32 ++++++++++++++++++++++--- pages/api/archive/[id].ts | 23 +++++++++++++++--- tests/pages/api/archive/archive.test.ts | 11 ++------- 3 files changed, 51 insertions(+), 15 deletions(-) diff --git a/lib/initializeLocalDb.ts b/lib/initializeLocalDb.ts index c165db36e..560ecffe1 100644 --- a/lib/initializeLocalDb.ts +++ b/lib/initializeLocalDb.ts @@ -1,3 +1,4 @@ +import User from '@root/models/db/user'; import { AttemptContext } from '@root/models/schemas/playAttemptSchema'; import { PASSWORD_SALTROUNDS } from '@root/models/schemas/userSchema'; import bcrypt from 'bcryptjs'; @@ -339,11 +340,18 @@ export default async function initializeLocalDb() { await Promise.all(promises); } +// TODO: this should really have the same logic as api/publish so tests are consistent with real use cases export async function initLevel(userId: string, name: string, obj: Partial = {}, createReviews = true) { const ts = TimerUtil.getTs(); const id = new Types.ObjectId(); - const user = await UserModel.findById(userId, 'name'); + const user = await UserModel.findById(userId, 'name'); + + if (!user) { + throw new Error(`user ${userId} not found`); + } + const slug = await generateLevelSlug(user.name, name); + const leastMoves = 20; // based on name length create that many reviews const lvl = await LevelModel.create({ @@ -352,14 +360,32 @@ export async function initLevel(userId: string, name: string, obj: Partial { - // level is over 24hrs old, move to archive - const slug = await generateLevelSlug('archive', level.name, level._id.toString(), { session: session }); + const [record, slug] = await Promise.all([ + RecordModel.findOne({ levelId: level._id }, {}, { session: session }).sort({ ts: -1 }), + generateLevelSlug('archive', level.name, level._id.toString(), { session: session }), + ]); + + if (!record) { + throw new Error(`Record not found for level ${level._id}`); + } + + await Promise.all([ + LevelModel.findOneAndUpdate({ _id: id }, { $set: { + archivedBy: level.userId, + archivedTs: ts, + slug: slug, + userId: new Types.ObjectId(TestId.ARCHIVE), + } }, { new: true, session: session }), + UserModel.updateOne({ _id: record.userId }, { $inc: { calc_records: -1 } }, { session: session }), + ]); newLevel = await LevelModel.findOneAndUpdate({ _id: id }, { $set: { archivedBy: level.userId, diff --git a/tests/pages/api/archive/archive.test.ts b/tests/pages/api/archive/archive.test.ts index 747981c38..e684cc955 100644 --- a/tests/pages/api/archive/archive.test.ts +++ b/tests/pages/api/archive/archive.test.ts @@ -29,7 +29,7 @@ beforeAll(async () => { [userACollection, userBCollection, userALevel1] = await Promise.all([ initCollection(TestId.USER, 'user A collection'), initCollection(TestId.USER_B, 'user B collection'), - initLevel(TestId.USER, 'user A level 1') + initLevel(TestId.USER, 'user A level 1'), ]); }); enableFetchMocks(); @@ -50,7 +50,6 @@ describe('Testing archive', () => { query: { id: userALevel1._id, }, - headers: { 'content-type': 'application/json', }, @@ -82,7 +81,6 @@ describe('Testing archive', () => { query: { id: userALevel1._id, }, - headers: { 'content-type': 'application/json', }, @@ -97,7 +95,6 @@ describe('Testing archive', () => { expect(response.error).toBe('Internal server error'); expect(res.status).toBe(500); }, - }); }); test('Archiving one of the levels should remove it from all collections', async () => { @@ -111,7 +108,6 @@ describe('Testing archive', () => { query: { id: userALevel1._id, }, - headers: { 'content-type': 'application/json', }, @@ -131,6 +127,7 @@ describe('Testing archive', () => { const user = await UserModel.findById(TestId.USER); expect(user?.calc_levels_created_count).toEqual(2); + expect(user?.calc_records).toBe(1); const level = await LevelModel.findOne({ _id: userALevel1._id }); @@ -168,7 +165,6 @@ describe('Testing archive', () => { query: { id: new Types.ObjectId(), }, - headers: { 'content-type': 'application/json', }, @@ -183,7 +179,6 @@ describe('Testing archive', () => { expect(response.error).toBe('Level not found'); expect(res.status).toBe(404); }, - }); }); test('Archiving a level that does not belong to you should fail', async () => { @@ -197,7 +192,6 @@ describe('Testing archive', () => { query: { id: TestId.LEVEL_3, }, - headers: { 'content-type': 'application/json', }, @@ -212,7 +206,6 @@ describe('Testing archive', () => { expect(response.error).toBe('Not authorized to delete this Level'); expect(res.status).toBe(401); }, - }); }); });