Skip to content

Commit

Permalink
feat(api): update campaignManagementRepository to handle external-id …
Browse files Browse the repository at this point in the history
…feature
  • Loading branch information
lionelB committed Oct 9, 2024
1 parent 2ae5391 commit fd59813
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class CampaignManagement {
code,
name,
idPixLabel,
idPixType,
createdAt,
archivedAt,
deletedAt,
Expand Down Expand Up @@ -35,6 +36,7 @@ class CampaignManagement {
this.name = name;
this.type = type;
this.idPixLabel = idPixLabel;
this.idPixType = idPixType;
this.createdAt = createdAt;
this.archivedAt = archivedAt;
this.deletedAt = deletedAt;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { knex } from '../../../../../db/knex-database-connection.js';
import { CAMPAIGN_FEATURES } from '../../../../shared/domain/constants.js';
import { fetchPage } from '../../../../shared/infrastructure/utils/knex-utils.js';
import { CampaignParticipationStatuses, CampaignTypes } from '../../../shared/domain/constants.js';
import { CampaignManagement } from '../../domain/models/CampaignManagement.js';
Expand All @@ -11,7 +12,6 @@ const get = async function (campaignId) {
id: 'campaigns.id',
code: 'campaigns.code',
name: 'campaigns.name',
idPixLabel: 'campaigns.idPixLabel',
isForAbsoluteNovice: 'campaigns.isForAbsoluteNovice',
createdAt: 'campaigns.createdAt',
archivedAt: 'campaigns.archivedAt',
Expand Down Expand Up @@ -40,8 +40,19 @@ const get = async function (campaignId) {
.where('campaigns.id', campaignId)
.first();

const externalIdFeature = await knex('campaign-features')
.select('params')
.join('features', 'features.id', 'featureId')
.where({ campaignId: campaign.id, 'features.key': CAMPAIGN_FEATURES.EXTERNAL_ID.key })
.first();

const participationCountByStatus = await _countParticipationsByStatus(campaignId, campaign.type);
campaign = { ...campaign, ...participationCountByStatus };
campaign = {
...campaign,
...participationCountByStatus,
idPixLabel: externalIdFeature?.params?.label,
idPixType: externalIdFeature?.params?.type,
};
const campaignManagement = new CampaignManagement(campaign);
return campaignManagement;
};
Expand All @@ -52,7 +63,6 @@ const findPaginatedCampaignManagements = async function ({ organizationId, page
id: 'campaigns.id',
code: 'campaigns.code',
name: 'campaigns.name',
idPixLabel: 'campaigns.idPixLabel',
createdAt: 'campaigns.createdAt',
archivedAt: 'campaigns.archivedAt',
deletedAt: 'campaigns.deletedAt',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import _ from 'lodash';

import * as campaignManagementRepository from '../../../../../../src/prescription/campaign/infrastructure/repositories/campaign-management-repository.js';
import {
CampaignExternalIdTypes,
CampaignParticipationStatuses,
CampaignTypes,
} from '../../../../../../src/prescription/shared/domain/constants.js';
import { CAMPAIGN_FEATURES } from '../../../../../../src/shared/domain/constants.js';
import { databaseBuilder, expect } from '../../../../../test-helper.js';

const { SHARED, TO_SHARE, STARTED } = CampaignParticipationStatuses;
Expand All @@ -24,6 +26,63 @@ describe('Integration | Repository | Campaign-Management', function () {
organizationId: organization.id,
isForAbsoluteNovice: true,
});
const featureId = databaseBuilder.factory.buildFeature(CAMPAIGN_FEATURES.EXTERNAL_ID).id;
databaseBuilder.factory.buildCampaignFeature({
featureId,
campaignId: campaign.id,
params: { label: 'Id externe', type: CampaignExternalIdTypes.STRING },
});
await databaseBuilder.commit();

// when
const result = await campaignManagementRepository.get(campaign.id);

// then
expect(result).to.deep.include({
id: campaign.id,
name: campaign.name,
code: campaign.code,
type: campaign.type,
idPixLabel: 'Id externe',
idPixType: CampaignExternalIdTypes.STRING,
createdAt: campaign.createdAt,
archivedAt: campaign.archivedAt,
creatorFirstName: user.firstName,
creatorLastName: user.lastName,
creatorId: user.id,
ownerId: owner.id,
ownerFirstName: owner.firstName,
ownerLastName: owner.lastName,
organizationId: organization.id,
organizationName: organization.name,
targetProfileId: targetProfile.id,
targetProfileName: targetProfile.name,
title: campaign.title,
isForAbsoluteNovice: true,
customLandingPageText: campaign.customLandingPageText,
customResultPageText: null,
customResultPageButtonText: null,
customResultPageButtonUrl: null,
sharedParticipationsCount: 0,
totalParticipationsCount: 0,
multipleSendings: false,
});
});

it('should return campaign details without external id campaign feature', async function () {
// given
const user = databaseBuilder.factory.buildUser();
const owner = databaseBuilder.factory.buildUser();
const targetProfile = databaseBuilder.factory.buildTargetProfile();
const organization = databaseBuilder.factory.buildOrganization({});
const campaign = databaseBuilder.factory.buildCampaign({
creatorId: user.id,
ownerId: owner.id,
targetProfileId: targetProfile.id,
organizationId: organization.id,
isForAbsoluteNovice: true,
});

await databaseBuilder.commit();

// when
Expand All @@ -35,7 +94,6 @@ describe('Integration | Repository | Campaign-Management', function () {
name: campaign.name,
code: campaign.code,
type: campaign.type,
idPixLabel: campaign.idPixLabel,
createdAt: campaign.createdAt,
archivedAt: campaign.archivedAt,
creatorFirstName: user.firstName,
Expand Down Expand Up @@ -83,7 +141,6 @@ describe('Integration | Repository | Campaign-Management', function () {
name: campaign.name,
code: campaign.code,
type: campaign.type,
idPixLabel: campaign.idPixLabel,
createdAt: campaign.createdAt,
archivedAt: campaign.archivedAt,
creatorFirstName: user.firstName,
Expand Down Expand Up @@ -288,7 +345,6 @@ describe('Integration | Repository | Campaign-Management', function () {
id: campaign.id,
name: campaign.name,
code: campaign.code,
idPixLabel: campaign.idPixLabel,
createdAt: campaign.createdAt,
archivedAt: campaign.archivedAt,
deletedAt: campaign.deletedAt,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,74 @@
import { CampaignManagement } from '../../../../../../src/prescription/campaign/domain/models/CampaignManagement.js';
import { CampaignTypes } from '../../../../../../src/prescription/shared/domain/constants.js';
import { expect } from '../../../../../test-helper.js';

describe('CampaignManagement', function () {
it('returns correct object', function () {
const input = {
id: 1,
code: 'code',
name: 'name',
type: CampaignTypes.PROFILES_COLLECTION,
idPixLabel: 'idPixLabel',
idPixType: 'idPixType',
createdAt: new Date(2020, 10, 23),
archivedAt: new Date(2021, 10, 23),
deletedAt: null,
creatorLastName: 'creatorLastName',
creatorFirstName: 'creatorFirstName',
creatorId: 123,
organizationId: 456,
organizationName: 'organizationName',
targetProfileId: 678,
targetProfileName: 'targetProfileName',
isForAbsoluteNovice: false,
title: 'title',
customLandingPageText: 'customLandingPageText',
customResultPageText: 'customResultPageText',
customResultPageButtonText: 'customResultPageButtonText',
customResultPageButtonUrl: 'customResultPageButtonUrl',
ownerLastName: 'ownerLastName',
ownerFirstName: 'ownerFirstName',
ownerId: 234,
multipleSendings: 'multipleSendings',
shared: 5,
started: 3,
completed: 2,
};
const campaignManagement = new CampaignManagement(input);

expect(campaignManagement).to.deep.equal({
id: 1,
code: 'code',
name: 'name',
type: CampaignTypes.PROFILES_COLLECTION,
idPixLabel: 'idPixLabel',
idPixType: 'idPixType',
createdAt: new Date(2020, 10, 23),
archivedAt: new Date(2021, 10, 23),
deletedAt: null,
creatorLastName: 'creatorLastName',
creatorFirstName: 'creatorFirstName',
creatorId: 123,
organizationId: 456,
organizationName: 'organizationName',
targetProfileId: 678,
targetProfileName: 'targetProfileName',
isForAbsoluteNovice: false,
title: 'title',
customLandingPageText: 'customLandingPageText',
customResultPageText: 'customResultPageText',
customResultPageButtonText: 'customResultPageButtonText',
customResultPageButtonUrl: 'customResultPageButtonUrl',
ownerLastName: 'ownerLastName',
ownerFirstName: 'ownerFirstName',
ownerId: 234,
multipleSendings: 'multipleSendings',
sharedParticipationsCount: 5,
totalParticipationsCount: 10,
});
});

describe('#totalParticipationsCount', function () {
it('returns total participations count', function () {
const campaignManagement = new CampaignManagement({
Expand Down

0 comments on commit fd59813

Please sign in to comment.