agrs-sequelize-sdk 1.1.53 → 1.1.54

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 (2) hide show
  1. package/models/Channel.js +201 -16
  2. package/package.json +1 -1
package/models/Channel.js CHANGED
@@ -1,45 +1,230 @@
1
- // models/Channel.js
2
1
  module.exports = (sequelize, DataTypes) => {
3
- const Channel = sequelize.define(
4
- "Channel",
2
+ const RSOCFeedCampaign = sequelize.define(
3
+ "RSOCFeedCampaign",
5
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: false,
14
+ comment: "Foreign key referencing Article.AGRSAID",
15
+ },
16
+ articleName: {
17
+ type: DataTypes.STRING,
18
+ allowNull: false,
19
+ comment: "Name of the associated article",
20
+ },
6
21
  channelId: {
7
22
  type: DataTypes.STRING,
8
23
  allowNull: false,
9
- unique: false, // Explicitly set to false to override any default
24
+ comment: "Foreign key referencing Channel.channelId",
10
25
  },
11
26
  styleId: {
27
+ type: DataTypes.STRING,
28
+ allowNull: true,
29
+ comment: "Style ID (stid) inherited from Channel",
30
+ },
31
+ assignee: {
12
32
  type: DataTypes.STRING,
13
33
  allowNull: false,
14
34
  },
15
35
  status: {
16
36
  type: DataTypes.STRING,
17
37
  allowNull: false,
18
- defaultValue: "free",
38
+ defaultValue: "active",
19
39
  validate: {
20
- isIn: [["free", "used", "archived"]],
40
+ isIn: [["active", "inactive", "archived"]],
21
41
  },
22
42
  },
23
- connectedCampaigns: {
24
- type: DataTypes.JSONB,
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
+ link: {
69
+ type: DataTypes.STRING(1024),
70
+ allowNull: true,
71
+ comment: "Campaign-specific URL based on article URL with parameters",
72
+ },
73
+ redirectLink: {
74
+ type: DataTypes.STRING(1024),
75
+ allowNull: true,
76
+ },
77
+ pixelId: {
78
+ type: DataTypes.STRING(1024),
79
+ allowNull: true,
80
+ },
81
+ token: {
82
+ type: DataTypes.STRING(1024),
83
+ allowNull: true,
84
+ },
85
+ accountId: {
86
+ type: DataTypes.STRING,
87
+ allowNull: false,
88
+ },
89
+ feedName: {
90
+ type: DataTypes.STRING,
91
+ allowNull: true,
92
+ defaultValue: "RSOC",
93
+ },
94
+ adTitle: {
95
+ type: DataTypes.STRING,
96
+ allowNull: true,
97
+ },
98
+ domain: {
99
+ type: DataTypes.STRING,
100
+ allowNull: true,
101
+ },
102
+ feedProvider: {
103
+ type: DataTypes.STRING,
104
+ allowNull: false,
105
+ defaultValue: "RSOC",
106
+ },
107
+ createdCampaignAt: {
108
+ type: DataTypes.DATE,
109
+ allowNull: true,
110
+ defaultValue: DataTypes.NOW,
111
+ },
112
+ draft: {
113
+ type: DataTypes.BOOLEAN,
114
+ allowNull: true,
115
+ defaultValue: false,
116
+ },
117
+ createdAt: {
118
+ type: DataTypes.DATE,
119
+ defaultValue: DataTypes.NOW,
120
+ },
121
+ updatedAt: {
122
+ type: DataTypes.DATE,
123
+ defaultValue: DataTypes.NOW,
124
+ },
125
+ platform: {
126
+ type: DataTypes.STRING,
25
127
  allowNull: false,
26
- defaultValue: [],
128
+ },
129
+ mediaBuyer: {
130
+ type: DataTypes.STRING,
131
+ allowNull: true,
27
132
  },
28
133
  },
29
134
  {
30
- tableName: "channels",
135
+ tableName: "rsoc_feed_campaigns",
31
136
  timestamps: true,
32
- // Remove any existing indexes here - we'll add the constraint after table creation
33
137
  }
34
138
  );
35
139
 
36
- Channel.associate = (models) => {
37
- Channel.hasMany(models.RSOCFeedCampaign, {
140
+ RSOCFeedCampaign.associate = (models) => {
141
+ RSOCFeedCampaign.belongsTo(models.Article, {
142
+ foreignKey: "AGRSAID",
143
+ targetKey: "AGRSAID",
144
+ as: "Article",
145
+ constraints: false, // Disable foreign key constraint
146
+ });
147
+
148
+ RSOCFeedCampaign.belongsTo(models.Channel, {
38
149
  foreignKey: "channelId",
39
- sourceKey: "channelId",
40
- as: "RSOCFeedCampaigns",
150
+ targetKey: "channelId",
151
+ as: "Channel",
152
+ constraints: false, // Disable foreign key constraint
41
153
  });
42
154
  };
43
155
 
44
- return Channel;
156
+ RSOCFeedCampaign.beforeCreate(async (campaign) => {
157
+ // Set redirectLink to null as requested
158
+ campaign.setDataValue("redirectLink", null);
159
+
160
+ const channel = await sequelize.models.Channel.findOne({
161
+ where: { channelId: campaign.channelId, styleId: campaign.styleId },
162
+ });
163
+
164
+ if (channel) {
165
+ const connectedCampaigns = channel.connectedCampaigns || [];
166
+ if (!connectedCampaigns.some((c) => c.campaignId === campaign.AGRS_CID)) {
167
+ connectedCampaigns.push({
168
+ campaignId: campaign.AGRS_CID,
169
+ assignedAt: new Date(),
170
+ releasedAt: null,
171
+ });
172
+ await channel.update({ status: "used", connectedCampaigns });
173
+ }
174
+ }
175
+ });
176
+
177
+ RSOCFeedCampaign.beforeUpdate(async (campaign) => {
178
+ // Ensure redirectLink remains null
179
+ campaign.setDataValue("redirectLink", null);
180
+
181
+ if (campaign.changed("channelId") || campaign.changed("styleId")) {
182
+ const oldChannelId = campaign.previous("channelId");
183
+ const oldStyleId = campaign.previous("styleId");
184
+ const newChannelId = campaign.channelId;
185
+ const newStyleId = campaign.styleId;
186
+
187
+ if (oldChannelId) {
188
+ const oldChannel = await sequelize.models.Channel.findOne({
189
+ where: { channelId: oldChannelId, styleId: oldStyleId },
190
+ });
191
+
192
+ if (oldChannel) {
193
+ const connectedCampaigns = oldChannel.connectedCampaigns || [];
194
+ const updatedCampaigns = connectedCampaigns
195
+ .map((c) =>
196
+ c.campaignId === campaign.AGRS_CID
197
+ ? { ...c, releasedAt: new Date() }
198
+ : c
199
+ )
200
+ .filter((c) => c.releasedAt === null);
201
+
202
+ await oldChannel.update({
203
+ connectedCampaigns: updatedCampaigns,
204
+ status: updatedCampaigns.length > 0 ? "used" : "free",
205
+ });
206
+ }
207
+ }
208
+
209
+ const newChannel = await sequelize.models.Channel.findOne({
210
+ where: { channelId: newChannelId, styleId: newStyleId },
211
+ });
212
+
213
+ if (newChannel) {
214
+ const connectedCampaigns = newChannel.connectedCampaigns || [];
215
+ if (
216
+ !connectedCampaigns.some((c) => c.campaignId === campaign.AGRS_CID)
217
+ ) {
218
+ connectedCampaigns.push({
219
+ campaignId: campaign.AGRS_CID,
220
+ assignedAt: new Date(),
221
+ releasedAt: null,
222
+ });
223
+ await newChannel.update({ status: "used", connectedCampaigns });
224
+ }
225
+ }
226
+ }
227
+ });
228
+
229
+ return RSOCFeedCampaign;
45
230
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agrs-sequelize-sdk",
3
- "version": "1.1.53",
3
+ "version": "1.1.54",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "start": "node index.js",