-
Notifications
You must be signed in to change notification settings - Fork 5
/
campaign.js
171 lines (152 loc) · 4.33 KB
/
campaign.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
'use strict';
const dateFns = require('date-fns');
const logger = require('winston');
const Promise = require('bluebird');
const helpers = require('../helpers');
const contentful = require('../contentful');
const phoenix = require('../phoenix');
const config = require('../../config/lib/helpers/campaign');
/**
* @param {Number} campaignId
* @return {Promise}
*/
function fetchById(campaignId) {
return phoenix.fetchCampaignById(campaignId)
.then(res => module.exports.parseCampaign(res.data))
.then(campaign => helpers.cache.campaigns.set(campaignId, campaign));
}
/**
* @param {Number} campaignId
* @return {Promise}
*/
function fetchCampaignConfigByCampaignId(campaignId) {
const query = {
content_type: 'campaign',
'fields.campaignId': campaignId,
};
return contentful.fetchEntries(query)
.then(res => module.exports.parseCampaignConfig(res.data[0]))
.then(campaignConfig => helpers.cache.campaignConfigs.set(campaignId, campaignConfig));
}
/**
* @param {Number} campaignId
* @param {Boolean} resetCache
* @return {Promise}
*/
async function getById(campaignId, resetCache = false) {
if (resetCache === true) {
logger.debug('Campaign cache reset', { campaignId });
return module.exports.fetchById(campaignId);
}
const data = await helpers.cache.campaigns.get(campaignId);
if (data) {
logger.debug('Campaign cache hit', { campaignId });
return data;
}
logger.debug('Campaign cache miss', { campaignId });
return module.exports.fetchById(campaignId);
}
/**
* @param {Number} campaignId
* @param {Boolean} resetCache
* @return {Promise}
*/
async function getCampaignConfigByCampaignId(campaignId, resetCache = false) {
if (resetCache === true) {
logger.debug('Campaign config cache reset', { campaignId });
return module.exports.fetchCampaignConfigByCampaignId(campaignId);
}
const data = await helpers.cache.campaignConfigs.get(campaignId);
if (data) {
logger.debug('Campaign config cache hit', { campaignId });
return data;
}
logger.debug('Campaign config cache miss', { campaignId });
return module.exports.fetchCampaignConfigByCampaignId(campaignId);
}
/**
* @param {Object} contentfulEntry
* @return {Promise}
*/
async function parseCampaignConfig(contentfulEntry) {
if (!contentfulEntry) {
return {};
}
const data = {
id: contentful.getContentfulIdFromContentfulEntry(contentfulEntry),
templates: {
webSignup: {},
},
};
const webSignupEntry = contentfulEntry.fields.webSignup;
if (!webSignupEntry) {
return data;
}
data.templates.webSignup = await helpers.contentfulEntry
.getMessageTemplate(webSignupEntry, 'webSignup');
return data;
}
module.exports = {
fetchById,
fetchCampaignConfigByCampaignId,
getById,
getByIds: function fetchByIds(campaignIds) {
const promises = [];
campaignIds.forEach(campaignId => promises.push(this.getById(campaignId)));
return Promise.all(promises);
},
getCampaignConfigByCampaignId,
/**
* @param {Object} campaign
* @return {Boolean}
*/
hasEnded: function hasEnded(campaign) {
const endDate = dateFns.parse(campaign.endDate.date);
return dateFns.isPast(endDate);
},
/**
* @param {Object} campaign
* @return {Boolean}
*/
isClosed: function isClosed(campaign) {
if (campaign.status) {
return this.isClosedStatus(campaign);
}
if (!campaign.endDate) {
return false;
}
return this.hasEnded(campaign);
},
isClosedStatus: function isClosedStatus(campaign) {
return campaign.status === config.statuses.closed;
},
/**
* @param {Object} campaign
* @return {String}
*/
parseStatus: function parseStatus(campaign) {
if (this.isClosed(campaign)) {
return config.statuses.closed;
}
return config.statuses.active;
},
/**
* @param {Object} campaign
* @return {Object}
*/
parseCampaign: function parseCampaign(campaign) {
const campaignId = Number(campaign.legacyCampaignId);
logger.debug('parseCampaign', { campaignId });
const result = {
id: campaignId,
title: campaign.title,
tagline: campaign.tagline,
status: this.parseStatus(campaign),
currentCampaignRun: { id: Number(campaign.legacyCampaignRunId) },
endDate: campaign.endDate,
};
logger.debug('parseCampaign', { result });
return result;
},
parseCampaignConfig,
};