Switches to using list IDs when calculating estimated size (#366)

This commit is contained in:
Chris Anderson 2024-02-29 22:25:54 -06:00 committed by GitHub
parent 1a9b5934f1
commit 7356ce04e0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 39 additions and 3 deletions

View file

@ -33,7 +33,7 @@ You can do a one-click deploy on Render using the button below:
[![Deploy to Render](https://render.com/images/deploy-to-render-button.svg)](https://render.com/deploy?repo=https://github.com/parcelvoy/platform)
Make sure to the `BASE_URL` to the URL of the web server.
Make sure to set the `BASE_URL` environment variable to the URL of the web server.
### Docker Compose

View file

@ -421,6 +421,6 @@ export const campaignPreview = async (project: Project, campaign: Campaign) => {
}
export const estimatedSendSize = async (campaign: Campaign) => {
const lists: List[] = await List.query().whereIn('id', campaign.lists ?? [])
const lists: List[] = await List.query().whereIn('id', campaign.list_ids ?? [])
return lists.reduce((acc, list) => (list.users_count ?? 0) + acc, 0)
}

View file

@ -6,9 +6,10 @@ import { createSubscription, subscribe } from '../../subscriptions/SubscriptionS
import { User } from '../../users/User'
import { uuid } from '../../utilities'
import Campaign, { CampaignSend, SentCampaign } from '../Campaign'
import { allCampaigns, createCampaign, getCampaign, sendCampaign, generateSendList } from '../CampaignService'
import { allCampaigns, createCampaign, getCampaign, sendCampaign, generateSendList, estimatedSendSize } from '../CampaignService'
import { createProvider } from '../../providers/ProviderRepository'
import { createTestProject } from '../../projects/__tests__/ProjectTestHelpers'
import ListStatsJob from '../../lists/ListStatsJob'
afterEach(() => {
jest.clearAllMocks()
@ -237,4 +238,39 @@ describe('CampaignService', () => {
expect(updatedCampaign?.state).toEqual('scheduled')
})
})
describe('estimatedSendSize', () => {
test('send size is equal to combination of all lists', async () => {
const params = await createCampaignDependencies()
const list1 = await createList(params.project_id, {
name: uuid(),
type: 'static',
is_visible: true,
})
const list2 = await createList(params.project_id, {
name: uuid(),
type: 'static',
is_visible: true,
})
const campaign = await createTestCampaign(params, {
list_ids: [list1.id, list2.id],
send_at: new Date(),
}) as SentCampaign
for (let i = 0; i < 20; i++) {
const user = await createUser(params.project_id)
await addUserToList(user, list1)
await addUserToList(user, list2)
await subscribe(user.id, params.subscription_id)
}
await ListStatsJob.handler({ listId: list1.id, projectId: params.project_id })
await ListStatsJob.handler({ listId: list2.id, projectId: params.project_id })
const sendSize = await estimatedSendSize(campaign)
expect(sendSize).toEqual(40)
})
})
})