-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9419 from weseek/imprv/shorten-thread-deletion-ex…
…pired-at imprv(ai): Shorten thread deletion expiredAt
- Loading branch information
Showing
7 changed files
with
93 additions
and
6 deletions.
There are no files selected for viewing
7 changes: 3 additions & 4 deletions
7
apps/app/src/features/openai/server/models/thread-relation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
apps/app/src/features/openai/server/services/normalize-data/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './normalize-thread-relation-expired-at'; |
1 change: 1 addition & 0 deletions
1
...tures/openai/server/services/normalize-data/normalize-thread-relation-expired-at/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './normalize-thread-relation-expired-at'; |
70 changes: 70 additions & 0 deletions
70
...e-data/normalize-thread-relation-expired-at/normalize-thread-relation-expired-at.integ.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { faker } from '@faker-js/faker'; | ||
import { addDays, subDays } from 'date-fns'; | ||
import { Types } from 'mongoose'; | ||
|
||
import ThreadRelation from '../../../models/thread-relation'; | ||
|
||
import { MAX_DAYS_UNTIL_EXPIRATION, normalizeExpiredAtForThreadRelations } from './normalize-thread-relation-expired-at'; | ||
|
||
describe('normalizeExpiredAtForThreadRelations', () => { | ||
|
||
it('should update expiredAt to 3 days from now for expired thread relations', async() => { | ||
// arrange | ||
const expiredDays = faker.number.int({ min: MAX_DAYS_UNTIL_EXPIRATION, max: 30 }); | ||
const expiredDate = addDays(new Date(), expiredDays); | ||
const threadRelation = new ThreadRelation({ | ||
userId: new Types.ObjectId(), | ||
threadId: 'test-thread', | ||
expiredAt: expiredDate, | ||
}); | ||
await threadRelation.save(); | ||
|
||
// act | ||
await normalizeExpiredAtForThreadRelations(); | ||
|
||
// assert | ||
const updatedThreadRelation = await ThreadRelation.findById(threadRelation._id); | ||
expect(updatedThreadRelation).not.toBeNull(); | ||
assert(updatedThreadRelation?.expiredAt != null); | ||
expect(updatedThreadRelation.expiredAt < addDays(new Date(), MAX_DAYS_UNTIL_EXPIRATION)).toBeTruthy(); | ||
}); | ||
|
||
it('should not update expiredAt for non-expired thread relations', async() => { | ||
// arrange | ||
const nonExpiredDays = faker.number.int({ min: 0, max: MAX_DAYS_UNTIL_EXPIRATION }); | ||
const nonExpiredDate = addDays(new Date(), nonExpiredDays); | ||
const threadRelation = new ThreadRelation({ | ||
userId: new Types.ObjectId(), | ||
threadId: 'test-thread-2', | ||
expiredAt: nonExpiredDate, | ||
}); | ||
await threadRelation.save(); | ||
|
||
// act | ||
await normalizeExpiredAtForThreadRelations(); | ||
|
||
// assert | ||
const updatedThreadRelation = await ThreadRelation.findById(threadRelation._id); | ||
expect(updatedThreadRelation).not.toBeNull(); | ||
expect(updatedThreadRelation?.expiredAt).toEqual(nonExpiredDate); | ||
}); | ||
|
||
it('should not update expiredAt is before today', async() => { | ||
// arrange | ||
const nonExpiredDate = subDays(new Date(), 1); | ||
const threadRelation = new ThreadRelation({ | ||
userId: new Types.ObjectId(), | ||
threadId: 'test-thread-3', | ||
expiredAt: nonExpiredDate, | ||
}); | ||
await threadRelation.save(); | ||
|
||
// act | ||
await normalizeExpiredAtForThreadRelations(); | ||
|
||
// assert | ||
const updatedThreadRelation = await ThreadRelation.findById(threadRelation._id); | ||
expect(updatedThreadRelation).not.toBeNull(); | ||
expect(updatedThreadRelation?.expiredAt).toEqual(nonExpiredDate); | ||
}); | ||
}); |
14 changes: 14 additions & 0 deletions
14
...rmalize-data/normalize-thread-relation-expired-at/normalize-thread-relation-expired-at.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { addDays } from 'date-fns'; | ||
|
||
import ThreadRelation from '../../../models/thread-relation'; | ||
|
||
export const MAX_DAYS_UNTIL_EXPIRATION = 3; | ||
|
||
export const normalizeExpiredAtForThreadRelations = async(): Promise<void> => { | ||
const maxDaysExpiredAt = addDays(new Date(), MAX_DAYS_UNTIL_EXPIRATION); | ||
|
||
await ThreadRelation.updateMany( | ||
{ expiredAt: { $gt: maxDaysExpiredAt } }, | ||
{ $set: { expiredAt: maxDaysExpiredAt } }, | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters