From 53e6c356f33937b7194db9fbd1fedf36401afeca Mon Sep 17 00:00:00 2001 From: Mike Schennum Date: Mon, 7 Apr 2025 09:03:30 -0700 Subject: [PATCH 1/5] update 14 day logic for new users --- src/model/UserDataSource.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/model/UserDataSource.ts b/src/model/UserDataSource.ts index 8eb1f40c..c3d51efc 100644 --- a/src/model/UserDataSource.ts +++ b/src/model/UserDataSource.ts @@ -141,12 +141,17 @@ export default class UserDataSource extends MongoDataSource { } const usernameInfo = rs?.usernameInfo + const accountCreatedAt = rs?.createdAt ?? new Date() + console.log('🚀 ~ UserDataSource ~ rs?.createdAt:', rs?.createdAt) if (username != null && username !== usernameInfo?.username) { - const lastUpdated = usernameInfo?.updatedAt ?? new Date() + const accountAgeInDays = UserDataSource.calculateLastUpdatedInDays(accountCreatedAt) + const lastUpdated = usernameInfo?.updatedAt ?? new Date(accountAgeInDays) const lastUpdatedInDays = - UserDataSource.calculateLastUpdatedInDays(lastUpdated) - if (lastUpdatedInDays < USERNAME_UPDATE_WAITING_IN_DAYS) { + UserDataSource.calculateLastUpdatedInDays(new Date(lastUpdated)) + const withinFirst14Days = accountAgeInDays < 14 + + if (!withinFirst14Days && lastUpdatedInDays < USERNAME_UPDATE_WAITING_IN_DAYS) { const daysToWait = USERNAME_UPDATE_WAITING_IN_DAYS - lastUpdatedInDays throw new Error( `Too frequent update. Please wait ${daysToWait.toString()} more days.` From 636618d93cf63b5fd4c3e5e896f34dc5737d2b04 Mon Sep 17 00:00:00 2001 From: Mike Schennum Date: Mon, 7 Apr 2025 17:46:51 -0700 Subject: [PATCH 2/5] fix spaceing remove console.log --- src/model/UserDataSource.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/model/UserDataSource.ts b/src/model/UserDataSource.ts index c3d51efc..e6056c80 100644 --- a/src/model/UserDataSource.ts +++ b/src/model/UserDataSource.ts @@ -142,7 +142,6 @@ export default class UserDataSource extends MongoDataSource { const usernameInfo = rs?.usernameInfo const accountCreatedAt = rs?.createdAt ?? new Date() - console.log('🚀 ~ UserDataSource ~ rs?.createdAt:', rs?.createdAt) if (username != null && username !== usernameInfo?.username) { const accountAgeInDays = UserDataSource.calculateLastUpdatedInDays(accountCreatedAt) @@ -154,7 +153,7 @@ export default class UserDataSource extends MongoDataSource { if (!withinFirst14Days && lastUpdatedInDays < USERNAME_UPDATE_WAITING_IN_DAYS) { const daysToWait = USERNAME_UPDATE_WAITING_IN_DAYS - lastUpdatedInDays throw new Error( - `Too frequent update. Please wait ${daysToWait.toString()} more days.` + `Too frequent update. Please wait ${daysToWait.toString()} more days.` ) } From 99f6de204d414b15102275e24b892c05c01af35d Mon Sep 17 00:00:00 2001 From: Mike Schennum Date: Wed, 9 Apr 2025 14:46:47 -0700 Subject: [PATCH 3/5] update test for enforce waiting period --- src/model/__tests__/UserDataSource.ts | 57 +++++++++++++-------------- 1 file changed, 28 insertions(+), 29 deletions(-) diff --git a/src/model/__tests__/UserDataSource.ts b/src/model/__tests__/UserDataSource.ts index 1a63bef9..4189942d 100644 --- a/src/model/__tests__/UserDataSource.ts +++ b/src/model/__tests__/UserDataSource.ts @@ -122,41 +122,40 @@ describe('UserDataSource', () => { email: 'cat@example.com' } + // Initial user creation await users.createOrUpdateUserProfile(updater, input) - await expect( - users.createOrUpdateUserProfile(updater, { - userUuid: input.userUuid, - username: 'woof1234' - }) - ).rejects.toThrowError(/frequent update/i) - }) - - it('should allow username update after the waiting period', async () => { - const updater = muuid.v4() - const userUuid = muuid.v4() - const input: UpdateProfileGQLInput = { - userUuid: userUuid.toUUID().toString(), - username: 'winnie', - email: 'cat@example.com' - } - - await users.createOrUpdateUserProfile(updater, input) + // First update should be allowed (username 'woof1234') + await users.createOrUpdateUserProfile(updater, { + userUuid: input.userUuid, + username: 'woof1234' + }) - jest - .spyOn(UserDataSource, 'calculateLastUpdatedInDays') - .mockImplementation(() => 14) + // Mock implementation with a function + // that correctly tracks call count + let callCount = 0 + jest.spyOn(UserDataSource, 'calculateLastUpdatedInDays') + .mockImplementation(() => { + // For both calls in the third update operation: + // First call (account age) - return 15 days (older than 14 days) + // Second call (last username update) - return 1 day (very recent) + callCount++ + return callCount % 2 === 1 ? 15 : 1 + }) - const newInput: UpdateProfileGQLInput = { - userUuid: input.userUuid, - username: 'pooh', - bio: 'I\'m a bear' + // Try the update operation that should fail + let errorThrown = false + try { + await users.createOrUpdateUserProfile(updater, { + userUuid: input.userUuid, + username: 'bark1234' + }) + } catch (error) { + errorThrown = true + expect(error.message).toMatch(/Too frequent update/i) } - await users.createOrUpdateUserProfile(updater, newInput) - - const updatedUser = await users.getUserPublicProfileByUuid(muuid.from(newInput.userUuid)) - expect(updatedUser?.username).toEqual(newInput.username) + expect(errorThrown).toBe(true) }) it('should reject invalid website url', async () => { From 47f0755692b860f65d6dc1e30b30e3ed516886cf Mon Sep 17 00:00:00 2001 From: Mike Schennum Date: Wed, 9 Apr 2025 15:00:21 -0700 Subject: [PATCH 4/5] update code comment --- src/model/__tests__/UserDataSource.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/model/__tests__/UserDataSource.ts b/src/model/__tests__/UserDataSource.ts index 4189942d..2eff0c32 100644 --- a/src/model/__tests__/UserDataSource.ts +++ b/src/model/__tests__/UserDataSource.ts @@ -143,7 +143,7 @@ describe('UserDataSource', () => { return callCount % 2 === 1 ? 15 : 1 }) - // Try the update operation that should fail + // Try the 3rd update operation that should fail let errorThrown = false try { await users.createOrUpdateUserProfile(updater, { From ea3b23cd2e6f2fa8c11b7788d692643b4556fa4d Mon Sep 17 00:00:00 2001 From: Mike Schennum Date: Wed, 9 Apr 2025 17:04:42 -0700 Subject: [PATCH 5/5] update comment on test --- src/model/__tests__/UserDataSource.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/model/__tests__/UserDataSource.ts b/src/model/__tests__/UserDataSource.ts index 2eff0c32..9d6cc57c 100644 --- a/src/model/__tests__/UserDataSource.ts +++ b/src/model/__tests__/UserDataSource.ts @@ -136,14 +136,14 @@ describe('UserDataSource', () => { let callCount = 0 jest.spyOn(UserDataSource, 'calculateLastUpdatedInDays') .mockImplementation(() => { - // For both calls in the third update operation: + // For both calls in the third update operation: // First call (account age) - return 15 days (older than 14 days) // Second call (last username update) - return 1 day (very recent) callCount++ return callCount % 2 === 1 ? 15 : 1 }) - // Try the 3rd update operation that should fail + // Try the update operation that should fail let errorThrown = false try { await users.createOrUpdateUserProfile(updater, {