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
100 changes: 100 additions & 0 deletions __tests__/alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,3 +429,103 @@ describe('mutation clearOpportunityAlert', () => {
expect(alerts.opportunityId).toBeNull();
});
});

describe('mutation updateHasSeenOpportunity', () => {
const MUTATION = (hasSeenOpportunity?: boolean) => /* GraphQL */ `
mutation UpdateHasSeenOpportunity {
updateHasSeenOpportunity${hasSeenOpportunity !== undefined ? `(hasSeenOpportunity: ${hasSeenOpportunity})` : ''} {
_
}
}
`;

it('should not authorize when not logged in', () =>
testMutationErrorCode(client, { mutation: MUTATION() }, 'UNAUTHENTICATED'));

it('should set hasSeenOpportunity flag to true by default', async () => {
loggedUser = '1';

await con.getRepository(Alerts).save(
con.getRepository(Alerts).create({
userId: '1',
flags: { hasSeenOpportunity: false },
}),
);

const res = await client.mutate(MUTATION());
expect(res.errors).toBeFalsy();
const alerts = await con.getRepository(Alerts).findOneBy({ userId: '1' });
expect(alerts.flags.hasSeenOpportunity).toBe(true);
});

it('should set hasSeenOpportunity flag to true when explicitly passed', async () => {
loggedUser = '1';

await con.getRepository(Alerts).save(
con.getRepository(Alerts).create({
userId: '1',
flags: { hasSeenOpportunity: false },
}),
);

const res = await client.mutate(MUTATION(true));
expect(res.errors).toBeFalsy();
const alerts = await con.getRepository(Alerts).findOneBy({ userId: '1' });
expect(alerts.flags.hasSeenOpportunity).toBe(true);
});

it('should set hasSeenOpportunity flag to false when passed', async () => {
loggedUser = '1';

await con.getRepository(Alerts).save(
con.getRepository(Alerts).create({
userId: '1',
flags: { hasSeenOpportunity: true },
}),
);

const res = await client.mutate(MUTATION(false));
expect(res.errors).toBeFalsy();
const alerts = await con.getRepository(Alerts).findOneBy({ userId: '1' });
expect(alerts.flags.hasSeenOpportunity).toBe(false);
});

it('should preserve existing flags when updating hasSeenOpportunity', async () => {
loggedUser = '1';

const lastReferralDate = new Date('2023-02-05 12:00:00');
await con.getRepository(Alerts).save(
con.getRepository(Alerts).create({
userId: '1',
flags: {
hasSeenOpportunity: false,
lastReferralReminder: lastReferralDate,
},
}),
);

const res = await client.mutate(MUTATION(true));
expect(res.errors).toBeFalsy();
const alerts = await con.getRepository(Alerts).findOneBy({ userId: '1' });
expect(alerts.flags.hasSeenOpportunity).toBe(true);
// JSONB stores dates as ISO strings
expect(alerts.flags.lastReferralReminder).toEqual(
lastReferralDate.toISOString(),
);
});

it('should work when alerts record does not have flags set', async () => {
loggedUser = '1';

await con.getRepository(Alerts).save(
con.getRepository(Alerts).create({
userId: '1',
}),
);

const res = await client.mutate(MUTATION());
expect(res.errors).toBeFalsy();
const alerts = await con.getRepository(Alerts).findOneBy({ userId: '1' });
expect(alerts.flags.hasSeenOpportunity).toBe(true);
});
});
21 changes: 21 additions & 0 deletions src/schema/alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { AuthContext, BaseContext, Context } from '../Context';
import { DataSource, QueryRunner } from 'typeorm';
import { insertOrIgnoreAction } from './actions';
import { GQLEmptyResponse } from './common';
import { updateFlagsStatement } from '../common';

interface GQLAlerts {
filter: boolean;
Expand Down Expand Up @@ -189,6 +190,13 @@ export const typeDefs = /* GraphQL */ `
Reset opportunity alert
"""
clearOpportunityAlert: EmptyResponse! @auth

"""
Update the hasSeenOpportunity flag
"""
updateHasSeenOpportunity(
hasSeenOpportunity: Boolean = true
): EmptyResponse! @auth
}

extend type Query {
Expand Down Expand Up @@ -314,6 +322,19 @@ export const resolvers: IResolvers<unknown, BaseContext> = traceResolvers<
await updateAlerts(ctx.con, ctx.userId, { opportunityId: null });
return { _: true };
},
updateHasSeenOpportunity: async (
_,
{ hasSeenOpportunity = true }: { hasSeenOpportunity?: boolean },
ctx: AuthContext,
): Promise<GQLEmptyResponse> => {
await ctx.con.getRepository(Alerts).update(
{ userId: ctx.userId },
{
flags: updateFlagsStatement<Alerts>({ hasSeenOpportunity }),
},
);
return { _: true };
},
},
Query: {
userAlerts: (_, __, ctx: Context): Promise<GQLAlerts> | GQLAlerts =>
Expand Down
Loading