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
50 changes: 42 additions & 8 deletions __tests__/cron/cleanZombieOpportunities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ import { randomUUID } from 'node:crypto';
import { OpportunityJob } from '../../src/entity/opportunities/OpportunityJob';
import { subDays } from 'date-fns';
import { Organization } from '../../src/entity/Organization';
import { Opportunity } from '../../src/entity/opportunities/Opportunity';
import { DatasetLocation } from '../../src/entity/dataset/DatasetLocation';
import {
ClaimableItem,
ClaimableItemTypes,
} from '../../src/entity/ClaimableItem';
import { Opportunity } from '../../src/entity/opportunities/Opportunity';
import { OpportunityState } from '@dailydotdev/schema';

let con: DataSource;

Expand All @@ -33,38 +38,63 @@ beforeEach(async () => {
opportunitiesFixture.map((opportunity) => {
return {
...opportunity,
state: OpportunityState.DRAFT,
id: randomUUID(),
};
}),
);

await saveFixtures(
con,
OpportunityJob,
const opps1 = await con.getRepository(OpportunityJob).save(
opportunitiesFixture.map((opportunity) => {
return {
...opportunity,
state: OpportunityState.DRAFT,
id: randomUUID(),
organizationId: null,
flags: { anonUserId: randomUUID() },
createdAt: subDays(new Date(), 3),
};
}),
);

await saveFixtures(
con,
OpportunityJob,
ClaimableItem,
opps1.map((opportunity) => {
return {
identifier: randomUUID(),
type: ClaimableItemTypes.Opportunity,
flags: {
opportunityId: opportunity.id,
},
};
}),
);

const opps2 = await con.getRepository(OpportunityJob).save(
opportunitiesFixture.map((opportunity) => {
return {
...opportunity,
state: OpportunityState.DRAFT,
id: randomUUID(),
organizationId: null,
flags: { anonUserId: randomUUID() },
createdAt: subDays(new Date(), 1),
};
}),
);

await saveFixtures(
con,
ClaimableItem,
opps2.map((opportunity) => {
return {
identifier: randomUUID(),
type: ClaimableItemTypes.Opportunity,
flags: {
opportunityId: opportunity.id,
},
};
}),
);
});

describe('cleanZombieOpportunities cron', () => {
Expand All @@ -79,10 +109,14 @@ describe('cleanZombieOpportunities cron', () => {

expect(opportunities.length).toEqual(15);

const claimableItems = await con.getRepository(ClaimableItem).find();

const zombieOpportunitiesCount = opportunities.filter((opportunity) => {
return (
!(opportunity as OpportunityJob).organizationId &&
opportunity.flags?.anonUserId &&
claimableItems.some(
(item) => item.flags.opportunityId === opportunity.id,
) &&
opportunity.createdAt < subDays(new Date(), 2)
);
}).length;
Expand Down
20 changes: 10 additions & 10 deletions __tests__/paddle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1222,10 +1222,10 @@ describe('plus subscription', () => {

const claimableItem = await con
.getRepository(ClaimableItem)
.findOneByOrFail({ email: mockCustomer.email });
.findOneByOrFail({ identifier: mockCustomer.email });

expect(claimableItem).toBeTruthy();
expect(claimableItem.email).toBe('test@example.com');
expect(claimableItem.identifier).toBe('test@example.com');
expect(claimableItem.type).toBe(ClaimableItemTypes.Plus);
expect(claimableItem.flags).toHaveProperty(
'cycle',
Expand Down Expand Up @@ -1259,7 +1259,7 @@ it('should throw an error if the email already has a claimable subscription', as
});

await con.getRepository(ClaimableItem).save({
email: 'test@example.com',
identifier: 'test@example.com',
type: ClaimableItemTypes.Plus,
flags: {
cycle: SubscriptionCycles.Yearly,
Expand All @@ -1279,7 +1279,7 @@ it('should not throw an error if the email has claimed a previously claimable su
.mockResolvedValue(mockCustomer as Customer);

await con.getRepository(ClaimableItem).save({
email: 'test@example.com',
identifier: 'test@example.com',
type: ClaimableItemTypes.Plus,
flags: {
cycle: SubscriptionCycles.Yearly,
Expand Down Expand Up @@ -1310,10 +1310,10 @@ describe('anonymous subscription', () => {

const claimableItem = await con
.getRepository(ClaimableItem)
.findOneByOrFail({ email: mockCustomer.email });
.findOneByOrFail({ identifier: mockCustomer.email });

expect(claimableItem).toBeTruthy();
expect(claimableItem.email).toBe('test@example.com');
expect(claimableItem.identifier).toBe('test@example.com');
expect(claimableItem.type).toBe(ClaimableItemTypes.Plus);
expect(claimableItem.flags).toHaveProperty(
'cycle',
Expand Down Expand Up @@ -1346,7 +1346,7 @@ describe('anonymous subscription', () => {
});

await con.getRepository(ClaimableItem).save({
email: 'test@example.com',
identifier: 'test@example.com',
type: ClaimableItemTypes.Plus,
flags: {
status: SubscriptionStatus.Active,
Expand All @@ -1369,7 +1369,7 @@ describe('anonymous subscription', () => {
.mockResolvedValue(mockCustomer as Customer);

await con.getRepository(ClaimableItem).save({
email: 'test@example.com',
identifier: 'test@example.com',
type: ClaimableItemTypes.Plus,
flags: {
status: SubscriptionStatus.Active,
Expand All @@ -1393,7 +1393,7 @@ describe('anonymous subscription', () => {
const mockCustomer = { email: 'test@example.com' };

await con.getRepository(ClaimableItem).save({
email: 'test@example.com',
identifier: 'test@example.com',
type: ClaimableItemTypes.Plus,
flags: {
status: SubscriptionStatus.Active,
Expand All @@ -1418,7 +1418,7 @@ describe('anonymous subscription', () => {

const claimableItem = await con
.getRepository(ClaimableItem)
.findOneBy({ email: mockCustomer.email });
.findOneBy({ identifier: mockCustomer.email });

expect(claimableItem).toBeNull();
});
Expand Down
27 changes: 19 additions & 8 deletions __tests__/private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { generateShortId } from '../src/ids';
import { OpportunityUser } from '../src/entity/opportunities/user';
import { OpportunityUserType } from '../src/entity/opportunities/types';
import { QuestionFeedback } from '../src/entity/questions/QuestionFeedback';
import { ClaimableItem, ClaimableItemTypes } from '../src/entity/ClaimableItem';

jest.mock('../src/common/geo', () => ({
...(jest.requireActual('../src/common/geo') as Record<string, unknown>),
Expand Down Expand Up @@ -793,18 +794,23 @@ describe('POST /p/newUser', () => {
title: 'Test',
tldr: 'Test',
state: OpportunityState.DRAFT,
flags: {
anonUserId,
},
}),
);

await con.getRepository(ClaimableItem).save({
identifier: anonUserId,
type: ClaimableItemTypes.Opportunity,
flags: {
opportunityId: opportunity.id,
},
});

const { body } = await request(app.server)
.post('/p/newUser')
.set('Content-type', 'application/json')
.set('authorization', `Service ${process.env.ACCESS_SECRET}`)
.send({
id: opportunity.flags.anonUserId,
id: anonUserId,
name: anonUserId,
image: usersFixture[0].image,
username: anonUserId,
Expand All @@ -816,10 +822,15 @@ describe('POST /p/newUser', () => {
expect(body.status).toEqual('ok');
expect(body.userId).toEqual(anonUserId);

const updatedOpportunity = await con
.getRepository(OpportunityJob)
.findOneBy({ id: opportunity.id });
expect(updatedOpportunity!.flags.anonUserId).toBeNull();
const updatedClaimableItem = await con
.getRepository(ClaimableItem)
.findOneBy({
identifier: anonUserId,
type: ClaimableItemTypes.Opportunity,
});
expect(updatedClaimableItem).not.toBeNull();
expect(updatedClaimableItem!.claimedAt).toBeInstanceOf(Date);
expect(updatedClaimableItem!.claimedById).toBe(anonUserId);

const opportunityUser = await con.getRepository(OpportunityUser).findOneBy({
opportunityId: opportunity.id,
Expand Down
Loading
Loading