agrs-sequelize-sdk 1.2.39 → 1.2.41

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.
@@ -1,347 +1,347 @@
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
- adTitle: {
101
- type: DataTypes.STRING,
102
- allowNull: true,
103
- },
104
- domain: {
105
- type: DataTypes.STRING,
106
- allowNull: true,
107
- defaultValue: "sportfoy.com",
108
- comment:
109
- "Target domain for campaign URLs (searchlabz.com or sportfoy.com)",
110
- },
111
- url_structure: {
112
- type: DataTypes.ENUM("legacy", "unified"),
113
- allowNull: false,
114
- defaultValue: "legacy",
115
- comment:
116
- "URL structure type: legacy (slug-based) or unified (search-based)",
117
- },
118
- feedProvider: {
119
- type: DataTypes.STRING,
120
- allowNull: false,
121
- defaultValue: "RSOC",
122
- },
123
- createdCampaignAt: {
124
- type: DataTypes.DATE,
125
- allowNull: true,
126
- defaultValue: DataTypes.NOW,
127
- },
128
- draft: {
129
- type: DataTypes.BOOLEAN,
130
- allowNull: true,
131
- defaultValue: false,
132
- },
133
- openedFromDashboard: {
134
- type: DataTypes.BOOLEAN,
135
- allowNull: true,
136
- defaultValue: null,
137
- comment:
138
- "Tracks if the campaign was opened/viewed from the dashboard, allow null for campaigns created before this field was added",
139
- },
140
- createdAt: {
141
- type: DataTypes.DATE,
142
- allowNull: false,
143
- defaultValue: DataTypes.NOW,
144
- },
145
- updatedAt: {
146
- type: DataTypes.DATE,
147
- allowNull: false,
148
- defaultValue: DataTypes.NOW,
149
- },
150
- platform: {
151
- type: DataTypes.STRING,
152
- allowNull: false,
153
- },
154
- mediaBuyer: {
155
- type: DataTypes.STRING,
156
- allowNull: true,
157
- },
158
- language: {
159
- type: DataTypes.STRING,
160
- allowNull: true,
161
- comment: "Language code for the campaign",
162
- },
163
- },
164
- {
165
- tableName: "rsoc_feed_campaigns",
166
- timestamps: true,
167
- }
168
- );
169
-
170
- // Define associations without enforcing foreign key constraints
171
- RSOCFeedCampaign.associate = (models) => {
172
- if (models.Article) {
173
- RSOCFeedCampaign.belongsTo(models.Article, {
174
- foreignKey: "AGRSAID",
175
- targetKey: "AGRSAID",
176
- as: "Article",
177
- constraints: false,
178
- });
179
- }
180
-
181
- if (models.Channel) {
182
- RSOCFeedCampaign.belongsTo(models.Channel, {
183
- foreignKey: "channelId",
184
- targetKey: "channelId",
185
- as: "Channel",
186
- constraints: false,
187
- });
188
- }
189
- };
190
-
191
- // BeforeCreate hook: Update Channel status and connectedCampaigns
192
- RSOCFeedCampaign.beforeCreate(async (campaign, options) => {
193
- campaign.setDataValue("redirectLink", null);
194
-
195
- // Set default domain and url_structure if not provided
196
- if (!campaign.domain) {
197
- campaign.setDataValue("domain", "sportfoy.com");
198
- }
199
- if (!campaign.url_structure) {
200
- campaign.setDataValue("url_structure", "legacy");
201
- }
202
-
203
- // Auto-adjust url_structure based on domain
204
- if (
205
- campaign.domain === "sportfoy.com" &&
206
- campaign.url_structure === "legacy"
207
- ) {
208
- campaign.setDataValue("url_structure", "unified");
209
- }
210
-
211
- // Skip channel update if in a silent operation (e.g., during sync)
212
- if (options.silent) return;
213
-
214
- try {
215
- const channel = await sequelize.models.Channel.findOne({
216
- where: {
217
- channelId: campaign.channelId,
218
- styleId: campaign.styleId || null,
219
- },
220
- });
221
-
222
- if (channel) {
223
- const connectedCampaigns = channel.connectedCampaigns || [];
224
- if (
225
- !connectedCampaigns.some((c) => c.campaignId === campaign.AGRS_CID)
226
- ) {
227
- connectedCampaigns.push({
228
- campaignId: campaign.AGRS_CID,
229
- assignedAt: new Date(),
230
- releasedAt: null,
231
- });
232
- await channel.update(
233
- {
234
- status: "used",
235
- connectedCampaigns,
236
- },
237
- { silent: true }
238
- );
239
- }
240
- } else {
241
- console.warn(
242
- `Channel not found for channelId: ${campaign.channelId}, styleId: ${campaign.styleId}`
243
- );
244
- }
245
- } catch (error) {
246
- console.error("Error in beforeCreate hook:", error.message);
247
- }
248
- });
249
-
250
- // BeforeUpdate hook: Handle channel reassignment
251
- RSOCFeedCampaign.beforeUpdate(async (campaign, options) => {
252
- campaign.setDataValue("redirectLink", null);
253
-
254
- // Auto-adjust url_structure based on domain if domain changed
255
- if (campaign.changed("domain")) {
256
- if (
257
- campaign.domain === "sportfoy.com" &&
258
- campaign.url_structure === "legacy"
259
- ) {
260
- campaign.setDataValue("url_structure", "unified");
261
- }
262
- }
263
-
264
- // Skip channel update if in a silent operation or no relevant changes
265
- if (
266
- options.silent ||
267
- (!campaign.changed("channelId") && !campaign.changed("styleId"))
268
- ) {
269
- return;
270
- }
271
-
272
- try {
273
- const oldChannelId = campaign.previous("channelId");
274
- const oldStyleId = campaign.previous("styleId") || null;
275
- const newChannelId = campaign.channelId;
276
- const newStyleId = campaign.styleId || null;
277
-
278
- // Release old channel
279
- if (
280
- oldChannelId &&
281
- (oldChannelId !== newChannelId || oldStyleId !== newStyleId)
282
- ) {
283
- const oldChannel = await sequelize.models.Channel.findOne({
284
- where: {
285
- channelId: oldChannelId,
286
- styleId: oldStyleId,
287
- },
288
- });
289
-
290
- if (oldChannel) {
291
- const connectedCampaigns = oldChannel.connectedCampaigns || [];
292
- const updatedCampaigns = connectedCampaigns
293
- .map((c) =>
294
- c.campaignId === campaign.AGRS_CID
295
- ? { ...c, releasedAt: new Date() }
296
- : c
297
- )
298
- .filter((c) => !c.releasedAt);
299
-
300
- await oldChannel.update(
301
- {
302
- connectedCampaigns: updatedCampaigns,
303
- status: updatedCampaigns.length > 0 ? "used" : "free",
304
- },
305
- { silent: true }
306
- );
307
- }
308
- }
309
-
310
- // Assign new channel
311
- const newChannel = await sequelize.models.Channel.findOne({
312
- where: {
313
- channelId: newChannelId,
314
- styleId: newStyleId,
315
- },
316
- });
317
-
318
- if (newChannel) {
319
- const connectedCampaigns = newChannel.connectedCampaigns || [];
320
- if (
321
- !connectedCampaigns.some((c) => c.campaignId === campaign.AGRS_CID)
322
- ) {
323
- connectedCampaigns.push({
324
- campaignId: campaign.AGRS_CID,
325
- assignedAt: new Date(),
326
- releasedAt: null,
327
- });
328
- await newChannel.update(
329
- {
330
- status: "used",
331
- connectedCampaigns,
332
- },
333
- { silent: true }
334
- );
335
- }
336
- } else {
337
- console.warn(
338
- `New channel not found for channelId: ${newChannelId}, styleId: ${newStyleId}`
339
- );
340
- }
341
- } catch (error) {
342
- console.error("Error in beforeUpdate hook:", error.message);
343
- }
344
- });
345
-
346
- return RSOCFeedCampaign;
347
- };
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
+ adTitle: {
101
+ type: DataTypes.STRING,
102
+ allowNull: true,
103
+ },
104
+ domain: {
105
+ type: DataTypes.STRING,
106
+ allowNull: true,
107
+ defaultValue: "sportfoy.com",
108
+ comment:
109
+ "Target domain for campaign URLs (searchlabz.com or sportfoy.com)",
110
+ },
111
+ url_structure: {
112
+ type: DataTypes.ENUM("legacy", "unified"),
113
+ allowNull: false,
114
+ defaultValue: "legacy",
115
+ comment:
116
+ "URL structure type: legacy (slug-based) or unified (search-based)",
117
+ },
118
+ feedProvider: {
119
+ type: DataTypes.STRING,
120
+ allowNull: false,
121
+ defaultValue: "RSOC",
122
+ },
123
+ createdCampaignAt: {
124
+ type: DataTypes.DATE,
125
+ allowNull: true,
126
+ defaultValue: DataTypes.NOW,
127
+ },
128
+ draft: {
129
+ type: DataTypes.BOOLEAN,
130
+ allowNull: true,
131
+ defaultValue: false,
132
+ },
133
+ openedFromDashboard: {
134
+ type: DataTypes.BOOLEAN,
135
+ allowNull: true,
136
+ defaultValue: null,
137
+ comment:
138
+ "Tracks if the campaign was opened/viewed from the dashboard, allow null for campaigns created before this field was added",
139
+ },
140
+ createdAt: {
141
+ type: DataTypes.DATE,
142
+ allowNull: false,
143
+ defaultValue: DataTypes.NOW,
144
+ },
145
+ updatedAt: {
146
+ type: DataTypes.DATE,
147
+ allowNull: false,
148
+ defaultValue: DataTypes.NOW,
149
+ },
150
+ platform: {
151
+ type: DataTypes.STRING,
152
+ allowNull: false,
153
+ },
154
+ mediaBuyer: {
155
+ type: DataTypes.STRING,
156
+ allowNull: true,
157
+ },
158
+ language: {
159
+ type: DataTypes.STRING,
160
+ allowNull: true,
161
+ comment: "Language code for the campaign",
162
+ },
163
+ },
164
+ {
165
+ tableName: "rsoc_feed_campaigns",
166
+ timestamps: true,
167
+ }
168
+ );
169
+
170
+ // Define associations without enforcing foreign key constraints
171
+ RSOCFeedCampaign.associate = (models) => {
172
+ if (models.Article) {
173
+ RSOCFeedCampaign.belongsTo(models.Article, {
174
+ foreignKey: "AGRSAID",
175
+ targetKey: "AGRSAID",
176
+ as: "Article",
177
+ constraints: false,
178
+ });
179
+ }
180
+
181
+ if (models.Channel) {
182
+ RSOCFeedCampaign.belongsTo(models.Channel, {
183
+ foreignKey: "channelId",
184
+ targetKey: "channelId",
185
+ as: "Channel",
186
+ constraints: false,
187
+ });
188
+ }
189
+ };
190
+
191
+ // BeforeCreate hook: Update Channel status and connectedCampaigns
192
+ RSOCFeedCampaign.beforeCreate(async (campaign, options) => {
193
+ campaign.setDataValue("redirectLink", null);
194
+
195
+ // Set default domain and url_structure if not provided
196
+ if (!campaign.domain) {
197
+ campaign.setDataValue("domain", "sportfoy.com");
198
+ }
199
+ if (!campaign.url_structure) {
200
+ campaign.setDataValue("url_structure", "legacy");
201
+ }
202
+
203
+ // Auto-adjust url_structure based on domain
204
+ if (
205
+ campaign.domain === "sportfoy.com" &&
206
+ campaign.url_structure === "legacy"
207
+ ) {
208
+ campaign.setDataValue("url_structure", "unified");
209
+ }
210
+
211
+ // Skip channel update if in a silent operation (e.g., during sync)
212
+ if (options.silent) return;
213
+
214
+ try {
215
+ const channel = await sequelize.models.Channel.findOne({
216
+ where: {
217
+ channelId: campaign.channelId,
218
+ styleId: campaign.styleId || null,
219
+ },
220
+ });
221
+
222
+ if (channel) {
223
+ const connectedCampaigns = channel.connectedCampaigns || [];
224
+ if (
225
+ !connectedCampaigns.some((c) => c.campaignId === campaign.AGRS_CID)
226
+ ) {
227
+ connectedCampaigns.push({
228
+ campaignId: campaign.AGRS_CID,
229
+ assignedAt: new Date(),
230
+ releasedAt: null,
231
+ });
232
+ await channel.update(
233
+ {
234
+ status: "used",
235
+ connectedCampaigns,
236
+ },
237
+ { silent: true }
238
+ );
239
+ }
240
+ } else {
241
+ console.warn(
242
+ `Channel not found for channelId: ${campaign.channelId}, styleId: ${campaign.styleId}`
243
+ );
244
+ }
245
+ } catch (error) {
246
+ console.error("Error in beforeCreate hook:", error.message);
247
+ }
248
+ });
249
+
250
+ // BeforeUpdate hook: Handle channel reassignment
251
+ RSOCFeedCampaign.beforeUpdate(async (campaign, options) => {
252
+ campaign.setDataValue("redirectLink", null);
253
+
254
+ // Auto-adjust url_structure based on domain if domain changed
255
+ if (campaign.changed("domain")) {
256
+ if (
257
+ campaign.domain === "sportfoy.com" &&
258
+ campaign.url_structure === "legacy"
259
+ ) {
260
+ campaign.setDataValue("url_structure", "unified");
261
+ }
262
+ }
263
+
264
+ // Skip channel update if in a silent operation or no relevant changes
265
+ if (
266
+ options.silent ||
267
+ (!campaign.changed("channelId") && !campaign.changed("styleId"))
268
+ ) {
269
+ return;
270
+ }
271
+
272
+ try {
273
+ const oldChannelId = campaign.previous("channelId");
274
+ const oldStyleId = campaign.previous("styleId") || null;
275
+ const newChannelId = campaign.channelId;
276
+ const newStyleId = campaign.styleId || null;
277
+
278
+ // Release old channel
279
+ if (
280
+ oldChannelId &&
281
+ (oldChannelId !== newChannelId || oldStyleId !== newStyleId)
282
+ ) {
283
+ const oldChannel = await sequelize.models.Channel.findOne({
284
+ where: {
285
+ channelId: oldChannelId,
286
+ styleId: oldStyleId,
287
+ },
288
+ });
289
+
290
+ if (oldChannel) {
291
+ const connectedCampaigns = oldChannel.connectedCampaigns || [];
292
+ const updatedCampaigns = connectedCampaigns
293
+ .map((c) =>
294
+ c.campaignId === campaign.AGRS_CID
295
+ ? { ...c, releasedAt: new Date() }
296
+ : c
297
+ )
298
+ .filter((c) => !c.releasedAt);
299
+
300
+ await oldChannel.update(
301
+ {
302
+ connectedCampaigns: updatedCampaigns,
303
+ status: updatedCampaigns.length > 0 ? "used" : "free",
304
+ },
305
+ { silent: true }
306
+ );
307
+ }
308
+ }
309
+
310
+ // Assign new channel
311
+ const newChannel = await sequelize.models.Channel.findOne({
312
+ where: {
313
+ channelId: newChannelId,
314
+ styleId: newStyleId,
315
+ },
316
+ });
317
+
318
+ if (newChannel) {
319
+ const connectedCampaigns = newChannel.connectedCampaigns || [];
320
+ if (
321
+ !connectedCampaigns.some((c) => c.campaignId === campaign.AGRS_CID)
322
+ ) {
323
+ connectedCampaigns.push({
324
+ campaignId: campaign.AGRS_CID,
325
+ assignedAt: new Date(),
326
+ releasedAt: null,
327
+ });
328
+ await newChannel.update(
329
+ {
330
+ status: "used",
331
+ connectedCampaigns,
332
+ },
333
+ { silent: true }
334
+ );
335
+ }
336
+ } else {
337
+ console.warn(
338
+ `New channel not found for channelId: ${newChannelId}, styleId: ${newStyleId}`
339
+ );
340
+ }
341
+ } catch (error) {
342
+ console.error("Error in beforeUpdate hook:", error.message);
343
+ }
344
+ });
345
+
346
+ return RSOCFeedCampaign;
347
+ };