agrs-sequelize-sdk 1.1.98 → 1.1.99

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 (56) hide show
  1. package/agrs-sequelize/LICENSE +21 -0
  2. package/agrs-sequelize/README.md +179 -0
  3. package/agrs-sequelize/index.js +169 -0
  4. package/agrs-sequelize/jq.exe +0 -0
  5. package/agrs-sequelize/models/ActivityHistory.js +73 -0
  6. package/agrs-sequelize/models/Ad.js +209 -0
  7. package/agrs-sequelize/models/AdAccount.js +91 -0
  8. package/agrs-sequelize/models/AdAccountValues.js +26 -0
  9. package/agrs-sequelize/models/AdHistory.js +30 -0
  10. package/agrs-sequelize/models/AdPerformance.js +84 -0
  11. package/agrs-sequelize/models/AdPerformanceHourly.js +66 -0
  12. package/agrs-sequelize/models/AdSet.js +140 -0
  13. package/agrs-sequelize/models/AdSetHistory.js +30 -0
  14. package/agrs-sequelize/models/AdsetPerformance.js +116 -0
  15. package/agrs-sequelize/models/Article.js +156 -0
  16. package/agrs-sequelize/models/Buyers.js +26 -0
  17. package/agrs-sequelize/models/Campaign.js +136 -0
  18. package/agrs-sequelize/models/CampaignCreationLog.js +86 -0
  19. package/agrs-sequelize/models/CampaignHistory.js +33 -0
  20. package/agrs-sequelize/models/Channel.js +55 -0
  21. package/agrs-sequelize/models/CodefuelCampaign.js +159 -0
  22. package/agrs-sequelize/models/CodefuelCampaignKWHistory.js +41 -0
  23. package/agrs-sequelize/models/CodefuelKeywords.js +35 -0
  24. package/agrs-sequelize/models/CurrencyRate.js +27 -0
  25. package/agrs-sequelize/models/Domain.js +26 -0
  26. package/agrs-sequelize/models/ExplorAdsChannel.js +62 -0
  27. package/agrs-sequelize/models/Feed.js +34 -0
  28. package/agrs-sequelize/models/Files.js +73 -0
  29. package/agrs-sequelize/models/Folders.js +133 -0
  30. package/agrs-sequelize/models/KeywordPerformance.js +99 -0
  31. package/agrs-sequelize/models/KeywordRotationState.js +51 -0
  32. package/agrs-sequelize/models/Pages.js +74 -0
  33. package/agrs-sequelize/models/PipelineExecution.js +46 -0
  34. package/agrs-sequelize/models/RSOCFeedCampaign.js +302 -0
  35. package/agrs-sequelize/models/RsocKeywordPerformance.js +111 -0
  36. package/agrs-sequelize/models/Rule.js +39 -0
  37. package/agrs-sequelize/models/RulesValues.js +56 -0
  38. package/agrs-sequelize/models/SupportedLocale.js +24 -0
  39. package/agrs-sequelize/models/TonicCampaign.js +97 -0
  40. package/agrs-sequelize/models/Users.js +111 -0
  41. package/agrs-sequelize/models/Vertical.js +26 -0
  42. package/agrs-sequelize/models/newFiles.js +68 -0
  43. package/agrs-sequelize/models/pixel.js +33 -0
  44. package/agrs-sequelize/models/pixels.js +33 -0
  45. package/agrs-sequelize/package-lock.json +405 -0
  46. package/agrs-sequelize/package.json +19 -0
  47. package/agrs-sequelize/run.ps1 +98 -0
  48. package/agrs-sequelize/run.sh +214 -0
  49. package/migrations/20240321000000-add-performance-indexes.js +73 -0
  50. package/models/Ad.js +73 -9
  51. package/models/AdPerformance.js +84 -84
  52. package/models/AdSet.js +144 -0
  53. package/models/Campaign.js +135 -136
  54. package/models/RSOCFeedCampaign.js +1 -10
  55. package/models/pixel.js +5 -1
  56. package/package.json +1 -1
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Agressive Scale LTD
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,179 @@
1
+ # agrs-sequelize SDK
2
+
3
+ The `agrs-sequelize` SDK provides a structured way to manage your database models using Sequelize ORM. It allows you to easily add, update, and manage models, with support for publishing updates to NPM and pushing changes to GitHub.
4
+
5
+ ## Table of Contents
6
+
7
+ - [Prerequisites](#prerequisites)
8
+ - [Installation](#installation)
9
+ - [Usage](#usage)
10
+ - [Adding or Modifying Models](#adding-or-modifying-models)
11
+ - [Model Loading](#model-loading)
12
+ - [Publishing Changes](#publishing-changes)
13
+ - [Using PowerShell (Windows)](#using-powershell-windows)
14
+ - [Using Bash (Linux/macOS)](#using-bash-linuxmacos)
15
+ - [Script Details](#script-details)
16
+ - [run.ps1 (PowerShell Script for Windows)](#runps1-powershell-script-for-windows)
17
+ - [run.sh (Bash Script for Linux/macOS)](#runsh-bash-script-for-linuxmacos)
18
+ - [Example Model File](#example-model-file)
19
+ - [Versioning](#versioning)
20
+ - [Troubleshooting](#troubleshooting)
21
+ - [License](#license)
22
+
23
+ ## Prerequisites
24
+
25
+ Ensure the following dependencies are installed on your system:
26
+
27
+ - **Node.js**: [Download Node.js](https://nodejs.org/)
28
+ - **Sequelize**: Used for database interaction.
29
+ - **Git**: Required for version control and publishing changes to GitHub.
30
+ - **npm**: For managing and publishing packages.
31
+ - **PowerShell** (Windows) or **Bash** (Linux/macOS): For running automation scripts.
32
+
33
+ ## Installation
34
+
35
+ 1. **Clone the Repository:**
36
+
37
+ ```bash
38
+ git clone https://github.com/your-repo/agrs-sequelize.git
39
+ cd agrs-sequelize
40
+ ```
41
+
42
+ 2. **Install Dependencies:**
43
+
44
+ ```bash
45
+ npm install
46
+ ```
47
+
48
+
49
+ # Usage
50
+
51
+ ## Adding or Modifying Models
52
+ To add or modify models in the agrs-sequelize SDK:
53
+
54
+ 1. **Create or Edit a Model:**
55
+
56
+ - Add a new model file inside the models directory or edit an existing one.
57
+ - Each model file should export a function that initializes the model with Sequelize.
58
+
59
+ ```javascript
60
+ // models/YourModel.js
61
+ module.exports = (sequelize, DataTypes) => {
62
+ const YourModel = sequelize.define('YourModel', {
63
+ fieldName: {
64
+ type: DataTypes.STRING,
65
+ allowNull: false,
66
+ },
67
+ });
68
+
69
+ // Define associations (optional)
70
+ YourModel.associate = (models) => {
71
+ YourModel.hasMany(models.OtherModel);
72
+ };
73
+
74
+ return YourModel;
75
+ };
76
+ ```
77
+
78
+ 2. **Define Associations (if required):**
79
+
80
+ Use the associate method to define relationships with other models.
81
+ Associations like hasMany, belongsTo, etc., are supported.
82
+ ### Model Loading
83
+ Models are automatically loaded when initializing Sequelize.
84
+ The index.js file ensures all models in the models directory are properly loaded and associated.
85
+
86
+ ## Publishing Changes
87
+ After modifying or adding models, publish the changes to NPM and GitHub.
88
+
89
+ ### Using PowerShell (Windows)
90
+ Open PowerShell as Administrator.
91
+
92
+ Run the Script:
93
+
94
+ ```powershell
95
+ ./run.ps1
96
+ ```
97
+ The script will:
98
+
99
+ - Check for jq installation.
100
+ - Bump the version in package.json.
101
+ - Publish to NPM.
102
+ - Commit and push changes to GitHub.
103
+
104
+ ### Using Bash (Linux/macOS)
105
+ Open a Bash Terminal.
106
+
107
+ Run the Script:
108
+
109
+ ```bash
110
+ ./run.sh
111
+ ```
112
+ The script performs the same tasks as the PowerShell script, tailored for Bash environments.
113
+
114
+ ## Script Details
115
+ ### run.ps1 (PowerShell Script for Windows)
116
+ Automates the publishing process:
117
+
118
+ - Ensures jq is installed.
119
+ - Increments the version in package.json.
120
+ - Publishes the package to NPM.
121
+ - Commits and pushes changes to GitHub.
122
+
123
+ ### run.sh (Bash Script for Linux/macOS)
124
+ Provides similar functionality:
125
+
126
+ - Checks for jq installation.
127
+ - Bumps the package version.
128
+ = Publishes to NPM.
129
+ = Commits and pushes to GitHub.
130
+
131
+ ## Example Model File
132
+ ```javascript
133
+ Copy code
134
+ // models/YourModel.js
135
+ module.exports = (sequelize, DataTypes) => {
136
+ const YourModel = sequelize.define('YourModel', {
137
+ name: {
138
+ type: DataTypes.STRING,
139
+ allowNull: false,
140
+ },
141
+ });
142
+
143
+ // Define associations (optional)
144
+ YourModel.associate = (models) => {
145
+ YourModel.hasMany(models.OtherModel);
146
+ };
147
+
148
+ return YourModel;
149
+ };
150
+ ```
151
+ In this example:
152
+
153
+ YourModel has a name field.
154
+ It has a hasMany association with OtherModel.
155
+ ## Versioning
156
+ Versioning is handled automatically:
157
+
158
+ - The scripts increment the version in package.json.
159
+ - Follows semantic versioning (major, minor, patch).
160
+ - Updated versions are published to NPM and committed to the repository.
161
+
162
+ ## Troubleshooting
163
+ - ### Permission Issues:
164
+
165
+ - Linux/macOS: Ensure scripts have execute permissions (chmod +x run.sh).
166
+ - ### NPM Login Errors:
167
+
168
+ - The script will prompt for NPM login if not already logged in.
169
+ - Verify NPM account credentials.
170
+
171
+ - ### Missing Dependencies:
172
+
173
+ - The script checks for jq and installs it if missing.
174
+ ## License
175
+ This project is licensed under the MIT License. See the LICENSE file for details.
176
+
177
+
178
+
179
+
@@ -0,0 +1,169 @@
1
+ const Sequelize = require("sequelize");
2
+ const path = require("path");
3
+ const fs = require("fs");
4
+
5
+ // const env = process.env.NODE_ENV || "development";
6
+ class DBModels {
7
+ constructor(config) {
8
+ this.config = config;
9
+ this.db = {};
10
+
11
+ // Initialize Sequelize
12
+ const sequelize = new Sequelize(
13
+ this.config.database,
14
+ this.config.username,
15
+ this.config.password,
16
+ {
17
+ host: this.config.host,
18
+ dialect: this.config.dialect,
19
+ port: this.config.port,
20
+ logging: false, // Enable logging to console
21
+ dialectOptions: {
22
+ ssl: {
23
+ require: true, // Require SSL
24
+ rejectUnauthorized: false, // For self-signed or unverified certificates
25
+ },
26
+ },
27
+ pool: {
28
+ max: 200, // Increase from 50, but stay under max_connections (200)
29
+ min: 5, // Increase minimum connections
30
+ acquire: 60000, // Double acquire timeout
31
+ idle: 30000, // Align with idle_in_transaction_session_timeout
32
+ },
33
+
34
+ retry: {
35
+ max: 5,
36
+ timeout: 60000,
37
+ match: [
38
+ /Connection terminated/,
39
+ /Connection timed out/,
40
+ /Operation timeout/,
41
+ /password authentication failed/,
42
+ /canceling authentication/,
43
+ ],
44
+ },
45
+ }
46
+ );
47
+
48
+ // Load all models
49
+ fs.readdirSync(path.join(__dirname, "models"))
50
+ .filter((file) => {
51
+ return file.indexOf(".") !== 0 && file.slice(-3) === ".js";
52
+ })
53
+ .forEach((file) => {
54
+ const model = require(path.join(__dirname, "models", file))(
55
+ sequelize,
56
+ Sequelize.DataTypes
57
+ );
58
+ this.db[model.name] = model;
59
+ });
60
+
61
+ // Set up associations
62
+ Object.keys(this.db).forEach((modelName) => {
63
+ if (this.db[modelName].associate) {
64
+ this.db[modelName].associate(this.db);
65
+ }
66
+ });
67
+
68
+ // Export the db object with Sequelize and models
69
+ this.db.sequelize = sequelize;
70
+ this.db.Sequelize = Sequelize;
71
+ }
72
+ }
73
+
74
+ module.exports = DBModels;
75
+
76
+ // const Sequelize = require("sequelize");
77
+ // const path = require("path");
78
+ // const fs = require("fs");
79
+
80
+ // class DBModels {
81
+ // constructor(config) {
82
+ // this.config = config;
83
+ // this.db = {};
84
+
85
+ // const sequelize = new Sequelize(
86
+ // this.config.database,
87
+ // this.config.username,
88
+ // this.config.password,
89
+ // {
90
+ // host: this.config.host,
91
+ // dialect: this.config.dialect,
92
+ // port: this.config.port,
93
+ // logging: false,
94
+ // dialectOptions: {
95
+ // ssl: {
96
+ // require: true,
97
+ // rejectUnauthorized: false,
98
+ // },
99
+ // statement_timeout: 30000,
100
+ // idle_in_transaction_session_timeout: 30000,
101
+ // connectTimeout: 30000,
102
+ // },
103
+ // pool: {
104
+ // max: 50, // Quarter of max connections (200)
105
+ // min: 0, // Start from 0 and scale up as needed
106
+ // acquire: 30000, // Maximum time to wait for a connection
107
+ // idle: 10000, // How long a connection can remain idle
108
+ // },
109
+ // retry: {
110
+ // max: 3,
111
+ // timeout: 20000,
112
+ // match: [
113
+ // /Connection terminated/,
114
+ // /Connection timed out/,
115
+ // /Operation timeout/,
116
+ // /ECONNRESET/,
117
+ // /PROTOCOL_CONNECTION_LOST/,
118
+ // ],
119
+ // },
120
+ // }
121
+ // );
122
+
123
+ // // Add connection monitoring listeners
124
+ // sequelize.connectionManager.on("acquire", function (connection) {
125
+ // console.log("Connection %d acquired", connection.threadId);
126
+ // });
127
+
128
+ // sequelize.connectionManager.on("release", function (connection) {
129
+ // console.log("Connection %d released", connection.threadId);
130
+ // });
131
+
132
+ // // Load all models
133
+ // fs.readdirSync(path.join(__dirname, "models"))
134
+ // .filter((file) => {
135
+ // return file.indexOf(".") !== 0 && file.slice(-3) === ".js";
136
+ // })
137
+ // .forEach((file) => {
138
+ // const model = require(path.join(__dirname, "models", file))(
139
+ // sequelize,
140
+ // Sequelize.DataTypes
141
+ // );
142
+ // this.db[model.name] = model;
143
+ // });
144
+
145
+ // // Set up associations
146
+ // Object.keys(this.db).forEach((modelName) => {
147
+ // if (this.db[modelName].associate) {
148
+ // this.db[modelName].associate(this.db);
149
+ // }
150
+ // });
151
+
152
+ // // Export the db object with Sequelize and models
153
+ // this.db.sequelize = sequelize;
154
+ // this.db.Sequelize = Sequelize;
155
+ // }
156
+
157
+ // // Add method for pool monitoring
158
+ // async getConnectionPoolStats() {
159
+ // const pool = this.db.sequelize.connectionManager.pool;
160
+ // return {
161
+ // all: pool.size,
162
+ // available: pool.available,
163
+ // borrowed: pool.borrowed,
164
+ // pending: pool.pending,
165
+ // };
166
+ // }
167
+ // }
168
+
169
+ // module.exports = DBModels;
Binary file
@@ -0,0 +1,73 @@
1
+ // models/ActivityHistory.js
2
+ module.exports = (sequelize, DataTypes) => {
3
+ const ActivityHistory = sequelize.define(
4
+ "ActivityHistory",
5
+ {
6
+ ActivityHistoryID: {
7
+ type: DataTypes.INTEGER,
8
+ primaryKey: true,
9
+ autoIncrement: true,
10
+ },
11
+ AssociatedModelType: {
12
+ type: DataTypes.STRING,
13
+ allowNull: false, // Could be 'Ad', 'AdSet', 'Campaign', etc.
14
+ },
15
+ AssociatedModelID: {
16
+ type: DataTypes.STRING,
17
+ allowNull: false, // ID of the associated model
18
+ },
19
+ ActorID: {
20
+ type: DataTypes.STRING,
21
+ allowNull: true,
22
+ },
23
+ ActorName: {
24
+ type: DataTypes.STRING,
25
+ allowNull: true,
26
+ },
27
+ ApplicationName: {
28
+ type: DataTypes.STRING,
29
+ allowNull: true,
30
+ },
31
+ DateTimeInTimeZone: {
32
+ type: DataTypes.DATE,
33
+ allowNull: true,
34
+ },
35
+ EventType: {
36
+ type: DataTypes.STRING,
37
+ allowNull: true,
38
+ },
39
+ ObjectName: {
40
+ type: DataTypes.STRING,
41
+ allowNull: true,
42
+ },
43
+ ObjectType: {
44
+ type: DataTypes.STRING,
45
+ allowNull: true,
46
+ },
47
+ TranslatedEventType: {
48
+ type: DataTypes.STRING,
49
+ allowNull: true,
50
+ },
51
+ ObjectID: {
52
+ type: DataTypes.STRING,
53
+ allowNull: true,
54
+ },
55
+ ExtraData: {
56
+ type: DataTypes.JSONB,
57
+ allowNull: true,
58
+ },
59
+ },
60
+ {
61
+ tableName: "ActivityHistory",
62
+ indexes: [
63
+ {
64
+ unique: true,
65
+ name: "unique_index_on_date_object_event", // Custom name
66
+ fields: ["DateTimeInTimeZone", "ObjectID", "EventType"],
67
+ },
68
+ ],
69
+ }
70
+ );
71
+
72
+ return ActivityHistory;
73
+ };
@@ -0,0 +1,209 @@
1
+ module.exports = (sequelize, DataTypes) => {
2
+ const Ad = sequelize.define(
3
+ "Ad",
4
+ {
5
+ AdID: {
6
+ type: DataTypes.STRING,
7
+ primaryKey: true,
8
+ },
9
+ AdName: {
10
+ type: DataTypes.STRING,
11
+ allowNull: false,
12
+ },
13
+ AdSetID: {
14
+ type: DataTypes.STRING,
15
+ allowNull: false,
16
+ references: {
17
+ model: "AdSet",
18
+ key: "AdSetID",
19
+ },
20
+ },
21
+ creativeId: {
22
+ type: DataTypes.STRING,
23
+ allowNull: true,
24
+ },
25
+ pixelId: {
26
+ type: DataTypes.STRING,
27
+ allowNull: true,
28
+ },
29
+ startTime: {
30
+ type: DataTypes.DATE,
31
+ allowNull: true,
32
+ },
33
+ endTime: {
34
+ type: DataTypes.DATE,
35
+ allowNull: true,
36
+ },
37
+ format: {
38
+ type: DataTypes.STRING,
39
+ allowNull: false,
40
+ defaultValue: "SINGLE",
41
+ },
42
+ imageHashes: {
43
+ type: DataTypes.ARRAY(DataTypes.STRING),
44
+ allowNull: true,
45
+ },
46
+ videoIds: {
47
+ type: DataTypes.ARRAY(DataTypes.STRING),
48
+ allowNull: true,
49
+ },
50
+ messages: {
51
+ type: DataTypes.ARRAY(DataTypes.TEXT),
52
+ allowNull: true,
53
+ },
54
+ headlines: {
55
+ type: DataTypes.ARRAY(DataTypes.TEXT),
56
+ allowNull: true,
57
+ },
58
+ websiteUrl: {
59
+ type: DataTypes.TEXT,
60
+ allowNull: true,
61
+ },
62
+ pageId: {
63
+ type: DataTypes.STRING(256),
64
+ allowNull: true,
65
+ },
66
+ actionTypes: {
67
+ type: DataTypes.STRING,
68
+ allowNull: true,
69
+ defaultValue: "offsite_conversion",
70
+ },
71
+ Status: {
72
+ type: DataTypes.STRING,
73
+ allowNull: true,
74
+ },
75
+ publish: {
76
+ type: DataTypes.BOOLEAN,
77
+ allowNull: true,
78
+ defaultValue: true,
79
+ },
80
+ createdTime: {
81
+ type: DataTypes.DATE,
82
+ allowNull: true,
83
+ },
84
+ AGRS_CID: {
85
+ type: DataTypes.STRING,
86
+ allowNull: true,
87
+ },
88
+ Draft: {
89
+ type: DataTypes.BOOLEAN,
90
+ allowNull: true,
91
+ defaultValue: false,
92
+ },
93
+ UserCreated: {
94
+ type: DataTypes.STRING,
95
+ allowNull: true,
96
+ },
97
+ effectiveStatus: {
98
+ type: DataTypes.STRING,
99
+ allowNull: true,
100
+ },
101
+ issuesInfo: {
102
+ type: DataTypes.JSONB,
103
+ allowNull: true,
104
+ },
105
+ adReviewFeedback: {
106
+ type: DataTypes.JSONB,
107
+ allowNull: true,
108
+ },
109
+ waiting: {
110
+ type: DataTypes.BOOLEAN,
111
+ defaultValue: false,
112
+ },
113
+ },
114
+ {
115
+ tableName: "Ad",
116
+ indexes: [
117
+ {
118
+ fields: ["AdSetID"],
119
+ },
120
+ ],
121
+ }
122
+ );
123
+
124
+ Ad.associate = (models) => {
125
+ Ad.belongsTo(models.AdSet, { foreignKey: "AdSetID" });
126
+
127
+ Ad.belongsTo(models.CodefuelCampaign, {
128
+ foreignKey: "AGRS_CID",
129
+ targetKey: "AGRSCID",
130
+ as: "CodefuelCampaign",
131
+ constraints: false,
132
+ });
133
+
134
+ Ad.belongsTo(models.RSOCFeedCampaign, {
135
+ foreignKey: "AGRS_CID",
136
+ targetKey: "AGRS_CID",
137
+ as: "RSOCFeedCampaign",
138
+ constraints: false,
139
+ });
140
+
141
+ Ad.hasMany(models.AdPerformance, {
142
+ foreignKey: "AdID",
143
+ });
144
+ };
145
+
146
+ Ad.prototype.getRelatedCampaign = async function () {
147
+ if (!this.AGRS_CID) return null;
148
+
149
+ // Try fetching related CodefuelCampaign
150
+ const codefuelCampaign = await sequelize.models.CodefuelCampaign.findOne({
151
+ where: { AGRSCID: this.AGRS_CID },
152
+ });
153
+
154
+ if (codefuelCampaign) {
155
+ return {
156
+ type: "CodefuelCampaign",
157
+ data: codefuelCampaign,
158
+ };
159
+ }
160
+
161
+ // Try fetching related RSOCFeedCampaign
162
+ const rsocFeedCampaign = await sequelize.models.RSOCFeedCampaign.findOne({
163
+ where: { AGRS_CID: this.AGRS_CID },
164
+ });
165
+
166
+ if (rsocFeedCampaign) {
167
+ return {
168
+ type: "RSOCFeedCampaign",
169
+ data: rsocFeedCampaign,
170
+ };
171
+ }
172
+
173
+ return null;
174
+ };
175
+
176
+ Ad.addHook("afterCreate", async (ad, options) => {
177
+ if (ad.publish === false) {
178
+ // If publish is false on creation, save a snapshot to AdHistory
179
+ await sequelize.models.AdHistory.create({
180
+ AdID: ad.AdID,
181
+ DataSnapshot: ad.toJSON(), // Save the full Ad data as JSON
182
+ });
183
+ }
184
+ });
185
+
186
+ Ad.addHook("beforeUpdate", async (ad, options) => {
187
+ if (ad.changed("publish")) {
188
+ if (ad.previous("publish") === true && ad.publish === false) {
189
+ // Save current state to AdHistory
190
+ await sequelize.models.AdHistory.create({
191
+ AdID: ad.AdID,
192
+ DataSnapshot: ad._previousDataValues, // Save the full Ad data as JSON
193
+ });
194
+ } else if (ad.previous("publish") === false && ad.publish === true) {
195
+ // Delete the latest history entry
196
+ const latestHistory = await sequelize.models.AdHistory.findOne({
197
+ where: { AdID: ad.AdID },
198
+ order: [["timestamp", "DESC"]], // Get the latest history entry
199
+ });
200
+
201
+ if (latestHistory) {
202
+ await latestHistory.destroy();
203
+ }
204
+ }
205
+ }
206
+ });
207
+
208
+ return Ad;
209
+ };