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
39 changes: 36 additions & 3 deletions __tests__/workers/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -923,55 +923,88 @@ describe('post boost action', () => {
expect(registeredWorker).toBeDefined();
});

it('should add first milestone notification for the user sent to the worker', async () => {
it('should add first milestone notification for the user sent to the worker and not clear campaignId', async () => {
const worker = await import(
'../../src/workers/notifications/postBoostAction'
);

// Set up post with campaignId in flags
await con
.getRepository(Post)
.update({ id: 'p1' }, { flags: { campaignId: 'tmp-campaign' } });

const actual = await invokeNotificationWorker(worker.default, {
userId: '1',
postId: 'p1',
campaignId: 'tmp-campaign',
action: 'first_milestone',
});

expect(actual).toBeDefined();
expect(actual!.length).toEqual(1);
const bundle = actual![0];
const ctx = bundle.ctx as NotificationBoostContext;
expect(bundle.type).toEqual('post_boost_first_milestone');
expect(ctx.campaignId).toEqual('tmp-campaign');
expect(ctx.user.id).toEqual('1');

// Verify that campaignId is NOT cleared from the database
const updatedPost = await con.getRepository(Post).findOneBy({ id: 'p1' });
expect(updatedPost?.flags?.campaignId).toEqual('tmp-campaign');
});

it('should add completed notification for the user sent to the worker', async () => {
it('should add completed notification for the user sent to the worker and clear campaignId', async () => {
const worker = await import(
'../../src/workers/notifications/postBoostAction'
);

// Set up post with campaignId in flags
await con
.getRepository(Post)
.update({ id: 'p1' }, { flags: { campaignId: 'tmp-campaign' } });

const actual = await invokeNotificationWorker(worker.default, {
userId: '1',
postId: 'p1',
campaignId: 'tmp-campaign',
action: 'completed',
});

expect(actual).toBeDefined();
expect(actual!.length).toEqual(1);
const bundle = actual![0];
const ctx = bundle.ctx as NotificationBoostContext;
expect(bundle.type).toEqual('post_boost_completed');
expect(ctx.campaignId).toEqual('tmp-campaign');
expect(ctx.user.id).toEqual('1');

// Verify that campaignId IS cleared from the database
const updatedPost = await con.getRepository(Post).findOneBy({ id: 'p1' });
expect(updatedPost?.flags?.campaignId).toBeNull();
});

it('should not add notification for cancelled action', async () => {
it('should not add notification for cancelled action and not clear campaignId', async () => {
const worker = await import(
'../../src/workers/notifications/postBoostAction'
);

// Set up post with campaignId in flags
await con
.getRepository(Post)
.update({ id: 'p1' }, { flags: { campaignId: 'tmp-campaign' } });

const actual = await invokeNotificationWorker(worker.default, {
userId: '1',
postId: 'p1',
campaignId: 'tmp-campaign',
action: 'cancelled',
});

expect(actual).toBeFalsy();

// Verify that campaignId is NOT cleared from the database for cancelled actions
const updatedPost = await con.getRepository(Post).findOneBy({ id: 'p1' });
expect(updatedPost?.flags?.campaignId).toEqual('tmp-campaign');
});
});

Expand Down
13 changes: 6 additions & 7 deletions src/workers/notifications/postBoostAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@ const worker = generateTypedNotificationWorker<'skadi.v1.campaign-updated'>({
handler: async (params, con) => {
const { userId, postId, campaignId, action } = params;

await con
.getRepository(Post)
.update(
{ id: postId },
{ flags: updateFlagsStatement<Post>({ campaignId: null }) },
);

const user = await queryReadReplica(con, ({ queryRunner }) => {
return queryRunner.manager
.getRepository(User)
Expand All @@ -33,6 +26,12 @@ const worker = generateTypedNotificationWorker<'skadi.v1.campaign-updated'>({
case 'first_milestone':
return [{ type: NotificationType.PostBoostFirstMilestone, ctx }];
case 'completed':
await con
.getRepository(Post)
.update(
{ id: postId },
{ flags: updateFlagsStatement<Post>({ campaignId: null }) },
);
return [{ type: NotificationType.PostBoostCompleted, ctx }];
default:
return;
Expand Down
Loading