agrs-sequelize-sdk 1.1.54 → 1.1.56

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.
@@ -11,7 +11,7 @@ module.exports = (sequelize, DataTypes) => {
11
11
  AGRSAID: {
12
12
  type: DataTypes.STRING,
13
13
  allowNull: false,
14
- comment: "Foreign key referencing Article.AGRSAID",
14
+ comment: "ID referencing Article.AGRSAID",
15
15
  },
16
16
  articleName: {
17
17
  type: DataTypes.STRING,
@@ -21,7 +21,7 @@ module.exports = (sequelize, DataTypes) => {
21
21
  channelId: {
22
22
  type: DataTypes.STRING,
23
23
  allowNull: false,
24
- comment: "Foreign key referencing Channel.channelId",
24
+ comment: "ID referencing Channel.channelId",
25
25
  },
26
26
  styleId: {
27
27
  type: DataTypes.STRING,
@@ -134,88 +134,157 @@ module.exports = (sequelize, DataTypes) => {
134
134
  {
135
135
  tableName: "rsoc_feed_campaigns",
136
136
  timestamps: true,
137
+ // Entirely disable automatic foreign key creation
138
+ references: false,
137
139
  }
138
140
  );
139
141
 
140
- RSOCFeedCampaign.associate = (models) => {
141
- RSOCFeedCampaign.belongsTo(models.Article, {
142
- foreignKey: "AGRSAID",
143
- targetKey: "AGRSAID",
144
- as: "Article",
145
- constraints: false,
146
- });
142
+ // Override the standard associate method to use non-constraint associations
143
+ RSOCFeedCampaign.associate = function (models) {
144
+ // Define associations without foreign key constraints
145
+ const queryOptions = { constraints: false };
147
146
 
148
- RSOCFeedCampaign.belongsTo(models.Channel, {
149
- foreignKey: "channelId",
150
- targetKey: "channelId",
151
- as: "Channel",
152
- constraints: false,
153
- });
147
+ // Associate with Article model
148
+ if (models.Article) {
149
+ RSOCFeedCampaign.belongsTo(models.Article, {
150
+ foreignKey: "AGRSAID",
151
+ targetKey: "AGRSAID",
152
+ as: "Article",
153
+ ...queryOptions,
154
+ });
155
+ }
156
+
157
+ // Associate with Channel model
158
+ if (models.Channel) {
159
+ RSOCFeedCampaign.belongsTo(models.Channel, {
160
+ foreignKey: "channelId",
161
+ targetKey: "channelId",
162
+ as: "Channel",
163
+ ...queryOptions,
164
+ });
165
+ }
154
166
  };
155
167
 
156
- RSOCFeedCampaign.beforeCreate(async (campaign) => {
168
+ RSOCFeedCampaign.beforeCreate(async (campaign, options) => {
157
169
  // Set redirectLink to null as requested
158
170
  campaign.setDataValue("redirectLink", null);
159
171
 
160
- const channel = await sequelize.models.Channel.findOne({
161
- where: { channelId: campaign.channelId },
162
- });
163
- if (channel) {
164
- const connectedCampaigns = channel.connectedCampaigns || [];
165
- if (!connectedCampaigns.some((c) => c.campaignId === campaign.AGRS_CID)) {
166
- connectedCampaigns.push({
167
- campaignId: campaign.AGRS_CID,
168
- assignedAt: new Date(),
169
- releasedAt: null,
172
+ // Only update the channel if not in the middle of a sync operation
173
+ if (!options.silent) {
174
+ try {
175
+ const channel = await sequelize.models.Channel.findOne({
176
+ where: {
177
+ channelId: campaign.channelId,
178
+ styleId: campaign.styleId,
179
+ },
170
180
  });
171
- await channel.update({ status: "used", connectedCampaigns });
181
+
182
+ if (channel) {
183
+ const connectedCampaigns = channel.connectedCampaigns || [];
184
+ if (
185
+ !connectedCampaigns.some((c) => c.campaignId === campaign.AGRS_CID)
186
+ ) {
187
+ connectedCampaigns.push({
188
+ campaignId: campaign.AGRS_CID,
189
+ assignedAt: new Date(),
190
+ releasedAt: null,
191
+ });
192
+ await channel.update(
193
+ {
194
+ status: "used",
195
+ connectedCampaigns,
196
+ },
197
+ { silent: true }
198
+ );
199
+ }
200
+ }
201
+ } catch (error) {
202
+ console.error(
203
+ "Error updating channel in beforeCreate hook:",
204
+ error.message
205
+ );
206
+ // Continue with campaign creation even if channel update fails
172
207
  }
173
208
  }
174
209
  });
175
210
 
176
- RSOCFeedCampaign.beforeUpdate(async (campaign) => {
211
+ RSOCFeedCampaign.beforeUpdate(async (campaign, options) => {
177
212
  // Ensure redirectLink remains null
178
213
  campaign.setDataValue("redirectLink", null);
179
214
 
180
- if (campaign.changed("channelId")) {
181
- const oldChannelId = campaign.previous("channelId");
182
- const newChannelId = campaign.channelId;
215
+ // Only update the channels if not in the middle of a sync operation
216
+ if (
217
+ !options.silent &&
218
+ (campaign.changed("channelId") || campaign.changed("styleId"))
219
+ ) {
220
+ try {
221
+ const oldChannelId = campaign.previous("channelId");
222
+ const oldStyleId = campaign.previous("styleId");
223
+ const newChannelId = campaign.channelId;
224
+ const newStyleId = campaign.styleId;
183
225
 
184
- if (oldChannelId) {
185
- const oldChannel = await sequelize.models.Channel.findOne({
186
- where: { channelId: oldChannelId },
187
- });
188
- if (oldChannel) {
189
- const connectedCampaigns = oldChannel.connectedCampaigns || [];
190
- const updatedCampaigns = connectedCampaigns
191
- .map((c) =>
192
- c.campaignId === campaign.AGRS_CID
193
- ? { ...c, releasedAt: new Date() }
194
- : c
195
- )
196
- .filter((c) => c.releasedAt === null);
197
- await oldChannel.update({
198
- connectedCampaigns: updatedCampaigns,
199
- status: updatedCampaigns.length > 0 ? "used" : "free",
226
+ // Update old channel if exists
227
+ if (oldChannelId) {
228
+ const oldChannel = await sequelize.models.Channel.findOne({
229
+ where: {
230
+ channelId: oldChannelId,
231
+ styleId: oldStyleId,
232
+ },
200
233
  });
234
+
235
+ if (oldChannel) {
236
+ const connectedCampaigns = oldChannel.connectedCampaigns || [];
237
+ const updatedCampaigns = connectedCampaigns
238
+ .map((c) =>
239
+ c.campaignId === campaign.AGRS_CID
240
+ ? { ...c, releasedAt: new Date() }
241
+ : c
242
+ )
243
+ .filter((c) => c.releasedAt === null);
244
+
245
+ await oldChannel.update(
246
+ {
247
+ connectedCampaigns: updatedCampaigns,
248
+ status: updatedCampaigns.length > 0 ? "used" : "free",
249
+ },
250
+ { silent: true }
251
+ );
252
+ }
201
253
  }
202
- }
203
254
 
204
- const newChannel = await sequelize.models.Channel.findOne({
205
- where: { channelId: newChannelId },
206
- });
207
- if (newChannel) {
208
- const connectedCampaigns = newChannel.connectedCampaigns || [];
209
- if (
210
- !connectedCampaigns.some((c) => c.campaignId === campaign.AGRS_CID)
211
- ) {
212
- connectedCampaigns.push({
213
- campaignId: campaign.AGRS_CID,
214
- assignedAt: new Date(),
215
- releasedAt: null,
216
- });
217
- await newChannel.update({ status: "used", connectedCampaigns });
255
+ // Update new channel if exists
256
+ const newChannel = await sequelize.models.Channel.findOne({
257
+ where: {
258
+ channelId: newChannelId,
259
+ styleId: newStyleId,
260
+ },
261
+ });
262
+
263
+ if (newChannel) {
264
+ const connectedCampaigns = newChannel.connectedCampaigns || [];
265
+ if (
266
+ !connectedCampaigns.some((c) => c.campaignId === campaign.AGRS_CID)
267
+ ) {
268
+ connectedCampaigns.push({
269
+ campaignId: campaign.AGRS_CID,
270
+ assignedAt: new Date(),
271
+ releasedAt: null,
272
+ });
273
+ await newChannel.update(
274
+ {
275
+ status: "used",
276
+ connectedCampaigns,
277
+ },
278
+ { silent: true }
279
+ );
280
+ }
218
281
  }
282
+ } catch (error) {
283
+ console.error(
284
+ "Error updating channels in beforeUpdate hook:",
285
+ error.message
286
+ );
287
+ // Continue with campaign update even if channel updates fail
219
288
  }
220
289
  }
221
290
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agrs-sequelize-sdk",
3
- "version": "1.1.54",
3
+ "version": "1.1.56",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "start": "node index.js",