agrs-sequelize-sdk 1.0.85 → 1.0.86

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.
package/models/Ad.js CHANGED
@@ -167,6 +167,28 @@ module.exports = (sequelize, DataTypes) => {
167
167
 
168
168
  return null;
169
169
  };
170
+
171
+ Ad.addHook("beforeCreate", async (ad, options) => {
172
+ if (ad.changed("publish")) {
173
+ if (ad.previous("publish") === true && ad.publish === false) {
174
+ // Save current state to AdHistory
175
+ await sequelize.models.AdHistory.create({
176
+ AdID: ad.AdID,
177
+ DataSnapshot: ad._previousDataValues, // Save the full Ad data as JSON
178
+ });
179
+ } else if (ad.previous("publish") === false && ad.publish === true) {
180
+ // Delete the latest history entry
181
+ const latestHistory = await sequelize.models.AdHistory.findOne({
182
+ where: { AdID: ad.AdID },
183
+ order: [["timestamp", "DESC"]], // Get the latest history entry
184
+ });
185
+
186
+ if (latestHistory) {
187
+ await latestHistory.destroy();
188
+ }
189
+ }
190
+ }
191
+ });
170
192
  Ad.addHook("beforeUpdate", async (ad, options) => {
171
193
  if (ad.changed("publish")) {
172
194
  if (ad.previous("publish") === true && ad.publish === false) {
package/models/AdSet.js CHANGED
@@ -124,6 +124,30 @@ module.exports = (sequelize, DataTypes) => {
124
124
  }
125
125
  }
126
126
  });
127
+
128
+ AdSet.addHook("beforeCreate", async (adSet, options) => {
129
+ if (adSet.changed("publish")) {
130
+ if (adSet.previous("publish") === true && adSet.publish === false) {
131
+ // Save current state to AdSetHistory
132
+ await sequelize.models.AdSetHistory.create({
133
+ AdSetID: adSet.AdSetID,
134
+ DataSnapshot: adSet._previousDataValues, // Save the full AdSet data as JSON
135
+ });
136
+ } else if (
137
+ adSet.previous("publish") === false &&
138
+ adSet.publish === true
139
+ ) {
140
+ // Delete the latest history entry
141
+ const latestHistory = await sequelize.models.AdSetHistory.findOne({
142
+ where: { AdSetID: adSet.AdSetID },
143
+ order: [["timestamp", "DESC"]], // Get the latest history entry
144
+ });
127
145
 
146
+ if (latestHistory) {
147
+ await latestHistory.destroy();
148
+ }
149
+ }
150
+ }
151
+ });
128
152
  return AdSet;
129
153
  };
@@ -117,5 +117,30 @@ module.exports = (sequelize, DataTypes) => {
117
117
  }
118
118
  });
119
119
 
120
+ Campaign.addHook("beforeCreate", async (campaign, options) => {
121
+ if (campaign.changed("publish")) {
122
+ if (campaign.previous("publish") === true && campaign.publish === false) {
123
+ // Save current state to CampaignHistory
124
+ await sequelize.models.CampaignHistory.create({
125
+ CampaignID: campaign.CampaignID,
126
+ DataSnapshot: campaign._previousDataValues, // Store current state as JSON
127
+ });
128
+ } else if (
129
+ campaign.previous("publish") === false &&
130
+ campaign.publish === true
131
+ ) {
132
+ // Delete the latest history entry
133
+ const latestHistory = await sequelize.models.CampaignHistory.findOne({
134
+ where: { CampaignID: campaign.CampaignID },
135
+ order: [["timestamp", "DESC"]], // Get the latest history entry
136
+ });
137
+
138
+ if (latestHistory) {
139
+ await latestHistory.destroy(); // Delete the latest history entry
140
+ }
141
+ }
142
+ }
143
+ });
144
+
120
145
  return Campaign;
121
146
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agrs-sequelize-sdk",
3
- "version": "1.0.85",
3
+ "version": "1.0.86",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "start": "node index.js",