Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Popup log fixed #355

Merged
merged 21 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const authRoutes = require('./src/routes/auth.routes');
const userRoutes = require('./src/routes/user.routes');
const mocks = require('./src/routes/mocks.routes');
const popup = require('./src/routes/popup.routes');
const popup_log = require('./src/routes/popuplog.routes');
const guide_log = require('./src/routes/guidelog.routes');
const banner = require('./src/routes/banner.routes');
const teamRoutes = require('./src/routes/team.routes');
const hint = require('./src/routes/hint.routes');
Expand Down Expand Up @@ -47,7 +47,7 @@ app.use('/api/auth', authRoutes);
app.use('/api/users', userRoutes);
app.use('/api/mock/', mocks);
app.use('/api/popup', popup);
app.use('/api/popup_log', popup_log);
app.use('/api/guide_log', guide_log);
app.use('/api/banner', banner);
app.use('/api/team', teamRoutes);
// app.use('/api/tours', tourRoutes);
Expand Down
3 changes: 2 additions & 1 deletion backend/migrations/20240916052017-create_teams_table.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ module.exports = {
});

await queryInterface.bulkInsert("teams", [{
name: config.defaultTeamName
name: config.defaultTeamName,
createdAt: new Date()
erenfn marked this conversation as resolved.
Show resolved Hide resolved
}]);
},

Expand Down
70 changes: 70 additions & 0 deletions backend/migrations/20241203005208-change-popup-log-to-guide-log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
'use strict';
/** @type {import('sequelize-cli').Migration} */
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.renameTable('popup_logs', 'guide_logs');
await queryInterface.sequelize.query(`
ALTER TABLE "guide_logs"
ALTER COLUMN "popupType" TYPE INTEGER
USING
CASE
WHEN "popupType" = 'guide' THEN 1
WHEN "popupType" = 'tooltip' THEN 2
WHEN "popupType" = 'hotspot' THEN 3
WHEN "popupType" = 'checklist' THEN 4
ELSE NULL
END
`);
erenfn marked this conversation as resolved.
Show resolved Hide resolved

await queryInterface.changeColumn('guide_logs', 'popupType', {
type: Sequelize.INTEGER,
allowNull: false,
});

await queryInterface.addColumn('guide_logs', 'guideId', {
type: Sequelize.INTEGER,
allowNull: false,
});
erenfn marked this conversation as resolved.
Show resolved Hide resolved

await queryInterface.addIndex('guide_logs', ['userId'], {
name: 'idx_userId',
});
await queryInterface.addIndex('guide_logs', ['guideId'], {
name: 'idx_guideId',
});
await queryInterface.addIndex('guide_logs', ['popupType'], {
name: 'idx_popupType',
});
await queryInterface.addIndex('guide_logs', ['userId', 'guideId', 'popupType'], {
name: 'idx_userId_guideId_popupType',
unique: false,
});
erenfn marked this conversation as resolved.
Show resolved Hide resolved
},
erenfn marked this conversation as resolved.
Show resolved Hide resolved

down: async (queryInterface, Sequelize) => {
await queryInterface.renameTable('guide_logs', 'popup_logs');
await queryInterface.sequelize.query(`
ALTER TABLE "popup_logs"
ALTER COLUMN "popupType" TYPE ENUM('guide', 'tooltip', 'hotspot', 'checklist')
USING
CASE
WHEN "popupType" = 1 THEN 'guide'
WHEN "popupType" = 2 THEN 'tooltip'
WHEN "popupType" = 3 THEN 'hotspot'
WHEN "popupType" = 4 THEN 'checklist'
ELSE NULL
END
`);
await queryInterface.changeColumn('popup_logs', 'popupType', {
type: Sequelize.ENUM('guide', 'tooltip', 'hotspot', 'checklist'),
allowNull: false,
});

await queryInterface.removeColumn('popup_logs', 'guideId');

await queryInterface.removeIndex('popup_logs', 'idx_userId');
await queryInterface.removeIndex('popup_logs', 'idx_guideId');
await queryInterface.removeIndex('popup_logs', 'idx_popupType');
await queryInterface.removeIndex('popup_logs', 'idx_userId_guideId_popupType');
},
};
61 changes: 61 additions & 0 deletions backend/src/controllers/guidelog.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const guidelogService = require("../service/guidelog.service.js");
const { internalServerError } = require("../utils/errors.helper.js");

class GuideLogController {
async addGuideLog(req, res) {
try {
const { popupType, userId, completed, guideId } = req.body;
const isCompleted = completed !== undefined ? completed : false;

const guideLog = await guidelogService.addGuideLog({ popupType, guideId, userId, completed: isCompleted });

res.status(201).json(guideLog);
} catch (err) {
const { statusCode, payload } = internalServerError(
"Error adding Guide Logs:",
err.message,
);
res.status(statusCode).json(payload);
}
}
erenfn marked this conversation as resolved.
Show resolved Hide resolved
async getAllLogs(req, res) {
try {
const guideLogs = await guidelogService.getAllGuideLogs();
res.status(200).json(guideLogs);
} catch (err) {
const { statusCode, payload } = internalServerError(
"GET_ALL_GUIDE_LOGS_ERROR",
err.message,
);
res.status(statusCode).json(payload);
}
}
async getLogsByUser(req, res){
try {
const {userId} = req.body;
const guideLogs = await guidelogService.getLogsByUser(userId);
res.status(200).json(guideLogs);
} catch (err) {
const { statusCode, payload } = internalServerError(
"GET_ALL_LOGS_BY_USER_ERROR",
err.message,
);
res.status(statusCode).json(payload);
}
}
erenfn marked this conversation as resolved.
Show resolved Hide resolved
async getCompleteGuideLogs(req, res){
try {
const {userId} = req.body;
const guideLogs = await guidelogService.getCompleteGuideLogs(userId);
res.status(200).json(guideLogs);
} catch (err) {
const { statusCode, payload } = internalServerError(
"GET_ALL_COMPLETE_LOGS_BY_USER_ERROR",
err.message,
);
res.status(statusCode).json(payload);
}
}
erenfn marked this conversation as resolved.
Show resolved Hide resolved
}

module.exports = new GuideLogController();
34 changes: 0 additions & 34 deletions backend/src/controllers/popuplog.controller.js

This file was deleted.

58 changes: 58 additions & 0 deletions backend/src/models/GuideLog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const { GuideType } = require('../utils/guidelog.helper');

module.exports = (sequelize, DataTypes) => {
const GuideLog = sequelize.define('GuideLog', {
popupType: {
type: DataTypes.INTEGER,
allowNull: false,
validate: {
isIn: {
args: [
Object.values(GuideType),
],
msg: 'popupType must be a valid value.',
},
},
},
guideId: {
type: DataTypes.INTEGER,
allowNull: false,
},
userId: {
type: DataTypes.STRING,
allowNull: false,
},
erenfn marked this conversation as resolved.
Show resolved Hide resolved
showingTime: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,
},
completed: {
type: DataTypes.BOOLEAN,
defaultValue: false,
},
}, {
timestamps: false,
tableName: 'guide_logs',
erenfn marked this conversation as resolved.
Show resolved Hide resolved
indexes: [
{
name: 'idx_userId',
fields: ['userId'],
},
{
name: 'idx_guideId',
fields: ['guideId'],
},
{
name: 'idx_popupType',
fields: ['popupType'],
},
{
name: 'idx_userId_guideId_popupType',
fields: ['userId', 'guideId', 'popupType'],
unique: false,
},
],
});

return GuideLog;
};
24 changes: 0 additions & 24 deletions backend/src/models/PopupLog.js

This file was deleted.

2 changes: 1 addition & 1 deletion backend/src/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ db.sequelize = sequelize;
db.User = require("./User.js")(sequelize, Sequelize.DataTypes);
db.Popup = require("./Popup.js")(sequelize, Sequelize.DataTypes);
db.Token = require("./Token.js")(sequelize, Sequelize.DataTypes);
db.PopupLog = require("./PopupLog.js")(sequelize, Sequelize.DataTypes);
db.GuideLog = require("./GuideLog.js")(sequelize, Sequelize.DataTypes);
erenfn marked this conversation as resolved.
Show resolved Hide resolved
db.Banner = require("./Banner.js")(sequelize, Sequelize.DataTypes);
db.Team = require("./Team.js")(sequelize, Sequelize.DataTypes);
db.Invite = require("./Invite.js")(sequelize, Sequelize.DataTypes);
Expand Down
12 changes: 12 additions & 0 deletions backend/src/routes/guidelog.routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const express = require("express");
const guidelogController = require("../controllers/guidelog.controller");
const {addGuideLogValidation, getLogByUserValidation} = require('../utils/guidelog.helper')
const {handleValidationErrors} = require('../middleware/validation.middleware')
const router = express.Router();

router.post("/add_guide_log", addGuideLogValidation, handleValidationErrors, guidelogController.addGuideLog);
router.get("/all_guide_logs", guidelogController.getAllLogs);
router.get("/guide_logs_by_user", getLogByUserValidation, handleValidationErrors, guidelogController.getLogsByUser);
router.get("/complete_guide_logs", getLogByUserValidation, handleValidationErrors, guidelogController.getCompleteGuideLogs);
erenfn marked this conversation as resolved.
Show resolved Hide resolved

module.exports = router;
10 changes: 0 additions & 10 deletions backend/src/routes/popuplog.routes.js

This file was deleted.

43 changes: 43 additions & 0 deletions backend/src/service/guidelog.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const db = require("../models");
const GuideLog = db.GuideLog;

class GuideLogService {
async addGuideLog({ popupType, userId, guideId, completed }) {
return await GuideLog.create({
popupType,
userId,
guideId,
showingTime: new Date(),
completed
});
}
erenfn marked this conversation as resolved.
Show resolved Hide resolved
async getAllGuideLogs() {
return await GuideLog.findAll();
}
erenfn marked this conversation as resolved.
Show resolved Hide resolved
async getCompleteGuideLogs(userId) {
try {
return await GuideLog.findAll({
where: {
userId: userId,
completed: true,
},
});
} catch (err) {
throw new Error("Error retrieving Guide Log by UserID");
}
erenfn marked this conversation as resolved.
Show resolved Hide resolved
}

async getLogsByUser(userId) {
try {
return await GuideLog.findAll({
where: {
userId: userId,
},
});
} catch (err) {
throw new Error("Error retrieving Guide Log by UserID");
}
}
}

module.exports = new GuideLogService();
18 changes: 0 additions & 18 deletions backend/src/service/popuplog.service.js

This file was deleted.

Loading
Loading