Yuki Takei 1 год назад
Родитель
Сommit
43930d1e2e

+ 1 - 0
apps/app/src/features/openai/server/services/normalize-data/index.ts

@@ -0,0 +1 @@
+export * from './normalize-thread-relation-expired-at';

+ 1 - 0
apps/app/src/features/openai/server/services/normalize-data/normalize-thread-relation-expired-at/index.ts

@@ -0,0 +1 @@
+export * from './normalize-thread-relation-expired-at';

+ 70 - 0
apps/app/src/features/openai/server/services/normalize-data/normalize-thread-relation-expired-at/normalize-thread-relation-expired-at.integ.ts

@@ -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 - 0
apps/app/src/features/openai/server/services/normalize-data/normalize-thread-relation-expired-at/normalize-thread-relation-expired-at.ts

@@ -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 } },
+  );
+};

+ 2 - 0
apps/app/src/server/service/normalize-data/index.ts

@@ -1,3 +1,4 @@
+import { normalizeExpiredAtForThreadRelations } from '~/features/openai/server/services/normalize-data';
 import loggerFactory from '~/utils/logger';
 import loggerFactory from '~/utils/logger';
 
 
 import { convertRevisionPageIdToObjectId } from './convert-revision-page-id-to-objectid';
 import { convertRevisionPageIdToObjectId } from './convert-revision-page-id-to-objectid';
@@ -8,6 +9,7 @@ const logger = loggerFactory('growi:service:NormalizeData');
 export const normalizeData = async(): Promise<void> => {
 export const normalizeData = async(): Promise<void> => {
   await renameDuplicateRootPages();
   await renameDuplicateRootPages();
   await convertRevisionPageIdToObjectId();
   await convertRevisionPageIdToObjectId();
+  await normalizeExpiredAtForThreadRelations();
 
 
   logger.info('normalizeData has been executed');
   logger.info('normalizeData has been executed');
   return;
   return;