agrs-sequelize-sdk 1.2.19 → 1.2.21

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