Skip to content

Commit 4458bbf

Browse files
committed
modify the timestamp of veiwed history
1 parent 1a77216 commit 4458bbf

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

backend/migrations/20251028184721-create-view-history.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,18 @@ module.exports = {
3333
viewed_at: {
3434
type: Sequelize.DATE,
3535
allowNull: false,
36-
defaultValue: Sequelize.literal("CURRENT_TIMESTAMP"),
3736
},
3837
});
3938

4039
// Add indexes for common queries
4140
await queryInterface.addIndex("view_history", ["user_id"]);
4241
await queryInterface.addIndex("view_history", ["dataset_id"]);
4342
await queryInterface.addIndex("view_history", ["viewed_at"]);
43+
await queryInterface.addConstraint("view_history", {
44+
fields: ["user_id", "dataset_id"],
45+
type: "unique",
46+
name: "unique_user_dataset_view",
47+
});
4448
},
4549

4650
async down(queryInterface, Sequelize) {

backend/src/controllers/activity.controller.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,24 +305,42 @@ const incrementViewCount = async (dbName, datasetId) => {
305305

306306
// function2: add or update a viewed history
307307
const trackUserHistory = async (userId, datasetId) => {
308+
console.log("=== trackUserHistory called ===");
309+
console.log("userId:", userId);
310+
console.log("datasetId:", datasetId);
308311
const existingView = await ViewHistory.findOne({
309312
where: {
310313
user_id: userId,
311314
dataset_id: datasetId,
312315
},
313316
});
314317

318+
console.log("existingView found:", !!existingView);
319+
315320
if (existingView) {
321+
console.log("OLD viewed_at:", existingView.viewed_at);
316322
existingView.viewed_at = new Date();
323+
console.log("NEW viewed_at (before save):", existingView.viewed_at);
324+
// Tell Sequelize this field changed
325+
existingView.changed("viewed_at", true);
326+
console.log("Changed fields:", existingView.changed());
317327
await existingView.save();
318-
return { isNew: false };
328+
console.log("After save, viewed_at:", existingView.viewed_at);
329+
330+
await existingView.reload();
331+
console.log("After reload, viewed_at:", existingView.viewed_at);
332+
333+
return { isNew: false, viewed_at: existingView.viewed_at };
319334
} else {
320335
// create new record
321-
await ViewHistory.create({
336+
console.log("Creating new view record");
337+
const newView = await ViewHistory.create({
322338
user_id: userId,
323339
dataset_id: datasetId,
340+
viewed_at: new Date(),
324341
});
325-
return { isNew: true };
342+
console.log("Created view with viewed_at:", newView.viewed_at);
343+
return { isNew: true, viewed_at: newView.viewed_at };
326344
}
327345
};
328346

@@ -344,6 +362,7 @@ const trackView = async (req, res) => {
344362
viewCount: dataset.views_count,
345363
personalHistoryTracked: !!historyResult,
346364
isNewView: historyResult?.isNew,
365+
viewed_at: historyResult?.viewed_at,
347366
isAuthenticated: !!user,
348367
});
349368
} catch (error) {

backend/src/models/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,17 @@ ViewHistory.init(
162162
},
163163
viewed_at: {
164164
type: DataTypes.DATE,
165-
defaultValue: DataTypes.NOW,
165+
// defaultValue: DataTypes.NOW,
166166
},
167167
},
168168
{
169169
sequelize,
170170
tableName: "view_history",
171-
timestamps: true,
171+
// timestamps: true,
172+
timestamps: false,
172173
underscored: true,
173-
createdAt: "viewed_at",
174-
updatedAt: false,
174+
// createdAt: "viewed_at",
175+
// updatedAt: false,
175176
}
176177
);
177178

0 commit comments

Comments
 (0)