agrs-sequelize-sdk 1.1.19 → 1.1.20
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/KeywordPerformance.js +15 -36
- package/package.json +1 -1
|
@@ -64,44 +64,23 @@ module.exports = (sequelize, DataTypes) => {
|
|
|
64
64
|
hooks: {
|
|
65
65
|
beforeCreate: async (record, options) => {
|
|
66
66
|
try {
|
|
67
|
-
// Find
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
67
|
+
// Find existing record for this keyword
|
|
68
|
+
const existingKeyword =
|
|
69
|
+
await sequelize.models.KeywordPerformance.findOne({
|
|
70
|
+
where: {
|
|
71
|
+
Keyword: sequelize.where(
|
|
72
|
+
sequelize.fn("LOWER", sequelize.col("Keyword")),
|
|
73
|
+
sequelize.fn("LOWER", record.Keyword)
|
|
74
|
+
),
|
|
75
|
+
},
|
|
76
|
+
attributes: ["FirstAppearanceDate"],
|
|
77
77
|
transaction: options.transaction,
|
|
78
|
-
}
|
|
79
|
-
);
|
|
78
|
+
});
|
|
80
79
|
|
|
81
|
-
// If
|
|
82
|
-
//
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
? Math.min(new Date(existingFirstDate), new Date(record.Date))
|
|
86
|
-
: record.Date;
|
|
87
|
-
|
|
88
|
-
// If we found an earlier date, update all existing records for this keyword
|
|
89
|
-
if (existingFirstDate && record.Date < existingFirstDate) {
|
|
90
|
-
await sequelize.query(
|
|
91
|
-
`
|
|
92
|
-
UPDATE "KeywordPerformance"
|
|
93
|
-
SET "FirstAppearanceDate" = :newDate
|
|
94
|
-
WHERE LOWER("Keyword") = LOWER(:keyword)
|
|
95
|
-
`,
|
|
96
|
-
{
|
|
97
|
-
replacements: {
|
|
98
|
-
newDate: record.Date,
|
|
99
|
-
keyword: record.Keyword,
|
|
100
|
-
},
|
|
101
|
-
transaction: options.transaction,
|
|
102
|
-
}
|
|
103
|
-
);
|
|
104
|
-
}
|
|
80
|
+
// If keyword exists, use its FirstAppearanceDate
|
|
81
|
+
// If not, use the current record's Date
|
|
82
|
+
record.FirstAppearanceDate =
|
|
83
|
+
existingKeyword?.FirstAppearanceDate || record.Date;
|
|
105
84
|
} catch (error) {
|
|
106
85
|
console.error("Error in FirstAppearanceDate hook:", error);
|
|
107
86
|
throw error;
|