Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/domain/notification/create/insertData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import type { DataModel } from '../types';

export default async function insertData(data: DataModel): Promise<string>
{
return database.createRecord(RECORD_TYPE, { ...data });
return database.createRecord(RECORD_TYPE, { ...data, deleted: false });
}
17 changes: 17 additions & 0 deletions src/domain/notification/getByPostId/getByPostId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

import database from '^/integrations/database';

import type { RecordQuery } from '^/integrations/database';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The import should be grouped with the other ^/integration/... import.

import { RECORD_TYPE } from '../definitions';
import type { DataModel } from '../types';

export default async function getByPostId(postId: string): Promise<DataModel[]>
{
const query: RecordQuery =
{
deleted: { EQUALS: false },
postId: { EQUALS: postId }
};

return database.searchRecords(RECORD_TYPE, query) as Promise<DataModel[]>;
}
2 changes: 2 additions & 0 deletions src/domain/notification/getByPostId/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

export { default } from './getByPostId';
3 changes: 2 additions & 1 deletion src/domain/notification/getRecent/getRecent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

import type { RecordQuery, RecordSort} from '^/integrations/database';
import type { RecordQuery, RecordSort } from '^/integrations/database';
import database, { SortDirections } from '^/integrations/database';

import { RECORD_TYPE } from '../definitions';
Expand All @@ -9,6 +9,7 @@ export default async function getRecent(receiverId: string, limit: number, offse
{
const query: RecordQuery =
{
deleted: { EQUALS: false },
receiverId: { EQUALS: receiverId }
};

Expand Down
10 changes: 10 additions & 0 deletions src/domain/notification/notify/removedPost.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

import getNotifications from '../getByPostId';
import deleteData from '../remove';

export default async function removedPost(postId: string): Promise<Promise<void>[]>
{
const notifications = await getNotifications(postId);

return notifications.map(item => deleteData(item.id));
Copy link
Member

@petermasking petermasking Mar 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return notifications.map(item => deleteData(item.id));
const promises = notifications.map(item => deleteData(item.id));
await Promise.allSettled(promises);

Return type: Promise<void>

}
3 changes: 3 additions & 0 deletions src/domain/notification/notify/subscriptions.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@

import { subscribe as subscribeToPostCreated } from '^/domain/post/create';
import { subscribe as subscribeToPostRemoved } from '^/domain/post/remove';
import { subscribe as subscribeToPostRated } from '^/domain/rating/toggle';
import { subscribe as subscribeToRelationEstablished } from '^/domain/relation/establish';

import reactedToPost from './createdPost';
import ratedPost from './ratedPost';
import removedPost from './removedPost';
import startedFollowing from './startedFollowing';

async function subscribe(): Promise<void>
Expand All @@ -13,6 +15,7 @@ async function subscribe(): Promise<void>
subscribeToPostRated(({ creatorId, postId, rated }) => ratedPost(creatorId, postId, rated)),
subscribeToPostCreated(({ creatorId, postId, parentId }) => reactedToPost(creatorId, postId, parentId)),
subscribeToRelationEstablished(({ followerId, followingId }) => startedFollowing(followerId, followingId)),
subscribeToPostRemoved(({ postId }) => removedPost(postId)),
]);
}

Expand Down
9 changes: 9 additions & 0 deletions src/domain/notification/remove/deleteData.ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file contains the actual feature, and therefore should be named remove.ts.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

import database from '^/integrations/database';

import { RECORD_TYPE } from '../definitions';

export default async function deleteData(id: string): Promise<void>
{
return database.updateRecord(RECORD_TYPE, id, { deleted: true });
}
2 changes: 2 additions & 0 deletions src/domain/notification/remove/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

export { default } from './deleteData';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export { default } from './deleteData';
export { default } from './remove';

2 changes: 1 addition & 1 deletion test/domain/notification/fixtures/databases.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async function withCreatorsPostsAndNotifications(): Promise<void>
RECORDS.POSTS.map(post => database.createRecord(POST_RECORD_TYPE, { ...post })),
RECORDS.POST_METRICS.map(postMetric => database.createRecord(POST_METRICS_RECORD_TYPE, { ...postMetric })),
RECORDS.RATINGS.map(rating => database.createRecord(RATING_RECORD_TYPE, { rating })),
RECORDS.NOTIFICATIONS.map(notification => database.createRecord(NOTIFICATION_RECORD_TYPE, { ...notification }))
RECORDS.NOTIFICATIONS.map(notification => database.createRecord(NOTIFICATION_RECORD_TYPE, { ...notification, deleted: false }))
];

await Promise.all(promises.flat());
Expand Down
15 changes: 8 additions & 7 deletions test/domain/notification/fixtures/records.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { DataModel as ComicDataModel } from '^/domain/comic';
import type { DataModel as CreatorDataModel } from '^/domain/creator';
import type { DataModel as CreatorMetricsDataModel } from '^/domain/creator.metrics';
import type { DataModel as ImageDataModel } from '^/domain/image';
import type { DataModel as NotificationDataModel} from '^/domain/notification';
import type { DataModel as NotificationDataModel } from '^/domain/notification';
import { Types } from '^/domain/notification';
import type { DataModel as PostDataModel } from '^/domain/post';
import type { DataModel as PostMetricsModel } from '^/domain/post.metrics';
Expand Down Expand Up @@ -56,12 +56,13 @@ const RATINGS: RatingDataModel[] = [
{ id: VALUES.IDS.RATING2, createdAt: new Date().toISOString(), creatorId: VALUES.IDS.CREATOR2, postId: VALUES.IDS.REACTION_LIKED }
];

const NOTIFICATIONS: NotificationDataModel[] = [
{ id: VALUES.IDS.NOTIFICATION1, createdAt: new Date().toISOString(), type: Types.STARTED_FOLLOWING, senderId: VALUES.IDS.CREATOR1, receiverId: VALUES.IDS.CREATOR2, postId: undefined },
{ id: VALUES.IDS.NOTIFICATION2, createdAt: new Date().toISOString(), type: Types.STARTED_FOLLOWING, senderId: VALUES.IDS.CREATOR2, receiverId: VALUES.IDS.CREATOR1, postId: undefined },
{ id: VALUES.IDS.NOTIFICATION3, createdAt: new Date('01-05-2024').toISOString(), type: Types.RATED_POST, senderId: VALUES.IDS.CREATOR3, receiverId: VALUES.IDS.CREATOR2, postId: VALUES.IDS.POST_RATED },
{ id: VALUES.IDS.NOTIFICATION4, createdAt: new Date('01-04-2024').toISOString(), type: Types.RATED_POST, senderId: VALUES.IDS.CREATOR2, receiverId: VALUES.IDS.CREATOR1, postId: VALUES.IDS.REACTION_LIKED },
{ id: VALUES.IDS.NOTIFICATION5, createdAt: new Date('01-03-2024').toISOString(), type: Types.RATED_POST, senderId: VALUES.IDS.CREATOR1, receiverId: VALUES.IDS.CREATOR1, postId: VALUES.IDS.POST_DELETED }
const NOTIFICATIONS: (NotificationDataModel & { deleted: boolean; })[] = [
{ id: VALUES.IDS.NOTIFICATION1, createdAt: new Date().toISOString(), type: Types.STARTED_FOLLOWING, senderId: VALUES.IDS.CREATOR1, receiverId: VALUES.IDS.CREATOR2, postId: undefined, deleted: false },
{ id: VALUES.IDS.NOTIFICATION2, createdAt: new Date().toISOString(), type: Types.STARTED_FOLLOWING, senderId: VALUES.IDS.CREATOR2, receiverId: VALUES.IDS.CREATOR1, postId: undefined, deleted: false },
{ id: VALUES.IDS.NOTIFICATION3, createdAt: new Date('01-05-2024').toISOString(), type: Types.RATED_POST, senderId: VALUES.IDS.CREATOR3, receiverId: VALUES.IDS.CREATOR2, postId: VALUES.IDS.POST_RATED, deleted: false },
{ id: VALUES.IDS.NOTIFICATION6, createdAt: new Date().toISOString(), type: Types.REACTED_TO_POST, senderId: VALUES.IDS.CREATOR1, receiverId: VALUES.IDS.CREATOR3, postId: VALUES.IDS.REACTION_LIKED, deleted: false },
{ id: VALUES.IDS.NOTIFICATION4, createdAt: new Date('01-04-2024').toISOString(), type: Types.RATED_POST, senderId: VALUES.IDS.CREATOR2, receiverId: VALUES.IDS.CREATOR1, postId: VALUES.IDS.REACTION_LIKED, deleted: false },
{ id: VALUES.IDS.NOTIFICATION5, createdAt: new Date('01-03-2024').toISOString(), type: Types.RATED_POST, senderId: VALUES.IDS.CREATOR1, receiverId: VALUES.IDS.CREATOR1, postId: VALUES.IDS.POST_DELETED, deleted: false }
];

export const RECORDS: Record<string, RecordData[]> = { CREATORS, CREATOR_METRICS, RELATIONS, IMAGES, COMICS, POSTS, POST_METRICS, RATINGS, NOTIFICATIONS };
3 changes: 2 additions & 1 deletion test/domain/notification/fixtures/values.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export const VALUES =
NOTIFICATION2: 'N2',
NOTIFICATION3: 'N3',
NOTIFICATION4: 'N4',
NOTIFICATION5: 'N5'
NOTIFICATION5: 'N5',
NOTIFICATION6: 'N6'
},

STORAGE_KEYS: {
Expand Down
26 changes: 26 additions & 0 deletions test/domain/notification/removedPost.spec.ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file tests the remove feature of the notification. The name of the file should reflect this.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

import { beforeEach, describe, expect, it } from 'vitest';

import getByPostId from '^/domain/notification/getByPostId';
import removedPost from '^/domain/notification/notify/removedPost';
import { DATABASES, VALUES } from './fixtures';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be an empty line before this import. Those above are coming from the domain, and this one from ./


beforeEach(async () =>
{
await Promise.all([
DATABASES.withCreatorsPostsAndNotifications()
]);
});

describe('domain/notification/remove', () =>
{
it('should remove all notifications of a removed post', async () =>
{
await removedPost(VALUES.IDS.POST_RATED);

const result = await getByPostId(VALUES.IDS.POST_RATED);

expect(result).toHaveLength(0);
});

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This empty line is not required

});