agrs-sequelize-sdk 1.3.81 → 1.3.83

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 (64) hide show
  1. package/migrations/add-requested-from-dashboard-to-articles.js +17 -17
  2. package/migrations/change-adset-name-to-text.js +79 -79
  3. package/models/AICampaignQueue.js +136 -136
  4. package/models/AIGenerationLog.js +85 -85
  5. package/models/AIGenerationRequest.js +212 -212
  6. package/models/AdAccountValues.js +25 -25
  7. package/models/AdHistory.js +30 -30
  8. package/models/AdPerformance.js +94 -94
  9. package/models/AdSet.js +289 -289
  10. package/models/AdSetHistory.js +30 -30
  11. package/models/AdsetPerformance.js +126 -126
  12. package/models/AiArticleRetryQueue.js +150 -150
  13. package/models/Article.js +196 -196
  14. package/models/AutomationRule.js +173 -173
  15. package/models/BannerTemplate.js +129 -129
  16. package/models/Buyers.js +25 -25
  17. package/models/Campaign.js +157 -157
  18. package/models/CampaignActionHistory.js +86 -86
  19. package/models/CampaignCreationLog.js +309 -309
  20. package/models/CampaignCreationLogV2.js +314 -314
  21. package/models/CampaignHistory.js +33 -33
  22. package/models/Channel.js +55 -55
  23. package/models/Domain.js +35 -35
  24. package/models/DynamicFeed.js +212 -212
  25. package/models/ExplorAdsChannel.js +61 -61
  26. package/models/FacebookRetryQueue.js +156 -0
  27. package/models/Feed.js +33 -33
  28. package/models/FeedArticleConfiguration.js +80 -80
  29. package/models/FrontStoryChannel.js +59 -59
  30. package/models/FrontStoryChannelV2.js +60 -60
  31. package/models/GenericFlowRequest.js +114 -114
  32. package/models/MidoWebChannel.js +47 -47
  33. package/models/MineChannel.js +42 -42
  34. package/models/Pages.js +92 -92
  35. package/models/PipelineExecution.js +59 -59
  36. package/models/PolicyDogsCreativeCache.js +50 -50
  37. package/models/PolicyDogsImageCache.js +30 -30
  38. package/models/Presets.js +34 -34
  39. package/models/RSOCFeedCampaign.js +375 -375
  40. package/models/RsocKeywordPerformance.js +110 -110
  41. package/models/RuleAction.js +90 -90
  42. package/models/RuleCondition.js +137 -137
  43. package/models/RuleExecution.js +107 -107
  44. package/models/RulesValues.js +56 -56
  45. package/models/SupportedLocale.js +23 -23
  46. package/models/SyncHistory.js +249 -249
  47. package/models/TTQChannel.js +42 -42
  48. package/models/TemplateMetadata.js +260 -260
  49. package/models/Tier2_AdAccounts.js +110 -110
  50. package/models/Tier2_Assets.js +70 -70
  51. package/models/Tier2_BusinessManagers.js +105 -105
  52. package/models/Tier2_CreditLines.js +99 -99
  53. package/models/Tier2_Pages.js +91 -91
  54. package/models/Tier2_Pixels.js +82 -82
  55. package/models/Tier2_Tokens.js +64 -64
  56. package/models/Tier2_UserAdAccounts.js +83 -83
  57. package/models/TokenRotationState.js +121 -121
  58. package/models/TonicRSOCKeywordPerformance.js +122 -122
  59. package/models/Users.js +143 -138
  60. package/models/Vertical.js +25 -25
  61. package/models/newFiles.js +137 -137
  62. package/package.json +19 -21
  63. package/run.sh +214 -214
  64. package/services/sequelizeService.js +110 -110
@@ -1,375 +1,375 @@
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
- mediaBuyer: {
164
- type: DataTypes.STRING,
165
- allowNull: true,
166
- },
167
- Partner: {
168
- type: DataTypes.STRING,
169
- allowNull: true,
170
- comment: "Partner associated with the campaign",
171
- },
172
- language: {
173
- type: DataTypes.STRING,
174
- allowNull: true,
175
- comment: "Language code for the campaign",
176
- },
177
- feed_id: {
178
- type: DataTypes.UUID,
179
- allowNull: true,
180
- comment: "ID referencing DynamicFeed.id for dynamic feeds",
181
- },
182
- },
183
- {
184
- tableName: "rsoc_feed_campaigns",
185
- timestamps: true,
186
- }
187
- );
188
-
189
- // Define associations without enforcing foreign key constraints
190
- RSOCFeedCampaign.associate = (models) => {
191
- if (models.Article) {
192
- RSOCFeedCampaign.belongsTo(models.Article, {
193
- foreignKey: "AGRSAID",
194
- targetKey: "AGRSAID",
195
- as: "Article",
196
- constraints: false,
197
- });
198
- }
199
-
200
- if (models.Channel) {
201
- RSOCFeedCampaign.belongsTo(models.Channel, {
202
- foreignKey: "channelId",
203
- targetKey: "channelId",
204
- as: "Channel",
205
- constraints: false,
206
- });
207
- }
208
-
209
- if (models.DynamicFeed) {
210
- RSOCFeedCampaign.belongsTo(models.DynamicFeed, {
211
- foreignKey: "feed_id",
212
- targetKey: "id",
213
- as: "DynamicFeed",
214
- constraints: false,
215
- });
216
- }
217
- };
218
-
219
- // BeforeCreate hook: Update Channel status and connectedCampaigns
220
- RSOCFeedCampaign.beforeCreate(async (campaign, options) => {
221
- campaign.setDataValue("redirectLink", null);
222
-
223
- // Set default domain and url_structure if not provided
224
- if (!campaign.domain) {
225
- campaign.setDataValue("domain", "sportfoy.com");
226
- }
227
- if (!campaign.url_structure) {
228
- campaign.setDataValue("url_structure", "legacy");
229
- }
230
-
231
- // Auto-adjust url_structure based on domain
232
- if (
233
- campaign.domain === "sportfoy.com" &&
234
- campaign.url_structure === "legacy"
235
- ) {
236
- campaign.setDataValue("url_structure", "unified");
237
- }
238
-
239
- // Skip channel update if in a silent operation (e.g., during sync)
240
- if (options.silent) return;
241
-
242
- try {
243
- const channel = await sequelize.models.Channel.findOne({
244
- where: {
245
- channelId: campaign.channelId,
246
- styleId: campaign.styleId || null,
247
- },
248
- });
249
-
250
- if (channel) {
251
- const connectedCampaigns = channel.connectedCampaigns || [];
252
- if (
253
- !connectedCampaigns.some((c) => c.campaignId === campaign.AGRS_CID)
254
- ) {
255
- connectedCampaigns.push({
256
- campaignId: campaign.AGRS_CID,
257
- assignedAt: new Date(),
258
- releasedAt: null,
259
- });
260
- await channel.update(
261
- {
262
- status: "used",
263
- connectedCampaigns,
264
- },
265
- { silent: true }
266
- );
267
- }
268
- } else {
269
- console.warn(
270
- `Channel not found for channelId: ${campaign.channelId}, styleId: ${campaign.styleId}`
271
- );
272
- }
273
- } catch (error) {
274
- console.error("Error in beforeCreate hook:", error.message);
275
- }
276
- });
277
-
278
- // BeforeUpdate hook: Handle channel reassignment
279
- RSOCFeedCampaign.beforeUpdate(async (campaign, options) => {
280
- campaign.setDataValue("redirectLink", null);
281
-
282
- // Auto-adjust url_structure based on domain if domain changed
283
- if (campaign.changed("domain")) {
284
- if (
285
- campaign.domain === "sportfoy.com" &&
286
- campaign.url_structure === "legacy"
287
- ) {
288
- campaign.setDataValue("url_structure", "unified");
289
- }
290
- }
291
-
292
- // Skip channel update if in a silent operation or no relevant changes
293
- if (
294
- options.silent ||
295
- (!campaign.changed("channelId") && !campaign.changed("styleId"))
296
- ) {
297
- return;
298
- }
299
-
300
- try {
301
- const oldChannelId = campaign.previous("channelId");
302
- const oldStyleId = campaign.previous("styleId") || null;
303
- const newChannelId = campaign.channelId;
304
- const newStyleId = campaign.styleId || null;
305
-
306
- // Release old channel
307
- if (
308
- oldChannelId &&
309
- (oldChannelId !== newChannelId || oldStyleId !== newStyleId)
310
- ) {
311
- const oldChannel = await sequelize.models.Channel.findOne({
312
- where: {
313
- channelId: oldChannelId,
314
- styleId: oldStyleId,
315
- },
316
- });
317
-
318
- if (oldChannel) {
319
- const connectedCampaigns = oldChannel.connectedCampaigns || [];
320
- const updatedCampaigns = connectedCampaigns
321
- .map((c) =>
322
- c.campaignId === campaign.AGRS_CID
323
- ? { ...c, releasedAt: new Date() }
324
- : c
325
- )
326
- .filter((c) => !c.releasedAt);
327
-
328
- await oldChannel.update(
329
- {
330
- connectedCampaigns: updatedCampaigns,
331
- status: updatedCampaigns.length > 0 ? "used" : "free",
332
- },
333
- { silent: true }
334
- );
335
- }
336
- }
337
-
338
- // Assign new channel
339
- const newChannel = await sequelize.models.Channel.findOne({
340
- where: {
341
- channelId: newChannelId,
342
- styleId: newStyleId,
343
- },
344
- });
345
-
346
- if (newChannel) {
347
- const connectedCampaigns = newChannel.connectedCampaigns || [];
348
- if (
349
- !connectedCampaigns.some((c) => c.campaignId === campaign.AGRS_CID)
350
- ) {
351
- connectedCampaigns.push({
352
- campaignId: campaign.AGRS_CID,
353
- assignedAt: new Date(),
354
- releasedAt: null,
355
- });
356
- await newChannel.update(
357
- {
358
- status: "used",
359
- connectedCampaigns,
360
- },
361
- { silent: true }
362
- );
363
- }
364
- } else {
365
- console.warn(
366
- `New channel not found for channelId: ${newChannelId}, styleId: ${newStyleId}`
367
- );
368
- }
369
- } catch (error) {
370
- console.error("Error in beforeUpdate hook:", error.message);
371
- }
372
- });
373
-
374
- return RSOCFeedCampaign;
375
- };
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
+ mediaBuyer: {
164
+ type: DataTypes.STRING,
165
+ allowNull: true,
166
+ },
167
+ Partner: {
168
+ type: DataTypes.STRING,
169
+ allowNull: true,
170
+ comment: "Partner associated with the campaign",
171
+ },
172
+ language: {
173
+ type: DataTypes.STRING,
174
+ allowNull: true,
175
+ comment: "Language code for the campaign",
176
+ },
177
+ feed_id: {
178
+ type: DataTypes.UUID,
179
+ allowNull: true,
180
+ comment: "ID referencing DynamicFeed.id for dynamic feeds",
181
+ },
182
+ },
183
+ {
184
+ tableName: "rsoc_feed_campaigns",
185
+ timestamps: true,
186
+ }
187
+ );
188
+
189
+ // Define associations without enforcing foreign key constraints
190
+ RSOCFeedCampaign.associate = (models) => {
191
+ if (models.Article) {
192
+ RSOCFeedCampaign.belongsTo(models.Article, {
193
+ foreignKey: "AGRSAID",
194
+ targetKey: "AGRSAID",
195
+ as: "Article",
196
+ constraints: false,
197
+ });
198
+ }
199
+
200
+ if (models.Channel) {
201
+ RSOCFeedCampaign.belongsTo(models.Channel, {
202
+ foreignKey: "channelId",
203
+ targetKey: "channelId",
204
+ as: "Channel",
205
+ constraints: false,
206
+ });
207
+ }
208
+
209
+ if (models.DynamicFeed) {
210
+ RSOCFeedCampaign.belongsTo(models.DynamicFeed, {
211
+ foreignKey: "feed_id",
212
+ targetKey: "id",
213
+ as: "DynamicFeed",
214
+ constraints: false,
215
+ });
216
+ }
217
+ };
218
+
219
+ // BeforeCreate hook: Update Channel status and connectedCampaigns
220
+ RSOCFeedCampaign.beforeCreate(async (campaign, options) => {
221
+ campaign.setDataValue("redirectLink", null);
222
+
223
+ // Set default domain and url_structure if not provided
224
+ if (!campaign.domain) {
225
+ campaign.setDataValue("domain", "sportfoy.com");
226
+ }
227
+ if (!campaign.url_structure) {
228
+ campaign.setDataValue("url_structure", "legacy");
229
+ }
230
+
231
+ // Auto-adjust url_structure based on domain
232
+ if (
233
+ campaign.domain === "sportfoy.com" &&
234
+ campaign.url_structure === "legacy"
235
+ ) {
236
+ campaign.setDataValue("url_structure", "unified");
237
+ }
238
+
239
+ // Skip channel update if in a silent operation (e.g., during sync)
240
+ if (options.silent) return;
241
+
242
+ try {
243
+ const channel = await sequelize.models.Channel.findOne({
244
+ where: {
245
+ channelId: campaign.channelId,
246
+ styleId: campaign.styleId || null,
247
+ },
248
+ });
249
+
250
+ if (channel) {
251
+ const connectedCampaigns = channel.connectedCampaigns || [];
252
+ if (
253
+ !connectedCampaigns.some((c) => c.campaignId === campaign.AGRS_CID)
254
+ ) {
255
+ connectedCampaigns.push({
256
+ campaignId: campaign.AGRS_CID,
257
+ assignedAt: new Date(),
258
+ releasedAt: null,
259
+ });
260
+ await channel.update(
261
+ {
262
+ status: "used",
263
+ connectedCampaigns,
264
+ },
265
+ { silent: true }
266
+ );
267
+ }
268
+ } else {
269
+ console.warn(
270
+ `Channel not found for channelId: ${campaign.channelId}, styleId: ${campaign.styleId}`
271
+ );
272
+ }
273
+ } catch (error) {
274
+ console.error("Error in beforeCreate hook:", error.message);
275
+ }
276
+ });
277
+
278
+ // BeforeUpdate hook: Handle channel reassignment
279
+ RSOCFeedCampaign.beforeUpdate(async (campaign, options) => {
280
+ campaign.setDataValue("redirectLink", null);
281
+
282
+ // Auto-adjust url_structure based on domain if domain changed
283
+ if (campaign.changed("domain")) {
284
+ if (
285
+ campaign.domain === "sportfoy.com" &&
286
+ campaign.url_structure === "legacy"
287
+ ) {
288
+ campaign.setDataValue("url_structure", "unified");
289
+ }
290
+ }
291
+
292
+ // Skip channel update if in a silent operation or no relevant changes
293
+ if (
294
+ options.silent ||
295
+ (!campaign.changed("channelId") && !campaign.changed("styleId"))
296
+ ) {
297
+ return;
298
+ }
299
+
300
+ try {
301
+ const oldChannelId = campaign.previous("channelId");
302
+ const oldStyleId = campaign.previous("styleId") || null;
303
+ const newChannelId = campaign.channelId;
304
+ const newStyleId = campaign.styleId || null;
305
+
306
+ // Release old channel
307
+ if (
308
+ oldChannelId &&
309
+ (oldChannelId !== newChannelId || oldStyleId !== newStyleId)
310
+ ) {
311
+ const oldChannel = await sequelize.models.Channel.findOne({
312
+ where: {
313
+ channelId: oldChannelId,
314
+ styleId: oldStyleId,
315
+ },
316
+ });
317
+
318
+ if (oldChannel) {
319
+ const connectedCampaigns = oldChannel.connectedCampaigns || [];
320
+ const updatedCampaigns = connectedCampaigns
321
+ .map((c) =>
322
+ c.campaignId === campaign.AGRS_CID
323
+ ? { ...c, releasedAt: new Date() }
324
+ : c
325
+ )
326
+ .filter((c) => !c.releasedAt);
327
+
328
+ await oldChannel.update(
329
+ {
330
+ connectedCampaigns: updatedCampaigns,
331
+ status: updatedCampaigns.length > 0 ? "used" : "free",
332
+ },
333
+ { silent: true }
334
+ );
335
+ }
336
+ }
337
+
338
+ // Assign new channel
339
+ const newChannel = await sequelize.models.Channel.findOne({
340
+ where: {
341
+ channelId: newChannelId,
342
+ styleId: newStyleId,
343
+ },
344
+ });
345
+
346
+ if (newChannel) {
347
+ const connectedCampaigns = newChannel.connectedCampaigns || [];
348
+ if (
349
+ !connectedCampaigns.some((c) => c.campaignId === campaign.AGRS_CID)
350
+ ) {
351
+ connectedCampaigns.push({
352
+ campaignId: campaign.AGRS_CID,
353
+ assignedAt: new Date(),
354
+ releasedAt: null,
355
+ });
356
+ await newChannel.update(
357
+ {
358
+ status: "used",
359
+ connectedCampaigns,
360
+ },
361
+ { silent: true }
362
+ );
363
+ }
364
+ } else {
365
+ console.warn(
366
+ `New channel not found for channelId: ${newChannelId}, styleId: ${newStyleId}`
367
+ );
368
+ }
369
+ } catch (error) {
370
+ console.error("Error in beforeUpdate hook:", error.message);
371
+ }
372
+ });
373
+
374
+ return RSOCFeedCampaign;
375
+ };