agrs-sequelize-sdk 1.1.50 → 1.1.52
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/Channel.js +10 -55
- package/package.json +1 -1
package/models/Channel.js
CHANGED
|
@@ -1,53 +1,40 @@
|
|
|
1
|
-
// models/Channel.js
|
|
1
|
+
// models/Channel.js
|
|
2
2
|
module.exports = (sequelize, DataTypes) => {
|
|
3
3
|
const Channel = sequelize.define(
|
|
4
4
|
"Channel",
|
|
5
5
|
{
|
|
6
|
-
id: {
|
|
7
|
-
type: DataTypes.INTEGER,
|
|
8
|
-
primaryKey: true,
|
|
9
|
-
autoIncrement: true,
|
|
10
|
-
},
|
|
11
6
|
channelId: {
|
|
12
|
-
type: DataTypes.STRING
|
|
7
|
+
type: DataTypes.STRING,
|
|
13
8
|
allowNull: false,
|
|
14
|
-
unique
|
|
9
|
+
// No unique constraint here
|
|
15
10
|
},
|
|
16
11
|
styleId: {
|
|
17
12
|
type: DataTypes.STRING,
|
|
18
|
-
allowNull: false,
|
|
19
|
-
unique: "channel_style_unique",
|
|
13
|
+
allowNull: false,
|
|
20
14
|
},
|
|
21
15
|
status: {
|
|
22
16
|
type: DataTypes.STRING,
|
|
23
17
|
allowNull: false,
|
|
24
18
|
defaultValue: "free",
|
|
25
19
|
validate: {
|
|
26
|
-
isIn: [["free", "used"]],
|
|
20
|
+
isIn: [["free", "used", "archived"]],
|
|
27
21
|
},
|
|
28
22
|
},
|
|
29
23
|
connectedCampaigns: {
|
|
30
|
-
type: DataTypes.
|
|
31
|
-
allowNull:
|
|
32
|
-
defaultValue: [],
|
|
33
|
-
},
|
|
34
|
-
createdAt: {
|
|
35
|
-
type: DataTypes.DATE,
|
|
36
|
-
defaultValue: DataTypes.NOW,
|
|
37
|
-
},
|
|
38
|
-
updatedAt: {
|
|
39
|
-
type: DataTypes.DATE,
|
|
40
|
-
defaultValue: DataTypes.NOW,
|
|
24
|
+
type: DataTypes.JSONB,
|
|
25
|
+
allowNull: false,
|
|
26
|
+
defaultValue: [],
|
|
41
27
|
},
|
|
42
28
|
},
|
|
43
29
|
{
|
|
44
30
|
tableName: "channels",
|
|
45
31
|
timestamps: true,
|
|
46
32
|
indexes: [
|
|
33
|
+
// Add unique composite index
|
|
47
34
|
{
|
|
48
35
|
unique: true,
|
|
49
36
|
fields: ["channelId", "styleId"],
|
|
50
|
-
name: "
|
|
37
|
+
name: "channels_channelId_styleId_unique",
|
|
51
38
|
},
|
|
52
39
|
],
|
|
53
40
|
}
|
|
@@ -58,40 +45,8 @@ module.exports = (sequelize, DataTypes) => {
|
|
|
58
45
|
foreignKey: "channelId",
|
|
59
46
|
sourceKey: "channelId",
|
|
60
47
|
as: "RSOCFeedCampaigns",
|
|
61
|
-
constraints: false,
|
|
62
48
|
});
|
|
63
49
|
};
|
|
64
50
|
|
|
65
|
-
// Hook to update status and history when a channel is created or updated
|
|
66
|
-
Channel.afterCreate(async (channel, options) => {
|
|
67
|
-
const campaigns = await sequelize.models.RSOCFeedCampaign.findAll({
|
|
68
|
-
where: { channelId: channel.channelId },
|
|
69
|
-
});
|
|
70
|
-
const campaignIds = campaigns.map((campaign) => ({
|
|
71
|
-
campaignId: campaign.AGRS_CID,
|
|
72
|
-
assignedAt: new Date(),
|
|
73
|
-
releasedAt: null,
|
|
74
|
-
}));
|
|
75
|
-
if (campaignIds.length > 0) {
|
|
76
|
-
await channel.update({ status: "used", connectedCampaigns: campaignIds });
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
Channel.afterUpdate(async (channel, options) => {
|
|
81
|
-
const campaigns = await sequelize.models.RSOCFeedCampaign.findAll({
|
|
82
|
-
where: { channelId: channel.channelId },
|
|
83
|
-
});
|
|
84
|
-
const campaignIds = campaigns.map((campaign) => ({
|
|
85
|
-
campaignId: campaign.AGRS_CID,
|
|
86
|
-
assignedAt: new Date(),
|
|
87
|
-
releasedAt: null,
|
|
88
|
-
}));
|
|
89
|
-
if (campaignIds.length > 0) {
|
|
90
|
-
await channel.update({ status: "used", connectedCampaigns: campaignIds });
|
|
91
|
-
} else {
|
|
92
|
-
await channel.update({ status: "free", connectedCampaigns: [] });
|
|
93
|
-
}
|
|
94
|
-
});
|
|
95
|
-
|
|
96
51
|
return Channel;
|
|
97
52
|
};
|