agrs-sequelize-sdk 1.4.52 → 1.4.53

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.
Files changed (116) hide show
  1. package/migrations/2026-05-17-PIXELS-FOLLOWUP.md +85 -85
  2. package/migrations/2026-05-17-add-platform-to-ad.js +19 -19
  3. package/migrations/2026-05-17-add-platform-to-adaccount.js +19 -19
  4. package/migrations/2026-05-17-add-platform-to-adperformance.js +19 -19
  5. package/migrations/2026-05-17-add-platform-to-adset.js +19 -19
  6. package/migrations/2026-05-17-add-platform-to-adsetperformance.js +19 -19
  7. package/migrations/2026-05-17-add-platform-to-aiarticleretryqueue.js +19 -19
  8. package/migrations/2026-05-17-add-platform-to-aicampaignqueue.js +19 -19
  9. package/migrations/2026-05-17-add-platform-to-article.js +19 -19
  10. package/migrations/2026-05-17-add-platform-to-campaign.js +19 -19
  11. package/migrations/2026-05-17-add-platform-to-codefuelcampaign.js +19 -19
  12. package/migrations/2026-05-17-add-platform-to-dynamicfeed.js +19 -19
  13. package/migrations/2026-05-17-add-platform-to-facebookretryqueue.js +19 -19
  14. package/migrations/2026-05-17-add-platform-to-pages.js +19 -19
  15. package/migrations/2026-05-17-add-platform-to-presets.js +19 -19
  16. package/migrations/2026-05-18-add-group-id-to-ai-campaign-queue.js +18 -18
  17. package/migrations/2026-05-18-add-platform-code-to-creation-logs.js +65 -65
  18. package/migrations/2026-05-18-add-platform-code-to-pixels.js +55 -55
  19. package/migrations/2026-05-18-add-platform-code-to-rsoc-feed-campaigns.js +55 -55
  20. package/migrations/2026-05-18-add-platform-date-index-to-adperformance.js +16 -16
  21. package/migrations/2026-05-18-add-platform-date-index-to-adsetperformance.js +16 -16
  22. package/migrations/2026-05-18-add-review-status-to-ad.js +36 -36
  23. package/migrations/2026-05-18-create-canonical-insights.js +84 -84
  24. package/migrations/2026-05-18-create-snapchat-public-profiles.js +68 -68
  25. package/migrations/2026-05-18-create-tiktok-identities.js +71 -71
  26. package/migrations/2026-05-18-create-tiktok-snapchat-campaigns.js +143 -143
  27. package/migrations/2026-05-18-create-tt-snp-adset-ad-tables.js +309 -309
  28. package/migrations/RUN_ME_MANUALLY_2026-05-17.sql +99 -99
  29. package/migrations/add-requested-from-dashboard-to-articles.js +17 -17
  30. package/migrations/change-adset-name-to-text.js +79 -79
  31. package/models/AICampaignQueue.js +152 -152
  32. package/models/AIGenerationLog.js +85 -85
  33. package/models/AIGenerationRequest.js +212 -212
  34. package/models/AdAccountValues.js +25 -25
  35. package/models/AdHistory.js +30 -30
  36. package/models/AdPerformance.js +104 -104
  37. package/models/AdSet.js +295 -295
  38. package/models/AdSetHistory.js +30 -30
  39. package/models/AdsetPerformance.js +137 -137
  40. package/models/AiArticleRetryQueue.js +157 -157
  41. package/models/AiCreationRetryQueue.js +134 -134
  42. package/models/ApiAuditLog.js +127 -127
  43. package/models/ApiToken.js +67 -67
  44. package/models/Article.js +222 -222
  45. package/models/AutomationRule.js +179 -179
  46. package/models/BannerTemplate.js +129 -129
  47. package/models/Buyers.js +25 -25
  48. package/models/Campaign.js +168 -163
  49. package/models/CampaignActionHistory.js +101 -101
  50. package/models/CampaignCreationLog.js +340 -340
  51. package/models/CampaignCreationLogV2.js +392 -392
  52. package/models/CampaignHistory.js +33 -33
  53. package/models/CampaignTag.js +46 -0
  54. package/models/CampaignTagAssignment.js +48 -0
  55. package/models/CanonicalInsights.js +68 -68
  56. package/models/Channel.js +55 -55
  57. package/models/Domain.js +44 -44
  58. package/models/DynamicFeed.js +218 -218
  59. package/models/ExplorAdsChannel.js +61 -61
  60. package/models/FacebookRetryQueue.js +163 -163
  61. package/models/Feed.js +33 -33
  62. package/models/FeedArticleConfiguration.js +80 -80
  63. package/models/FrontStoryChannel.js +59 -59
  64. package/models/FrontStoryChannelV2.js +60 -60
  65. package/models/GenericFlowRequest.js +114 -114
  66. package/models/MidoWebChannel.js +47 -47
  67. package/models/MineBrandBlocklist.js +40 -40
  68. package/models/MineChannel.js +42 -42
  69. package/models/MineRedirectDomain.js +36 -36
  70. package/models/MineWebhookEvent.js +53 -53
  71. package/models/Pages.js +117 -117
  72. package/models/PipelineExecution.js +59 -59
  73. package/models/PolicyDogsCreativeCache.js +50 -50
  74. package/models/PolicyDogsImageCache.js +30 -30
  75. package/models/Presets.js +40 -40
  76. package/models/PublicApiBatch.js +155 -155
  77. package/models/PublicApiBatchItem.js +166 -166
  78. package/models/PublicApiIdempotencyCache.js +135 -135
  79. package/models/RSOCFeedCampaign.js +387 -387
  80. package/models/RsocKeywordPerformance.js +110 -110
  81. package/models/RuleAction.js +90 -90
  82. package/models/RuleCondition.js +137 -137
  83. package/models/RuleExecution.js +107 -107
  84. package/models/RulesValues.js +56 -56
  85. package/models/SingularChannel.js +31 -31
  86. package/models/SnapchatAd.js +69 -69
  87. package/models/SnapchatAdSquad.js +92 -92
  88. package/models/SnapchatCampaign.js +69 -69
  89. package/models/SnapchatChannel.js +31 -31
  90. package/models/SnapchatPublicProfiles.js +67 -67
  91. package/models/SupportedLocale.js +23 -23
  92. package/models/SyncHistory.js +249 -249
  93. package/models/TTQChannel.js +42 -42
  94. package/models/TemplateMetadata.js +260 -260
  95. package/models/Tier2_AdAccounts.js +110 -110
  96. package/models/Tier2_Assets.js +70 -70
  97. package/models/Tier2_BusinessManagers.js +105 -105
  98. package/models/Tier2_CreditLines.js +99 -99
  99. package/models/Tier2_Pages.js +91 -91
  100. package/models/Tier2_Pixels.js +82 -82
  101. package/models/Tier2_Tokens.js +64 -64
  102. package/models/Tier2_UserAdAccounts.js +83 -83
  103. package/models/TikTokAd.js +71 -71
  104. package/models/TikTokAdGroup.js +82 -82
  105. package/models/TikTokCampaign.js +71 -71
  106. package/models/TiktokChannel.js +31 -31
  107. package/models/TiktokIdentities.js +65 -65
  108. package/models/TokenRotationState.js +121 -121
  109. package/models/TonicRSOCKeywordPerformance.js +122 -122
  110. package/models/Users.js +190 -185
  111. package/models/Vertical.js +25 -25
  112. package/models/newFiles.js +137 -137
  113. package/package.json +21 -19
  114. package/run.sh +214 -214
  115. package/services/sequelizeService.js +110 -110
  116. package/tarzo-skalio-integration-docs.md +691 -691
@@ -1,387 +1,387 @@
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.TEXT,
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
- pixelEvent: {
88
- type: DataTypes.TEXT,
89
- allowNull: true,
90
- },
91
- token: {
92
- type: DataTypes.TEXT,
93
- allowNull: true,
94
- },
95
- accountId: {
96
- type: DataTypes.STRING,
97
- allowNull: false,
98
- },
99
- feedName: {
100
- type: DataTypes.STRING,
101
- allowNull: true,
102
- defaultValue: "RSOC",
103
- },
104
- feedType: {
105
- type: DataTypes.STRING,
106
- allowNull: true,
107
- comment: "Type of feed campaign",
108
- },
109
- adTitle: {
110
- type: DataTypes.STRING,
111
- allowNull: true,
112
- },
113
- domain: {
114
- type: DataTypes.STRING,
115
- allowNull: true,
116
- defaultValue: "sportfoy.com",
117
- comment:
118
- "Target domain for campaign URLs (searchlabz.com or sportfoy.com)",
119
- },
120
- url_structure: {
121
- type: DataTypes.ENUM("legacy", "unified"),
122
- allowNull: false,
123
- defaultValue: "legacy",
124
- comment:
125
- "URL structure type: legacy (slug-based) or unified (search-based)",
126
- },
127
- feedProvider: {
128
- type: DataTypes.STRING,
129
- allowNull: false,
130
- defaultValue: "RSOC",
131
- },
132
- createdCampaignAt: {
133
- type: DataTypes.DATE,
134
- allowNull: true,
135
- defaultValue: DataTypes.NOW,
136
- },
137
- draft: {
138
- type: DataTypes.BOOLEAN,
139
- allowNull: true,
140
- defaultValue: false,
141
- },
142
- openedFromDashboard: {
143
- type: DataTypes.BOOLEAN,
144
- allowNull: true,
145
- defaultValue: null,
146
- comment:
147
- "Tracks if the campaign was opened/viewed from the dashboard, allow null for campaigns created before this field was added",
148
- },
149
- createdAt: {
150
- type: DataTypes.DATE,
151
- allowNull: false,
152
- defaultValue: DataTypes.NOW,
153
- },
154
- updatedAt: {
155
- type: DataTypes.DATE,
156
- allowNull: false,
157
- defaultValue: DataTypes.NOW,
158
- },
159
- platform: {
160
- type: DataTypes.STRING,
161
- allowNull: false,
162
- },
163
- platform_code: {
164
- type: DataTypes.ENUM("fb", "tt", "snp"),
165
- allowNull: false,
166
- defaultValue: "fb",
167
- comment: "Canonical ad platform identifier: fb=Facebook, tt=TikTok, snp=Snapchat",
168
- },
169
- mediaBuyer: {
170
- type: DataTypes.STRING,
171
- allowNull: true,
172
- },
173
- Partner: {
174
- type: DataTypes.STRING,
175
- allowNull: true,
176
- comment: "Partner associated with the campaign",
177
- },
178
- language: {
179
- type: DataTypes.STRING,
180
- allowNull: true,
181
- comment: "Language code for the campaign",
182
- },
183
- feed_id: {
184
- type: DataTypes.UUID,
185
- allowNull: true,
186
- comment: "ID referencing DynamicFeed.id for dynamic feeds",
187
- },
188
- brand_name_hold: {
189
- type: DataTypes.BOOLEAN,
190
- allowNull: false,
191
- defaultValue: false,
192
- comment: "When true, nightly channel-release crons must skip this campaign",
193
- },
194
- },
195
- {
196
- tableName: "rsoc_feed_campaigns",
197
- timestamps: true,
198
- }
199
- );
200
-
201
- // Define associations without enforcing foreign key constraints
202
- RSOCFeedCampaign.associate = (models) => {
203
- if (models.Article) {
204
- RSOCFeedCampaign.belongsTo(models.Article, {
205
- foreignKey: "AGRSAID",
206
- targetKey: "AGRSAID",
207
- as: "Article",
208
- constraints: false,
209
- });
210
- }
211
-
212
- if (models.Channel) {
213
- RSOCFeedCampaign.belongsTo(models.Channel, {
214
- foreignKey: "channelId",
215
- targetKey: "channelId",
216
- as: "Channel",
217
- constraints: false,
218
- });
219
- }
220
-
221
- if (models.DynamicFeed) {
222
- RSOCFeedCampaign.belongsTo(models.DynamicFeed, {
223
- foreignKey: "feed_id",
224
- targetKey: "id",
225
- as: "DynamicFeed",
226
- constraints: false,
227
- });
228
- }
229
- };
230
-
231
- // BeforeCreate hook: Update Channel status and connectedCampaigns
232
- RSOCFeedCampaign.beforeCreate(async (campaign, options) => {
233
- campaign.setDataValue("redirectLink", null);
234
-
235
- // Set default domain and url_structure if not provided
236
- if (!campaign.domain) {
237
- campaign.setDataValue("domain", "sportfoy.com");
238
- }
239
- if (!campaign.url_structure) {
240
- campaign.setDataValue("url_structure", "legacy");
241
- }
242
-
243
- // Auto-adjust url_structure based on domain
244
- if (
245
- campaign.domain === "sportfoy.com" &&
246
- campaign.url_structure === "legacy"
247
- ) {
248
- campaign.setDataValue("url_structure", "unified");
249
- }
250
-
251
- // Skip channel update if in a silent operation (e.g., during sync)
252
- if (options.silent) return;
253
-
254
- try {
255
- const channel = await sequelize.models.Channel.findOne({
256
- where: {
257
- channelId: campaign.channelId,
258
- styleId: campaign.styleId || null,
259
- },
260
- });
261
-
262
- if (channel) {
263
- const connectedCampaigns = channel.connectedCampaigns || [];
264
- if (
265
- !connectedCampaigns.some((c) => c.campaignId === campaign.AGRS_CID)
266
- ) {
267
- connectedCampaigns.push({
268
- campaignId: campaign.AGRS_CID,
269
- assignedAt: new Date(),
270
- releasedAt: null,
271
- });
272
- await channel.update(
273
- {
274
- status: "used",
275
- connectedCampaigns,
276
- },
277
- { silent: true }
278
- );
279
- }
280
- } else {
281
- console.warn(
282
- `Channel not found for channelId: ${campaign.channelId}, styleId: ${campaign.styleId}`
283
- );
284
- }
285
- } catch (error) {
286
- console.error("Error in beforeCreate hook:", error.message);
287
- }
288
- });
289
-
290
- // BeforeUpdate hook: Handle channel reassignment
291
- RSOCFeedCampaign.beforeUpdate(async (campaign, options) => {
292
- campaign.setDataValue("redirectLink", null);
293
-
294
- // Auto-adjust url_structure based on domain if domain changed
295
- if (campaign.changed("domain")) {
296
- if (
297
- campaign.domain === "sportfoy.com" &&
298
- campaign.url_structure === "legacy"
299
- ) {
300
- campaign.setDataValue("url_structure", "unified");
301
- }
302
- }
303
-
304
- // Skip channel update if in a silent operation or no relevant changes
305
- if (
306
- options.silent ||
307
- (!campaign.changed("channelId") && !campaign.changed("styleId"))
308
- ) {
309
- return;
310
- }
311
-
312
- try {
313
- const oldChannelId = campaign.previous("channelId");
314
- const oldStyleId = campaign.previous("styleId") || null;
315
- const newChannelId = campaign.channelId;
316
- const newStyleId = campaign.styleId || null;
317
-
318
- // Release old channel
319
- if (
320
- oldChannelId &&
321
- (oldChannelId !== newChannelId || oldStyleId !== newStyleId)
322
- ) {
323
- const oldChannel = await sequelize.models.Channel.findOne({
324
- where: {
325
- channelId: oldChannelId,
326
- styleId: oldStyleId,
327
- },
328
- });
329
-
330
- if (oldChannel) {
331
- const connectedCampaigns = oldChannel.connectedCampaigns || [];
332
- const updatedCampaigns = connectedCampaigns
333
- .map((c) =>
334
- c.campaignId === campaign.AGRS_CID
335
- ? { ...c, releasedAt: new Date() }
336
- : c
337
- )
338
- .filter((c) => !c.releasedAt);
339
-
340
- await oldChannel.update(
341
- {
342
- connectedCampaigns: updatedCampaigns,
343
- status: updatedCampaigns.length > 0 ? "used" : "free",
344
- },
345
- { silent: true }
346
- );
347
- }
348
- }
349
-
350
- // Assign new channel
351
- const newChannel = await sequelize.models.Channel.findOne({
352
- where: {
353
- channelId: newChannelId,
354
- styleId: newStyleId,
355
- },
356
- });
357
-
358
- if (newChannel) {
359
- const connectedCampaigns = newChannel.connectedCampaigns || [];
360
- if (
361
- !connectedCampaigns.some((c) => c.campaignId === campaign.AGRS_CID)
362
- ) {
363
- connectedCampaigns.push({
364
- campaignId: campaign.AGRS_CID,
365
- assignedAt: new Date(),
366
- releasedAt: null,
367
- });
368
- await newChannel.update(
369
- {
370
- status: "used",
371
- connectedCampaigns,
372
- },
373
- { silent: true }
374
- );
375
- }
376
- } else {
377
- console.warn(
378
- `New channel not found for channelId: ${newChannelId}, styleId: ${newStyleId}`
379
- );
380
- }
381
- } catch (error) {
382
- console.error("Error in beforeUpdate hook:", error.message);
383
- }
384
- });
385
-
386
- return RSOCFeedCampaign;
387
- };
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.TEXT,
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
+ pixelEvent: {
88
+ type: DataTypes.TEXT,
89
+ allowNull: true,
90
+ },
91
+ token: {
92
+ type: DataTypes.TEXT,
93
+ allowNull: true,
94
+ },
95
+ accountId: {
96
+ type: DataTypes.STRING,
97
+ allowNull: false,
98
+ },
99
+ feedName: {
100
+ type: DataTypes.STRING,
101
+ allowNull: true,
102
+ defaultValue: "RSOC",
103
+ },
104
+ feedType: {
105
+ type: DataTypes.STRING,
106
+ allowNull: true,
107
+ comment: "Type of feed campaign",
108
+ },
109
+ adTitle: {
110
+ type: DataTypes.STRING,
111
+ allowNull: true,
112
+ },
113
+ domain: {
114
+ type: DataTypes.STRING,
115
+ allowNull: true,
116
+ defaultValue: "sportfoy.com",
117
+ comment:
118
+ "Target domain for campaign URLs (searchlabz.com or sportfoy.com)",
119
+ },
120
+ url_structure: {
121
+ type: DataTypes.ENUM("legacy", "unified"),
122
+ allowNull: false,
123
+ defaultValue: "legacy",
124
+ comment:
125
+ "URL structure type: legacy (slug-based) or unified (search-based)",
126
+ },
127
+ feedProvider: {
128
+ type: DataTypes.STRING,
129
+ allowNull: false,
130
+ defaultValue: "RSOC",
131
+ },
132
+ createdCampaignAt: {
133
+ type: DataTypes.DATE,
134
+ allowNull: true,
135
+ defaultValue: DataTypes.NOW,
136
+ },
137
+ draft: {
138
+ type: DataTypes.BOOLEAN,
139
+ allowNull: true,
140
+ defaultValue: false,
141
+ },
142
+ openedFromDashboard: {
143
+ type: DataTypes.BOOLEAN,
144
+ allowNull: true,
145
+ defaultValue: null,
146
+ comment:
147
+ "Tracks if the campaign was opened/viewed from the dashboard, allow null for campaigns created before this field was added",
148
+ },
149
+ createdAt: {
150
+ type: DataTypes.DATE,
151
+ allowNull: false,
152
+ defaultValue: DataTypes.NOW,
153
+ },
154
+ updatedAt: {
155
+ type: DataTypes.DATE,
156
+ allowNull: false,
157
+ defaultValue: DataTypes.NOW,
158
+ },
159
+ platform: {
160
+ type: DataTypes.STRING,
161
+ allowNull: false,
162
+ },
163
+ platform_code: {
164
+ type: DataTypes.ENUM("fb", "tt", "snp"),
165
+ allowNull: false,
166
+ defaultValue: "fb",
167
+ comment: "Canonical ad platform identifier: fb=Facebook, tt=TikTok, snp=Snapchat",
168
+ },
169
+ mediaBuyer: {
170
+ type: DataTypes.STRING,
171
+ allowNull: true,
172
+ },
173
+ Partner: {
174
+ type: DataTypes.STRING,
175
+ allowNull: true,
176
+ comment: "Partner associated with the campaign",
177
+ },
178
+ language: {
179
+ type: DataTypes.STRING,
180
+ allowNull: true,
181
+ comment: "Language code for the campaign",
182
+ },
183
+ feed_id: {
184
+ type: DataTypes.UUID,
185
+ allowNull: true,
186
+ comment: "ID referencing DynamicFeed.id for dynamic feeds",
187
+ },
188
+ brand_name_hold: {
189
+ type: DataTypes.BOOLEAN,
190
+ allowNull: false,
191
+ defaultValue: false,
192
+ comment: "When true, nightly channel-release crons must skip this campaign",
193
+ },
194
+ },
195
+ {
196
+ tableName: "rsoc_feed_campaigns",
197
+ timestamps: true,
198
+ }
199
+ );
200
+
201
+ // Define associations without enforcing foreign key constraints
202
+ RSOCFeedCampaign.associate = (models) => {
203
+ if (models.Article) {
204
+ RSOCFeedCampaign.belongsTo(models.Article, {
205
+ foreignKey: "AGRSAID",
206
+ targetKey: "AGRSAID",
207
+ as: "Article",
208
+ constraints: false,
209
+ });
210
+ }
211
+
212
+ if (models.Channel) {
213
+ RSOCFeedCampaign.belongsTo(models.Channel, {
214
+ foreignKey: "channelId",
215
+ targetKey: "channelId",
216
+ as: "Channel",
217
+ constraints: false,
218
+ });
219
+ }
220
+
221
+ if (models.DynamicFeed) {
222
+ RSOCFeedCampaign.belongsTo(models.DynamicFeed, {
223
+ foreignKey: "feed_id",
224
+ targetKey: "id",
225
+ as: "DynamicFeed",
226
+ constraints: false,
227
+ });
228
+ }
229
+ };
230
+
231
+ // BeforeCreate hook: Update Channel status and connectedCampaigns
232
+ RSOCFeedCampaign.beforeCreate(async (campaign, options) => {
233
+ campaign.setDataValue("redirectLink", null);
234
+
235
+ // Set default domain and url_structure if not provided
236
+ if (!campaign.domain) {
237
+ campaign.setDataValue("domain", "sportfoy.com");
238
+ }
239
+ if (!campaign.url_structure) {
240
+ campaign.setDataValue("url_structure", "legacy");
241
+ }
242
+
243
+ // Auto-adjust url_structure based on domain
244
+ if (
245
+ campaign.domain === "sportfoy.com" &&
246
+ campaign.url_structure === "legacy"
247
+ ) {
248
+ campaign.setDataValue("url_structure", "unified");
249
+ }
250
+
251
+ // Skip channel update if in a silent operation (e.g., during sync)
252
+ if (options.silent) return;
253
+
254
+ try {
255
+ const channel = await sequelize.models.Channel.findOne({
256
+ where: {
257
+ channelId: campaign.channelId,
258
+ styleId: campaign.styleId || null,
259
+ },
260
+ });
261
+
262
+ if (channel) {
263
+ const connectedCampaigns = channel.connectedCampaigns || [];
264
+ if (
265
+ !connectedCampaigns.some((c) => c.campaignId === campaign.AGRS_CID)
266
+ ) {
267
+ connectedCampaigns.push({
268
+ campaignId: campaign.AGRS_CID,
269
+ assignedAt: new Date(),
270
+ releasedAt: null,
271
+ });
272
+ await channel.update(
273
+ {
274
+ status: "used",
275
+ connectedCampaigns,
276
+ },
277
+ { silent: true }
278
+ );
279
+ }
280
+ } else {
281
+ console.warn(
282
+ `Channel not found for channelId: ${campaign.channelId}, styleId: ${campaign.styleId}`
283
+ );
284
+ }
285
+ } catch (error) {
286
+ console.error("Error in beforeCreate hook:", error.message);
287
+ }
288
+ });
289
+
290
+ // BeforeUpdate hook: Handle channel reassignment
291
+ RSOCFeedCampaign.beforeUpdate(async (campaign, options) => {
292
+ campaign.setDataValue("redirectLink", null);
293
+
294
+ // Auto-adjust url_structure based on domain if domain changed
295
+ if (campaign.changed("domain")) {
296
+ if (
297
+ campaign.domain === "sportfoy.com" &&
298
+ campaign.url_structure === "legacy"
299
+ ) {
300
+ campaign.setDataValue("url_structure", "unified");
301
+ }
302
+ }
303
+
304
+ // Skip channel update if in a silent operation or no relevant changes
305
+ if (
306
+ options.silent ||
307
+ (!campaign.changed("channelId") && !campaign.changed("styleId"))
308
+ ) {
309
+ return;
310
+ }
311
+
312
+ try {
313
+ const oldChannelId = campaign.previous("channelId");
314
+ const oldStyleId = campaign.previous("styleId") || null;
315
+ const newChannelId = campaign.channelId;
316
+ const newStyleId = campaign.styleId || null;
317
+
318
+ // Release old channel
319
+ if (
320
+ oldChannelId &&
321
+ (oldChannelId !== newChannelId || oldStyleId !== newStyleId)
322
+ ) {
323
+ const oldChannel = await sequelize.models.Channel.findOne({
324
+ where: {
325
+ channelId: oldChannelId,
326
+ styleId: oldStyleId,
327
+ },
328
+ });
329
+
330
+ if (oldChannel) {
331
+ const connectedCampaigns = oldChannel.connectedCampaigns || [];
332
+ const updatedCampaigns = connectedCampaigns
333
+ .map((c) =>
334
+ c.campaignId === campaign.AGRS_CID
335
+ ? { ...c, releasedAt: new Date() }
336
+ : c
337
+ )
338
+ .filter((c) => !c.releasedAt);
339
+
340
+ await oldChannel.update(
341
+ {
342
+ connectedCampaigns: updatedCampaigns,
343
+ status: updatedCampaigns.length > 0 ? "used" : "free",
344
+ },
345
+ { silent: true }
346
+ );
347
+ }
348
+ }
349
+
350
+ // Assign new channel
351
+ const newChannel = await sequelize.models.Channel.findOne({
352
+ where: {
353
+ channelId: newChannelId,
354
+ styleId: newStyleId,
355
+ },
356
+ });
357
+
358
+ if (newChannel) {
359
+ const connectedCampaigns = newChannel.connectedCampaigns || [];
360
+ if (
361
+ !connectedCampaigns.some((c) => c.campaignId === campaign.AGRS_CID)
362
+ ) {
363
+ connectedCampaigns.push({
364
+ campaignId: campaign.AGRS_CID,
365
+ assignedAt: new Date(),
366
+ releasedAt: null,
367
+ });
368
+ await newChannel.update(
369
+ {
370
+ status: "used",
371
+ connectedCampaigns,
372
+ },
373
+ { silent: true }
374
+ );
375
+ }
376
+ } else {
377
+ console.warn(
378
+ `New channel not found for channelId: ${newChannelId}, styleId: ${newStyleId}`
379
+ );
380
+ }
381
+ } catch (error) {
382
+ console.error("Error in beforeUpdate hook:", error.message);
383
+ }
384
+ });
385
+
386
+ return RSOCFeedCampaign;
387
+ };