Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 });
}
8 changes: 7 additions & 1 deletion src/domain/notification/getById/getById.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@

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

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

export default async function getById(id: string): Promise<DataModel>
{
return database.readRecord(RECORD_TYPE, id) as Promise<DataModel>;
const query: RecordQuery =
{
id: { EQUALS: id },
deleted: { EQUALS: false }
};
return database.findRecord(RECORD_TYPE, query) as Promise<DataModel>;
}
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 type { RecordQuery } from '^/integrations/database';
import database from '^/integrations/database';

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
11 changes: 11 additions & 0 deletions src/domain/notification/notify/removedPost.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

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

export default async function removedPost(postId: string): Promise<void>
{
const notifications = await getNotifications(postId);
const promises = notifications.map(item => remove(item.id));

await Promise.allSettled(promises);
}
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
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 './remove';
9 changes: 9 additions & 0 deletions src/domain/notification/remove/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 remove(id: string): Promise<void>
{
return database.updateRecord(RECORD_TYPE, id, { deleted: true });
}
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
25 changes: 25 additions & 0 deletions test/domain/notification/removePostNotifications.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

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';

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);
});
});