Skip to content
Merged

to main #1051

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 package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "web-jam-back",
"version": "2.11.13",
"version": "2.11.14",
"description": "web-jam.com",
"type": "module",
"main": "build/src/index.js",
Expand Down
16 changes: 14 additions & 2 deletions src/model/outreach/outreach-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -792,9 +792,21 @@ class OutreachController extends Controller {
// require + validate targetWeekend before calling resolvePitch, so `tw` is
// always present here when skipDedup is false. A legacy record with no
// targetWeekend can never match (both clauses require the field to exist) —
// legacy records don't block. Returns the error envelope to relay, or null.
// legacy records don't block.
//
// #1050 / #1046: Only active records sent or created within OUTREACH_COOLDOWN_DAYS
// (7 days) count as blocking; older campaigns allow re-pitching for the weekend.
// Returns the error envelope to relay, or null.
async dedupGuard(venueId: string | undefined, tw: TargetWeekend | null): Promise<AuthzError | null> {
const query: Record<string, unknown> = { venueId, status: { $in: ACTIVE_STATUSES } };
const cooldownThreshold = new Date(Date.now() - OUTREACH_COOLDOWN_DAYS * 24 * 60 * 60 * 1000);
const query: Record<string, unknown> = {
venueId,
status: { $in: ACTIVE_STATUSES },
$or: [
{ sentAt: { $gte: cooldownThreshold } },
{ created_at: { $gte: cooldownThreshold } },
],
};
if (tw) Object.assign(query, targetWeekendOverlapClause(tw));
let dupe;
try {
Expand Down
26 changes: 26 additions & 0 deletions test/unit/outreach/outreach-controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2417,4 +2417,30 @@ describe('Outreach Controller (#844 batch model)', () => {
expect(sendArgs.html).not.toContain('perfect fit');
});
});

describe('dedupGuard 7-day cooldown (#1046)', () => {
it('allows re-pitching when an active outreach record for the weekend was sent > 7 days ago', async () => {
// Mock findOne to return null (because the query with cooldown threshold filters out old records)
c.model.findOne = vi.fn((q: any) => {
if (q.$or) return Promise.resolve(null);
return Promise.resolve({ _id: oid(), status: 'sent', sentAt: new Date(Date.now() - 8 * 24 * 60 * 60 * 1000) });
});

const dupeErr = await c.dedupGuard(oid(), VALID_WEEKEND);
expect(dupeErr).toBeNull();
expect(c.model.findOne).toHaveBeenCalled();
const query = (c.model.findOne as any).mock.calls[0][0];
expect(query.$or).toBeDefined();
});

it('blocks re-pitching when an active outreach record was sent <= 7 days ago', async () => {
const recentDoc = { _id: oid(), status: 'sent', sentAt: new Date(Date.now() - 2 * 24 * 60 * 60 * 1000) };
c.model.findOne = vi.fn(() => Promise.resolve(recentDoc));

const dupeErr = await c.dedupGuard(oid(), VALID_WEEKEND);
expect(dupeErr).not.toBeNull();
expect(dupeErr?.status).toBe(409);
expect(dupeErr?.message).toContain('active outreach already exists');
});
});
});
Loading