agrs-sequelize-sdk 1.1.56 → 1.1.57

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.
@@ -116,10 +116,12 @@ module.exports = (sequelize, DataTypes) => {
116
116
  },
117
117
  createdAt: {
118
118
  type: DataTypes.DATE,
119
+ allowNull: false,
119
120
  defaultValue: DataTypes.NOW,
120
121
  },
121
122
  updatedAt: {
122
123
  type: DataTypes.DATE,
124
+ allowNull: false,
123
125
  defaultValue: DataTypes.NOW,
124
126
  },
125
127
  platform: {
@@ -133,97 +135,84 @@ module.exports = (sequelize, DataTypes) => {
133
135
  },
134
136
  {
135
137
  tableName: "rsoc_feed_campaigns",
136
- timestamps: true,
137
- // Entirely disable automatic foreign key creation
138
- references: false,
138
+ timestamps: true, // Enable Sequelize-managed timestamps
139
139
  }
140
140
  );
141
141
 
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 };
146
-
147
- // Associate with Article model
142
+ // Define associations without enforcing foreign key constraints
143
+ RSOCFeedCampaign.associate = (models) => {
148
144
  if (models.Article) {
149
145
  RSOCFeedCampaign.belongsTo(models.Article, {
150
146
  foreignKey: "AGRSAID",
151
147
  targetKey: "AGRSAID",
152
148
  as: "Article",
153
- ...queryOptions,
149
+ constraints: false, // Disable FK constraint
154
150
  });
155
151
  }
156
152
 
157
- // Associate with Channel model
158
153
  if (models.Channel) {
159
154
  RSOCFeedCampaign.belongsTo(models.Channel, {
160
155
  foreignKey: "channelId",
161
156
  targetKey: "channelId",
162
157
  as: "Channel",
163
- ...queryOptions,
158
+ constraints: false, // Disable FK constraint
164
159
  });
165
160
  }
166
161
  };
167
162
 
163
+ // BeforeCreate hook: Update Channel status and connectedCampaigns
168
164
  RSOCFeedCampaign.beforeCreate(async (campaign, options) => {
169
- // Set redirectLink to null as requested
170
- campaign.setDataValue("redirectLink", null);
165
+ campaign.setDataValue("redirectLink", null); // Ensure redirectLink is null
171
166
 
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
- },
180
- });
167
+ try {
168
+ const channel = await sequelize.models.Channel.findOne({
169
+ where: {
170
+ channelId: campaign.channelId,
171
+ styleId: campaign.styleId || null, // Handle null styleId
172
+ },
173
+ });
181
174
 
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
- }
175
+ if (channel) {
176
+ const connectedCampaigns = channel.connectedCampaigns || [];
177
+ if (
178
+ !connectedCampaigns.some((c) => c.campaignId === campaign.AGRS_CID)
179
+ ) {
180
+ connectedCampaigns.push({
181
+ campaignId: campaign.AGRS_CID,
182
+ assignedAt: new Date(),
183
+ releasedAt: null,
184
+ });
185
+ await channel.update(
186
+ {
187
+ status: "used",
188
+ connectedCampaigns,
189
+ },
190
+ { silent: true } // Prevent infinite loops
191
+ );
200
192
  }
201
- } catch (error) {
202
- console.error(
203
- "Error updating channel in beforeCreate hook:",
204
- error.message
193
+ } else {
194
+ console.warn(
195
+ `Channel not found for channelId: ${campaign.channelId}, styleId: ${campaign.styleId}`
205
196
  );
206
- // Continue with campaign creation even if channel update fails
207
197
  }
198
+ } catch (error) {
199
+ console.error("Error in beforeCreate hook:", error.message);
200
+ // Log error but don't throw to allow campaign creation to proceed
208
201
  }
209
202
  });
210
203
 
204
+ // BeforeUpdate hook: Handle channel reassignment
211
205
  RSOCFeedCampaign.beforeUpdate(async (campaign, options) => {
212
- // Ensure redirectLink remains null
213
- campaign.setDataValue("redirectLink", null);
206
+ campaign.setDataValue("redirectLink", null); // Ensure redirectLink remains null
214
207
 
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
- ) {
208
+ if (campaign.changed("channelId") || campaign.changed("styleId")) {
220
209
  try {
221
210
  const oldChannelId = campaign.previous("channelId");
222
- const oldStyleId = campaign.previous("styleId");
211
+ const oldStyleId = campaign.previous("styleId") || null;
223
212
  const newChannelId = campaign.channelId;
224
- const newStyleId = campaign.styleId;
213
+ const newStyleId = campaign.styleId || null;
225
214
 
226
- // Update old channel if exists
215
+ // Release old channel
227
216
  if (oldChannelId) {
228
217
  const oldChannel = await sequelize.models.Channel.findOne({
229
218
  where: {
@@ -240,7 +229,7 @@ module.exports = (sequelize, DataTypes) => {
240
229
  ? { ...c, releasedAt: new Date() }
241
230
  : c
242
231
  )
243
- .filter((c) => c.releasedAt === null);
232
+ .filter((c) => !c.releasedAt); // Remove released campaigns
244
233
 
245
234
  await oldChannel.update(
246
235
  {
@@ -252,7 +241,7 @@ module.exports = (sequelize, DataTypes) => {
252
241
  }
253
242
  }
254
243
 
255
- // Update new channel if exists
244
+ // Assign new channel
256
245
  const newChannel = await sequelize.models.Channel.findOne({
257
246
  where: {
258
247
  channelId: newChannelId,
@@ -278,13 +267,14 @@ module.exports = (sequelize, DataTypes) => {
278
267
  { silent: true }
279
268
  );
280
269
  }
270
+ } else {
271
+ console.warn(
272
+ `New channel not found for channelId: ${newChannelId}, styleId: ${newStyleId}`
273
+ );
281
274
  }
282
275
  } 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
276
+ console.error("Error in beforeUpdate hook:", error.message);
277
+ // Log error but don't throw to allow update to proceed
288
278
  }
289
279
  }
290
280
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agrs-sequelize-sdk",
3
- "version": "1.1.56",
3
+ "version": "1.1.57",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "start": "node index.js",