agrs-sequelize-sdk 1.2.99 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/migrations/add-requested-from-dashboard-to-articles.js +17 -17
- package/models/AdAccountValues.js +25 -25
- package/models/AdHistory.js +30 -30
- package/models/AdPerformance.js +94 -94
- package/models/AdSet.js +289 -289
- package/models/AdSetHistory.js +30 -30
- package/models/AdsetPerformance.js +126 -126
- package/models/Article.js +190 -190
- package/models/AutomationRule.js +161 -127
- package/models/Buyers.js +25 -25
- package/models/Campaign.js +157 -157
- package/models/CampaignActionHistory.js +86 -86
- package/models/CampaignCreationLog.js +309 -309
- package/models/CampaignHistory.js +33 -33
- package/models/Channel.js +55 -55
- package/models/Domain.js +25 -25
- package/models/DynamicFeed.js +212 -212
- package/models/ExplorAdsChannel.js +61 -61
- package/models/Feed.js +33 -33
- package/models/FrontStoryChannel.js +59 -59
- package/models/MineChannel.js +42 -42
- package/models/Pages.js +81 -81
- package/models/PipelineExecution.js +59 -59
- package/models/Presets.js +34 -34
- package/models/RSOCFeedCampaign.js +357 -357
- package/models/RsocKeywordPerformance.js +110 -110
- package/models/RuleAction.js +90 -90
- package/models/RuleCondition.js +98 -98
- package/models/RuleExecution.js +107 -107
- package/models/RulesValues.js +56 -56
- package/models/SupportedLocale.js +23 -23
- package/models/SyncHistory.js +249 -249
- package/models/Tier2_AdAccounts.js +110 -110
- package/models/Tier2_Assets.js +70 -70
- package/models/Tier2_BusinessManagers.js +105 -105
- package/models/Tier2_CreditLines.js +99 -99
- package/models/Tier2_Pages.js +91 -91
- package/models/Tier2_Pixels.js +82 -82
- package/models/Tier2_Tokens.js +64 -64
- package/models/Tier2_UserAdAccounts.js +83 -83
- package/models/TokenRotationState.js +121 -121
- package/models/TonicRSOCKeywordPerformance.js +122 -122
- package/models/Vertical.js +25 -25
- package/models/newFiles.js +110 -110
- package/package.json +19 -21
- package/run.sh +214 -214
- package/services/sequelizeService.js +63 -63
|
@@ -1,357 +1,357 @@
|
|
|
1
|
-
module.exports = (sequelize, DataTypes) => {
|
|
2
|
-
const RSOCFeedCampaign = sequelize.define(
|
|
3
|
-
"RSOCFeedCampaign",
|
|
4
|
-
{
|
|
5
|
-
AGRS_CID: {
|
|
6
|
-
type: DataTypes.STRING,
|
|
7
|
-
allowNull: false,
|
|
8
|
-
unique: true,
|
|
9
|
-
primaryKey: true,
|
|
10
|
-
},
|
|
11
|
-
AGRSAID: {
|
|
12
|
-
type: DataTypes.STRING,
|
|
13
|
-
allowNull: true,
|
|
14
|
-
comment: "ID referencing Article.AGRSAID",
|
|
15
|
-
},
|
|
16
|
-
articleName: {
|
|
17
|
-
type: DataTypes.STRING,
|
|
18
|
-
allowNull: true,
|
|
19
|
-
comment: "Name of the associated article",
|
|
20
|
-
},
|
|
21
|
-
channelId: {
|
|
22
|
-
type: DataTypes.STRING,
|
|
23
|
-
allowNull: false,
|
|
24
|
-
comment: "ID referencing Channel.channelId",
|
|
25
|
-
},
|
|
26
|
-
styleId: {
|
|
27
|
-
type: DataTypes.STRING,
|
|
28
|
-
allowNull: true,
|
|
29
|
-
comment: "Style ID (stid) inherited from Channel",
|
|
30
|
-
},
|
|
31
|
-
assignee: {
|
|
32
|
-
type: DataTypes.STRING,
|
|
33
|
-
allowNull: false,
|
|
34
|
-
},
|
|
35
|
-
status: {
|
|
36
|
-
type: DataTypes.STRING,
|
|
37
|
-
allowNull: false,
|
|
38
|
-
defaultValue: "active",
|
|
39
|
-
validate: {
|
|
40
|
-
isIn: [["active", "inactive", "archived"]],
|
|
41
|
-
},
|
|
42
|
-
},
|
|
43
|
-
country: {
|
|
44
|
-
type: DataTypes.STRING,
|
|
45
|
-
allowNull: false,
|
|
46
|
-
},
|
|
47
|
-
vertical: {
|
|
48
|
-
type: DataTypes.STRING,
|
|
49
|
-
allowNull: false,
|
|
50
|
-
comment: "Maps to Article.category for dynamic selection",
|
|
51
|
-
},
|
|
52
|
-
freeText: {
|
|
53
|
-
type: DataTypes.TEXT,
|
|
54
|
-
allowNull: true,
|
|
55
|
-
},
|
|
56
|
-
campaignName: {
|
|
57
|
-
type: DataTypes.STRING,
|
|
58
|
-
allowNull: false,
|
|
59
|
-
},
|
|
60
|
-
campaignId: {
|
|
61
|
-
type: DataTypes.STRING,
|
|
62
|
-
allowNull: true,
|
|
63
|
-
},
|
|
64
|
-
keywords: {
|
|
65
|
-
type: DataTypes.ARRAY(DataTypes.STRING),
|
|
66
|
-
allowNull: true,
|
|
67
|
-
},
|
|
68
|
-
MainKeyword: {
|
|
69
|
-
type: DataTypes.STRING,
|
|
70
|
-
allowNull: true,
|
|
71
|
-
comment:
|
|
72
|
-
"Primary/main keyword for the campaign, typically the first keyword from Predicto custom_query",
|
|
73
|
-
},
|
|
74
|
-
link: {
|
|
75
|
-
type: DataTypes.TEXT,
|
|
76
|
-
allowNull: true,
|
|
77
|
-
comment: "Campaign-specific URL based on article URL with parameters",
|
|
78
|
-
},
|
|
79
|
-
redirectLink: {
|
|
80
|
-
type: DataTypes.TEXT,
|
|
81
|
-
allowNull: true,
|
|
82
|
-
},
|
|
83
|
-
pixelId: {
|
|
84
|
-
type: DataTypes.TEXT,
|
|
85
|
-
allowNull: true,
|
|
86
|
-
},
|
|
87
|
-
token: {
|
|
88
|
-
type: DataTypes.TEXT,
|
|
89
|
-
allowNull: true,
|
|
90
|
-
},
|
|
91
|
-
accountId: {
|
|
92
|
-
type: DataTypes.STRING,
|
|
93
|
-
allowNull: false,
|
|
94
|
-
},
|
|
95
|
-
feedName: {
|
|
96
|
-
type: DataTypes.STRING,
|
|
97
|
-
allowNull: true,
|
|
98
|
-
defaultValue: "RSOC",
|
|
99
|
-
},
|
|
100
|
-
feedType: {
|
|
101
|
-
type: DataTypes.STRING,
|
|
102
|
-
allowNull: true,
|
|
103
|
-
comment: "Type of feed campaign",
|
|
104
|
-
},
|
|
105
|
-
adTitle: {
|
|
106
|
-
type: DataTypes.STRING,
|
|
107
|
-
allowNull: true,
|
|
108
|
-
},
|
|
109
|
-
domain: {
|
|
110
|
-
type: DataTypes.STRING,
|
|
111
|
-
allowNull: true,
|
|
112
|
-
defaultValue: "sportfoy.com",
|
|
113
|
-
comment:
|
|
114
|
-
"Target domain for campaign URLs (searchlabz.com or sportfoy.com)",
|
|
115
|
-
},
|
|
116
|
-
url_structure: {
|
|
117
|
-
type: DataTypes.ENUM("legacy", "unified"),
|
|
118
|
-
allowNull: false,
|
|
119
|
-
defaultValue: "legacy",
|
|
120
|
-
comment:
|
|
121
|
-
"URL structure type: legacy (slug-based) or unified (search-based)",
|
|
122
|
-
},
|
|
123
|
-
feedProvider: {
|
|
124
|
-
type: DataTypes.STRING,
|
|
125
|
-
allowNull: false,
|
|
126
|
-
defaultValue: "RSOC",
|
|
127
|
-
},
|
|
128
|
-
createdCampaignAt: {
|
|
129
|
-
type: DataTypes.DATE,
|
|
130
|
-
allowNull: true,
|
|
131
|
-
defaultValue: DataTypes.NOW,
|
|
132
|
-
},
|
|
133
|
-
draft: {
|
|
134
|
-
type: DataTypes.BOOLEAN,
|
|
135
|
-
allowNull: true,
|
|
136
|
-
defaultValue: false,
|
|
137
|
-
},
|
|
138
|
-
openedFromDashboard: {
|
|
139
|
-
type: DataTypes.BOOLEAN,
|
|
140
|
-
allowNull: true,
|
|
141
|
-
defaultValue: null,
|
|
142
|
-
comment:
|
|
143
|
-
"Tracks if the campaign was opened/viewed from the dashboard, allow null for campaigns created before this field was added",
|
|
144
|
-
},
|
|
145
|
-
createdAt: {
|
|
146
|
-
type: DataTypes.DATE,
|
|
147
|
-
allowNull: false,
|
|
148
|
-
defaultValue: DataTypes.NOW,
|
|
149
|
-
},
|
|
150
|
-
updatedAt: {
|
|
151
|
-
type: DataTypes.DATE,
|
|
152
|
-
allowNull: false,
|
|
153
|
-
defaultValue: DataTypes.NOW,
|
|
154
|
-
},
|
|
155
|
-
platform: {
|
|
156
|
-
type: DataTypes.STRING,
|
|
157
|
-
allowNull: false,
|
|
158
|
-
},
|
|
159
|
-
mediaBuyer: {
|
|
160
|
-
type: DataTypes.STRING,
|
|
161
|
-
allowNull: true,
|
|
162
|
-
},
|
|
163
|
-
Partner: {
|
|
164
|
-
type: DataTypes.STRING,
|
|
165
|
-
allowNull: true,
|
|
166
|
-
comment: "Partner associated with the campaign",
|
|
167
|
-
},
|
|
168
|
-
language: {
|
|
169
|
-
type: DataTypes.STRING,
|
|
170
|
-
allowNull: true,
|
|
171
|
-
comment: "Language code for the campaign",
|
|
172
|
-
},
|
|
173
|
-
},
|
|
174
|
-
{
|
|
175
|
-
tableName: "rsoc_feed_campaigns",
|
|
176
|
-
timestamps: true,
|
|
177
|
-
}
|
|
178
|
-
);
|
|
179
|
-
|
|
180
|
-
// Define associations without enforcing foreign key constraints
|
|
181
|
-
RSOCFeedCampaign.associate = (models) => {
|
|
182
|
-
if (models.Article) {
|
|
183
|
-
RSOCFeedCampaign.belongsTo(models.Article, {
|
|
184
|
-
foreignKey: "AGRSAID",
|
|
185
|
-
targetKey: "AGRSAID",
|
|
186
|
-
as: "Article",
|
|
187
|
-
constraints: false,
|
|
188
|
-
});
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
if (models.Channel) {
|
|
192
|
-
RSOCFeedCampaign.belongsTo(models.Channel, {
|
|
193
|
-
foreignKey: "channelId",
|
|
194
|
-
targetKey: "channelId",
|
|
195
|
-
as: "Channel",
|
|
196
|
-
constraints: false,
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
};
|
|
200
|
-
|
|
201
|
-
// BeforeCreate hook: Update Channel status and connectedCampaigns
|
|
202
|
-
RSOCFeedCampaign.beforeCreate(async (campaign, options) => {
|
|
203
|
-
campaign.setDataValue("redirectLink", null);
|
|
204
|
-
|
|
205
|
-
// Set default domain and url_structure if not provided
|
|
206
|
-
if (!campaign.domain) {
|
|
207
|
-
campaign.setDataValue("domain", "sportfoy.com");
|
|
208
|
-
}
|
|
209
|
-
if (!campaign.url_structure) {
|
|
210
|
-
campaign.setDataValue("url_structure", "legacy");
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
// Auto-adjust url_structure based on domain
|
|
214
|
-
if (
|
|
215
|
-
campaign.domain === "sportfoy.com" &&
|
|
216
|
-
campaign.url_structure === "legacy"
|
|
217
|
-
) {
|
|
218
|
-
campaign.setDataValue("url_structure", "unified");
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
// Skip channel update if in a silent operation (e.g., during sync)
|
|
222
|
-
if (options.silent) return;
|
|
223
|
-
|
|
224
|
-
try {
|
|
225
|
-
const channel = await sequelize.models.Channel.findOne({
|
|
226
|
-
where: {
|
|
227
|
-
channelId: campaign.channelId,
|
|
228
|
-
styleId: campaign.styleId || null,
|
|
229
|
-
},
|
|
230
|
-
});
|
|
231
|
-
|
|
232
|
-
if (channel) {
|
|
233
|
-
const connectedCampaigns = channel.connectedCampaigns || [];
|
|
234
|
-
if (
|
|
235
|
-
!connectedCampaigns.some((c) => c.campaignId === campaign.AGRS_CID)
|
|
236
|
-
) {
|
|
237
|
-
connectedCampaigns.push({
|
|
238
|
-
campaignId: campaign.AGRS_CID,
|
|
239
|
-
assignedAt: new Date(),
|
|
240
|
-
releasedAt: null,
|
|
241
|
-
});
|
|
242
|
-
await channel.update(
|
|
243
|
-
{
|
|
244
|
-
status: "used",
|
|
245
|
-
connectedCampaigns,
|
|
246
|
-
},
|
|
247
|
-
{ silent: true }
|
|
248
|
-
);
|
|
249
|
-
}
|
|
250
|
-
} else {
|
|
251
|
-
console.warn(
|
|
252
|
-
`Channel not found for channelId: ${campaign.channelId}, styleId: ${campaign.styleId}`
|
|
253
|
-
);
|
|
254
|
-
}
|
|
255
|
-
} catch (error) {
|
|
256
|
-
console.error("Error in beforeCreate hook:", error.message);
|
|
257
|
-
}
|
|
258
|
-
});
|
|
259
|
-
|
|
260
|
-
// BeforeUpdate hook: Handle channel reassignment
|
|
261
|
-
RSOCFeedCampaign.beforeUpdate(async (campaign, options) => {
|
|
262
|
-
campaign.setDataValue("redirectLink", null);
|
|
263
|
-
|
|
264
|
-
// Auto-adjust url_structure based on domain if domain changed
|
|
265
|
-
if (campaign.changed("domain")) {
|
|
266
|
-
if (
|
|
267
|
-
campaign.domain === "sportfoy.com" &&
|
|
268
|
-
campaign.url_structure === "legacy"
|
|
269
|
-
) {
|
|
270
|
-
campaign.setDataValue("url_structure", "unified");
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
// Skip channel update if in a silent operation or no relevant changes
|
|
275
|
-
if (
|
|
276
|
-
options.silent ||
|
|
277
|
-
(!campaign.changed("channelId") && !campaign.changed("styleId"))
|
|
278
|
-
) {
|
|
279
|
-
return;
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
try {
|
|
283
|
-
const oldChannelId = campaign.previous("channelId");
|
|
284
|
-
const oldStyleId = campaign.previous("styleId") || null;
|
|
285
|
-
const newChannelId = campaign.channelId;
|
|
286
|
-
const newStyleId = campaign.styleId || null;
|
|
287
|
-
|
|
288
|
-
// Release old channel
|
|
289
|
-
if (
|
|
290
|
-
oldChannelId &&
|
|
291
|
-
(oldChannelId !== newChannelId || oldStyleId !== newStyleId)
|
|
292
|
-
) {
|
|
293
|
-
const oldChannel = await sequelize.models.Channel.findOne({
|
|
294
|
-
where: {
|
|
295
|
-
channelId: oldChannelId,
|
|
296
|
-
styleId: oldStyleId,
|
|
297
|
-
},
|
|
298
|
-
});
|
|
299
|
-
|
|
300
|
-
if (oldChannel) {
|
|
301
|
-
const connectedCampaigns = oldChannel.connectedCampaigns || [];
|
|
302
|
-
const updatedCampaigns = connectedCampaigns
|
|
303
|
-
.map((c) =>
|
|
304
|
-
c.campaignId === campaign.AGRS_CID
|
|
305
|
-
? { ...c, releasedAt: new Date() }
|
|
306
|
-
: c
|
|
307
|
-
)
|
|
308
|
-
.filter((c) => !c.releasedAt);
|
|
309
|
-
|
|
310
|
-
await oldChannel.update(
|
|
311
|
-
{
|
|
312
|
-
connectedCampaigns: updatedCampaigns,
|
|
313
|
-
status: updatedCampaigns.length > 0 ? "used" : "free",
|
|
314
|
-
},
|
|
315
|
-
{ silent: true }
|
|
316
|
-
);
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
// Assign new channel
|
|
321
|
-
const newChannel = await sequelize.models.Channel.findOne({
|
|
322
|
-
where: {
|
|
323
|
-
channelId: newChannelId,
|
|
324
|
-
styleId: newStyleId,
|
|
325
|
-
},
|
|
326
|
-
});
|
|
327
|
-
|
|
328
|
-
if (newChannel) {
|
|
329
|
-
const connectedCampaigns = newChannel.connectedCampaigns || [];
|
|
330
|
-
if (
|
|
331
|
-
!connectedCampaigns.some((c) => c.campaignId === campaign.AGRS_CID)
|
|
332
|
-
) {
|
|
333
|
-
connectedCampaigns.push({
|
|
334
|
-
campaignId: campaign.AGRS_CID,
|
|
335
|
-
assignedAt: new Date(),
|
|
336
|
-
releasedAt: null,
|
|
337
|
-
});
|
|
338
|
-
await newChannel.update(
|
|
339
|
-
{
|
|
340
|
-
status: "used",
|
|
341
|
-
connectedCampaigns,
|
|
342
|
-
},
|
|
343
|
-
{ silent: true }
|
|
344
|
-
);
|
|
345
|
-
}
|
|
346
|
-
} else {
|
|
347
|
-
console.warn(
|
|
348
|
-
`New channel not found for channelId: ${newChannelId}, styleId: ${newStyleId}`
|
|
349
|
-
);
|
|
350
|
-
}
|
|
351
|
-
} catch (error) {
|
|
352
|
-
console.error("Error in beforeUpdate hook:", error.message);
|
|
353
|
-
}
|
|
354
|
-
});
|
|
355
|
-
|
|
356
|
-
return RSOCFeedCampaign;
|
|
357
|
-
};
|
|
1
|
+
module.exports = (sequelize, DataTypes) => {
|
|
2
|
+
const RSOCFeedCampaign = sequelize.define(
|
|
3
|
+
"RSOCFeedCampaign",
|
|
4
|
+
{
|
|
5
|
+
AGRS_CID: {
|
|
6
|
+
type: DataTypes.STRING,
|
|
7
|
+
allowNull: false,
|
|
8
|
+
unique: true,
|
|
9
|
+
primaryKey: true,
|
|
10
|
+
},
|
|
11
|
+
AGRSAID: {
|
|
12
|
+
type: DataTypes.STRING,
|
|
13
|
+
allowNull: true,
|
|
14
|
+
comment: "ID referencing Article.AGRSAID",
|
|
15
|
+
},
|
|
16
|
+
articleName: {
|
|
17
|
+
type: DataTypes.STRING,
|
|
18
|
+
allowNull: true,
|
|
19
|
+
comment: "Name of the associated article",
|
|
20
|
+
},
|
|
21
|
+
channelId: {
|
|
22
|
+
type: DataTypes.STRING,
|
|
23
|
+
allowNull: false,
|
|
24
|
+
comment: "ID referencing Channel.channelId",
|
|
25
|
+
},
|
|
26
|
+
styleId: {
|
|
27
|
+
type: DataTypes.STRING,
|
|
28
|
+
allowNull: true,
|
|
29
|
+
comment: "Style ID (stid) inherited from Channel",
|
|
30
|
+
},
|
|
31
|
+
assignee: {
|
|
32
|
+
type: DataTypes.STRING,
|
|
33
|
+
allowNull: false,
|
|
34
|
+
},
|
|
35
|
+
status: {
|
|
36
|
+
type: DataTypes.STRING,
|
|
37
|
+
allowNull: false,
|
|
38
|
+
defaultValue: "active",
|
|
39
|
+
validate: {
|
|
40
|
+
isIn: [["active", "inactive", "archived"]],
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
country: {
|
|
44
|
+
type: DataTypes.STRING,
|
|
45
|
+
allowNull: false,
|
|
46
|
+
},
|
|
47
|
+
vertical: {
|
|
48
|
+
type: DataTypes.STRING,
|
|
49
|
+
allowNull: false,
|
|
50
|
+
comment: "Maps to Article.category for dynamic selection",
|
|
51
|
+
},
|
|
52
|
+
freeText: {
|
|
53
|
+
type: DataTypes.TEXT,
|
|
54
|
+
allowNull: true,
|
|
55
|
+
},
|
|
56
|
+
campaignName: {
|
|
57
|
+
type: DataTypes.STRING,
|
|
58
|
+
allowNull: false,
|
|
59
|
+
},
|
|
60
|
+
campaignId: {
|
|
61
|
+
type: DataTypes.STRING,
|
|
62
|
+
allowNull: true,
|
|
63
|
+
},
|
|
64
|
+
keywords: {
|
|
65
|
+
type: DataTypes.ARRAY(DataTypes.STRING),
|
|
66
|
+
allowNull: true,
|
|
67
|
+
},
|
|
68
|
+
MainKeyword: {
|
|
69
|
+
type: DataTypes.STRING,
|
|
70
|
+
allowNull: true,
|
|
71
|
+
comment:
|
|
72
|
+
"Primary/main keyword for the campaign, typically the first keyword from Predicto custom_query",
|
|
73
|
+
},
|
|
74
|
+
link: {
|
|
75
|
+
type: DataTypes.TEXT,
|
|
76
|
+
allowNull: true,
|
|
77
|
+
comment: "Campaign-specific URL based on article URL with parameters",
|
|
78
|
+
},
|
|
79
|
+
redirectLink: {
|
|
80
|
+
type: DataTypes.TEXT,
|
|
81
|
+
allowNull: true,
|
|
82
|
+
},
|
|
83
|
+
pixelId: {
|
|
84
|
+
type: DataTypes.TEXT,
|
|
85
|
+
allowNull: true,
|
|
86
|
+
},
|
|
87
|
+
token: {
|
|
88
|
+
type: DataTypes.TEXT,
|
|
89
|
+
allowNull: true,
|
|
90
|
+
},
|
|
91
|
+
accountId: {
|
|
92
|
+
type: DataTypes.STRING,
|
|
93
|
+
allowNull: false,
|
|
94
|
+
},
|
|
95
|
+
feedName: {
|
|
96
|
+
type: DataTypes.STRING,
|
|
97
|
+
allowNull: true,
|
|
98
|
+
defaultValue: "RSOC",
|
|
99
|
+
},
|
|
100
|
+
feedType: {
|
|
101
|
+
type: DataTypes.STRING,
|
|
102
|
+
allowNull: true,
|
|
103
|
+
comment: "Type of feed campaign",
|
|
104
|
+
},
|
|
105
|
+
adTitle: {
|
|
106
|
+
type: DataTypes.STRING,
|
|
107
|
+
allowNull: true,
|
|
108
|
+
},
|
|
109
|
+
domain: {
|
|
110
|
+
type: DataTypes.STRING,
|
|
111
|
+
allowNull: true,
|
|
112
|
+
defaultValue: "sportfoy.com",
|
|
113
|
+
comment:
|
|
114
|
+
"Target domain for campaign URLs (searchlabz.com or sportfoy.com)",
|
|
115
|
+
},
|
|
116
|
+
url_structure: {
|
|
117
|
+
type: DataTypes.ENUM("legacy", "unified"),
|
|
118
|
+
allowNull: false,
|
|
119
|
+
defaultValue: "legacy",
|
|
120
|
+
comment:
|
|
121
|
+
"URL structure type: legacy (slug-based) or unified (search-based)",
|
|
122
|
+
},
|
|
123
|
+
feedProvider: {
|
|
124
|
+
type: DataTypes.STRING,
|
|
125
|
+
allowNull: false,
|
|
126
|
+
defaultValue: "RSOC",
|
|
127
|
+
},
|
|
128
|
+
createdCampaignAt: {
|
|
129
|
+
type: DataTypes.DATE,
|
|
130
|
+
allowNull: true,
|
|
131
|
+
defaultValue: DataTypes.NOW,
|
|
132
|
+
},
|
|
133
|
+
draft: {
|
|
134
|
+
type: DataTypes.BOOLEAN,
|
|
135
|
+
allowNull: true,
|
|
136
|
+
defaultValue: false,
|
|
137
|
+
},
|
|
138
|
+
openedFromDashboard: {
|
|
139
|
+
type: DataTypes.BOOLEAN,
|
|
140
|
+
allowNull: true,
|
|
141
|
+
defaultValue: null,
|
|
142
|
+
comment:
|
|
143
|
+
"Tracks if the campaign was opened/viewed from the dashboard, allow null for campaigns created before this field was added",
|
|
144
|
+
},
|
|
145
|
+
createdAt: {
|
|
146
|
+
type: DataTypes.DATE,
|
|
147
|
+
allowNull: false,
|
|
148
|
+
defaultValue: DataTypes.NOW,
|
|
149
|
+
},
|
|
150
|
+
updatedAt: {
|
|
151
|
+
type: DataTypes.DATE,
|
|
152
|
+
allowNull: false,
|
|
153
|
+
defaultValue: DataTypes.NOW,
|
|
154
|
+
},
|
|
155
|
+
platform: {
|
|
156
|
+
type: DataTypes.STRING,
|
|
157
|
+
allowNull: false,
|
|
158
|
+
},
|
|
159
|
+
mediaBuyer: {
|
|
160
|
+
type: DataTypes.STRING,
|
|
161
|
+
allowNull: true,
|
|
162
|
+
},
|
|
163
|
+
Partner: {
|
|
164
|
+
type: DataTypes.STRING,
|
|
165
|
+
allowNull: true,
|
|
166
|
+
comment: "Partner associated with the campaign",
|
|
167
|
+
},
|
|
168
|
+
language: {
|
|
169
|
+
type: DataTypes.STRING,
|
|
170
|
+
allowNull: true,
|
|
171
|
+
comment: "Language code for the campaign",
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
{
|
|
175
|
+
tableName: "rsoc_feed_campaigns",
|
|
176
|
+
timestamps: true,
|
|
177
|
+
}
|
|
178
|
+
);
|
|
179
|
+
|
|
180
|
+
// Define associations without enforcing foreign key constraints
|
|
181
|
+
RSOCFeedCampaign.associate = (models) => {
|
|
182
|
+
if (models.Article) {
|
|
183
|
+
RSOCFeedCampaign.belongsTo(models.Article, {
|
|
184
|
+
foreignKey: "AGRSAID",
|
|
185
|
+
targetKey: "AGRSAID",
|
|
186
|
+
as: "Article",
|
|
187
|
+
constraints: false,
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (models.Channel) {
|
|
192
|
+
RSOCFeedCampaign.belongsTo(models.Channel, {
|
|
193
|
+
foreignKey: "channelId",
|
|
194
|
+
targetKey: "channelId",
|
|
195
|
+
as: "Channel",
|
|
196
|
+
constraints: false,
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
// BeforeCreate hook: Update Channel status and connectedCampaigns
|
|
202
|
+
RSOCFeedCampaign.beforeCreate(async (campaign, options) => {
|
|
203
|
+
campaign.setDataValue("redirectLink", null);
|
|
204
|
+
|
|
205
|
+
// Set default domain and url_structure if not provided
|
|
206
|
+
if (!campaign.domain) {
|
|
207
|
+
campaign.setDataValue("domain", "sportfoy.com");
|
|
208
|
+
}
|
|
209
|
+
if (!campaign.url_structure) {
|
|
210
|
+
campaign.setDataValue("url_structure", "legacy");
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// Auto-adjust url_structure based on domain
|
|
214
|
+
if (
|
|
215
|
+
campaign.domain === "sportfoy.com" &&
|
|
216
|
+
campaign.url_structure === "legacy"
|
|
217
|
+
) {
|
|
218
|
+
campaign.setDataValue("url_structure", "unified");
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// Skip channel update if in a silent operation (e.g., during sync)
|
|
222
|
+
if (options.silent) return;
|
|
223
|
+
|
|
224
|
+
try {
|
|
225
|
+
const channel = await sequelize.models.Channel.findOne({
|
|
226
|
+
where: {
|
|
227
|
+
channelId: campaign.channelId,
|
|
228
|
+
styleId: campaign.styleId || null,
|
|
229
|
+
},
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
if (channel) {
|
|
233
|
+
const connectedCampaigns = channel.connectedCampaigns || [];
|
|
234
|
+
if (
|
|
235
|
+
!connectedCampaigns.some((c) => c.campaignId === campaign.AGRS_CID)
|
|
236
|
+
) {
|
|
237
|
+
connectedCampaigns.push({
|
|
238
|
+
campaignId: campaign.AGRS_CID,
|
|
239
|
+
assignedAt: new Date(),
|
|
240
|
+
releasedAt: null,
|
|
241
|
+
});
|
|
242
|
+
await channel.update(
|
|
243
|
+
{
|
|
244
|
+
status: "used",
|
|
245
|
+
connectedCampaigns,
|
|
246
|
+
},
|
|
247
|
+
{ silent: true }
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
} else {
|
|
251
|
+
console.warn(
|
|
252
|
+
`Channel not found for channelId: ${campaign.channelId}, styleId: ${campaign.styleId}`
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
} catch (error) {
|
|
256
|
+
console.error("Error in beforeCreate hook:", error.message);
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
// BeforeUpdate hook: Handle channel reassignment
|
|
261
|
+
RSOCFeedCampaign.beforeUpdate(async (campaign, options) => {
|
|
262
|
+
campaign.setDataValue("redirectLink", null);
|
|
263
|
+
|
|
264
|
+
// Auto-adjust url_structure based on domain if domain changed
|
|
265
|
+
if (campaign.changed("domain")) {
|
|
266
|
+
if (
|
|
267
|
+
campaign.domain === "sportfoy.com" &&
|
|
268
|
+
campaign.url_structure === "legacy"
|
|
269
|
+
) {
|
|
270
|
+
campaign.setDataValue("url_structure", "unified");
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// Skip channel update if in a silent operation or no relevant changes
|
|
275
|
+
if (
|
|
276
|
+
options.silent ||
|
|
277
|
+
(!campaign.changed("channelId") && !campaign.changed("styleId"))
|
|
278
|
+
) {
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
try {
|
|
283
|
+
const oldChannelId = campaign.previous("channelId");
|
|
284
|
+
const oldStyleId = campaign.previous("styleId") || null;
|
|
285
|
+
const newChannelId = campaign.channelId;
|
|
286
|
+
const newStyleId = campaign.styleId || null;
|
|
287
|
+
|
|
288
|
+
// Release old channel
|
|
289
|
+
if (
|
|
290
|
+
oldChannelId &&
|
|
291
|
+
(oldChannelId !== newChannelId || oldStyleId !== newStyleId)
|
|
292
|
+
) {
|
|
293
|
+
const oldChannel = await sequelize.models.Channel.findOne({
|
|
294
|
+
where: {
|
|
295
|
+
channelId: oldChannelId,
|
|
296
|
+
styleId: oldStyleId,
|
|
297
|
+
},
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
if (oldChannel) {
|
|
301
|
+
const connectedCampaigns = oldChannel.connectedCampaigns || [];
|
|
302
|
+
const updatedCampaigns = connectedCampaigns
|
|
303
|
+
.map((c) =>
|
|
304
|
+
c.campaignId === campaign.AGRS_CID
|
|
305
|
+
? { ...c, releasedAt: new Date() }
|
|
306
|
+
: c
|
|
307
|
+
)
|
|
308
|
+
.filter((c) => !c.releasedAt);
|
|
309
|
+
|
|
310
|
+
await oldChannel.update(
|
|
311
|
+
{
|
|
312
|
+
connectedCampaigns: updatedCampaigns,
|
|
313
|
+
status: updatedCampaigns.length > 0 ? "used" : "free",
|
|
314
|
+
},
|
|
315
|
+
{ silent: true }
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// Assign new channel
|
|
321
|
+
const newChannel = await sequelize.models.Channel.findOne({
|
|
322
|
+
where: {
|
|
323
|
+
channelId: newChannelId,
|
|
324
|
+
styleId: newStyleId,
|
|
325
|
+
},
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
if (newChannel) {
|
|
329
|
+
const connectedCampaigns = newChannel.connectedCampaigns || [];
|
|
330
|
+
if (
|
|
331
|
+
!connectedCampaigns.some((c) => c.campaignId === campaign.AGRS_CID)
|
|
332
|
+
) {
|
|
333
|
+
connectedCampaigns.push({
|
|
334
|
+
campaignId: campaign.AGRS_CID,
|
|
335
|
+
assignedAt: new Date(),
|
|
336
|
+
releasedAt: null,
|
|
337
|
+
});
|
|
338
|
+
await newChannel.update(
|
|
339
|
+
{
|
|
340
|
+
status: "used",
|
|
341
|
+
connectedCampaigns,
|
|
342
|
+
},
|
|
343
|
+
{ silent: true }
|
|
344
|
+
);
|
|
345
|
+
}
|
|
346
|
+
} else {
|
|
347
|
+
console.warn(
|
|
348
|
+
`New channel not found for channelId: ${newChannelId}, styleId: ${newStyleId}`
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
} catch (error) {
|
|
352
|
+
console.error("Error in beforeUpdate hook:", error.message);
|
|
353
|
+
}
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
return RSOCFeedCampaign;
|
|
357
|
+
};
|