Added accidents to diary
This commit is contained in:
23
backend/controllers/accidentController.js
Normal file
23
backend/controllers/accidentController.js
Normal file
@@ -0,0 +1,23 @@
|
||||
import AccidentService from '../services/accidentService.js';
|
||||
|
||||
export const addAccident = async (req, res) => {
|
||||
try {
|
||||
const { authcode: userToken } = req.headers;
|
||||
const { clubId, memberId, diaryDateId, accident } = req.body;
|
||||
await AccidentService.createAccident(userToken, clubId, memberId, diaryDateId, accident);
|
||||
res.status(201).json({ message: 'Accident added successfully' });
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: 'Error adding accident', error: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
export const getAccidents = async (req, res) => {
|
||||
try {
|
||||
const { authcode: userToken } = req.headers;
|
||||
const { clubId, dateId } = req.params;
|
||||
const accidents = await AccidentService.getAccidents(userToken, clubId, dateId);
|
||||
res.status(200).json(accidents);
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: 'Error getting activities', error: error.message });
|
||||
}
|
||||
}
|
||||
43
backend/models/Accident.js
Normal file
43
backend/models/Accident.js
Normal file
@@ -0,0 +1,43 @@
|
||||
import { DataTypes } from 'sequelize';
|
||||
import sequelize from '../database.js';
|
||||
import bcrypt from 'bcrypt';
|
||||
import { encryptData, decryptData } from '../utils/encrypt.js';
|
||||
|
||||
const Accident = sequelize.define('Accident', {
|
||||
memberId: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false,
|
||||
},
|
||||
diaryDateId: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false,
|
||||
},
|
||||
accident: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
set(value) {
|
||||
const encryptedValue = encryptData(value);
|
||||
this.setDataValue('accident', encryptedValue);
|
||||
},
|
||||
get() {
|
||||
const encryptedValue = this.getDataValue('accident');
|
||||
return decryptData(encryptedValue);
|
||||
}
|
||||
},
|
||||
salt: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: true,
|
||||
},
|
||||
}, {
|
||||
tableName: 'accident',
|
||||
underscored: true,
|
||||
timestamps: true,
|
||||
hooks: {
|
||||
beforeCreate: async (user) => {
|
||||
const salt = await bcrypt.genSalt(10);
|
||||
user.salt = salt;
|
||||
},
|
||||
}
|
||||
});
|
||||
|
||||
export default Accident;
|
||||
@@ -4,7 +4,7 @@ import sequelize from '../database.js';
|
||||
import Club from './Club.js';
|
||||
import { encryptData, decryptData } from '../utils/encrypt.js';
|
||||
|
||||
const Member = sequelize.define('User', {
|
||||
const Member = sequelize.define('Member', {
|
||||
id: {
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true,
|
||||
|
||||
@@ -21,6 +21,7 @@ import Season from './Season.js';
|
||||
import Location from './Location.js';
|
||||
import Group from './Group.js';
|
||||
import GroupActivity from './GroupActivity.js';
|
||||
import Accident from './Accident.js';
|
||||
|
||||
User.hasMany(Log, { foreignKey: 'userId' });
|
||||
Log.belongsTo(User, { foreignKey: 'userId' });
|
||||
@@ -114,6 +115,12 @@ DiaryDateTag.belongsTo(DiaryTag, { foreignKey: 'tagId', as: 'tag' });
|
||||
DiaryMemberTag.belongsTo(DiaryDate, { foreignKey: 'diaryDateId', as: 'diaryDates' });
|
||||
DiaryDate.hasMany(DiaryMemberTag, { foreignKey: 'diaryDateId', as: 'diaryMemberTags' });
|
||||
|
||||
Accident.belongsTo(Member, { foreignKey: 'memberId', as: 'members' });
|
||||
Member.hasMany(Accident, { foreignKey: 'memberId', as: 'accidents' });
|
||||
|
||||
Accident.belongsTo(DiaryDate, { foreignKey: 'diaryDateId', as: 'diaryDates' });
|
||||
DiaryDate.hasMany(Accident, { foreignKey: 'diaryDateId', as: 'accidents' });
|
||||
|
||||
export {
|
||||
User,
|
||||
Log,
|
||||
@@ -137,4 +144,5 @@ export {
|
||||
Team,
|
||||
Group,
|
||||
GroupActivity,
|
||||
Accident,
|
||||
};
|
||||
|
||||
10
backend/routes/accidentRoutes.js
Normal file
10
backend/routes/accidentRoutes.js
Normal file
@@ -0,0 +1,10 @@
|
||||
import express from 'express';
|
||||
import { addAccident, getAccidents } from '../controllers/accidentController.js';
|
||||
import { authenticate } from '../middleware/authMiddleware.js';
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.post('/', authenticate, addAccident);
|
||||
router.get('/:clubId/:dateId', authenticate, getAccidents);
|
||||
|
||||
export default router;
|
||||
@@ -7,7 +7,8 @@ import {
|
||||
User, Log, Club, UserClub, Member, DiaryDate, Participant, Activity, MemberNote,
|
||||
DiaryNote, DiaryTag, MemberDiaryTag, DiaryDateTag, DiaryMemberNote, DiaryMemberTag,
|
||||
PredefinedActivity, DiaryDateActivity, Match, League, Team, Group,
|
||||
GroupActivity
|
||||
GroupActivity,
|
||||
Accident
|
||||
} from './models/index.js';
|
||||
import authRoutes from './routes/authRoutes.js';
|
||||
import clubRoutes from './routes/clubRoutes.js';
|
||||
@@ -27,6 +28,7 @@ import Location from './models/Location.js';
|
||||
import groupRoutes from './routes/groupRoutes.js';
|
||||
import diaryDateTagRoutes from './routes/diaryDateTagRoutes.js';
|
||||
import sessionRoutes from './routes/sessionRoutes.js';
|
||||
import accidentRoutes from './routes/accidentRoutes.js';
|
||||
|
||||
const app = express();
|
||||
const port = process.env.PORT || 3000;
|
||||
@@ -53,6 +55,7 @@ app.use('/api/matches', matchRoutes);
|
||||
app.use('/api/group', groupRoutes);
|
||||
app.use('/api/diarydatetags', diaryDateTagRoutes);
|
||||
app.use('/api/session', sessionRoutes);
|
||||
app.use('/api/accident', accidentRoutes);
|
||||
|
||||
app.use(express.static(path.join(__dirname, '../frontend/dist')));
|
||||
|
||||
@@ -88,6 +91,7 @@ app.get('*', (req, res) => {
|
||||
await Match.sync({ alter: true });
|
||||
await Group.sync({ alter: true });
|
||||
await GroupActivity.sync({ alter: true });
|
||||
await Accident.sync({ alter: true });
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Server is running on http://localhost:${port}`);
|
||||
|
||||
62
backend/services/accidentService.js
Normal file
62
backend/services/accidentService.js
Normal file
@@ -0,0 +1,62 @@
|
||||
import Accident from '../models/Accident.js';
|
||||
import DiaryDate from '../models/DiaryDates.js';
|
||||
import Member from '../models/Member.js';
|
||||
import { checkAccess, getUserByToken} from '../utils/userUtils.js';
|
||||
|
||||
class AccidentService {
|
||||
async createAccident(userToken, clubId, memberId, diaryDateId, accident) {
|
||||
await checkAccess(userToken, clubId);
|
||||
const user = await getUserByToken(userToken);
|
||||
if (!user) {
|
||||
console.log('---------------');
|
||||
throw new Error('User not found');
|
||||
}
|
||||
const member = await Member.findByPk(memberId);
|
||||
if (!member || member.clubId != clubId) {
|
||||
throw new Error('Member not found');
|
||||
}
|
||||
console.log(diaryDateId);
|
||||
const diaryDate = await DiaryDate.findByPk(diaryDateId);
|
||||
if (!diaryDate || diaryDate.clubId != clubId) {
|
||||
throw new Error('Diary date not found');
|
||||
}
|
||||
const newAccident = await Accident.create({
|
||||
memberId,
|
||||
diaryDateId: diaryDate.id,
|
||||
accident,
|
||||
});
|
||||
|
||||
return newAccident;
|
||||
}
|
||||
|
||||
async getAccidents(userToken, clubId, diaryDateId) {
|
||||
await checkAccess(userToken, clubId);
|
||||
const user = await getUserByToken(userToken);
|
||||
if (!user) {
|
||||
throw new Error('User not found');
|
||||
}
|
||||
const diaryDate = await DiaryDate.findByPk(diaryDateId);
|
||||
if (!diaryDate || diaryDate.clubId != clubId) {
|
||||
throw new Error('Diary date not found');
|
||||
}
|
||||
const accidents = await Accident.findAll({
|
||||
where: {
|
||||
diaryDateId,
|
||||
},
|
||||
include: [{
|
||||
model: Member,
|
||||
as: 'members'
|
||||
}]
|
||||
});
|
||||
const cleanupedAccidents = accidents.map(accident => {
|
||||
return {
|
||||
accident: accident.accident,
|
||||
firstName: accident.members.firstName,
|
||||
lastName: accident.members.lastName,
|
||||
}
|
||||
})
|
||||
return cleanupedAccidents;
|
||||
}
|
||||
}
|
||||
|
||||
export default new AccidentService();
|
||||
@@ -1,6 +1,7 @@
|
||||
import User from '../models/User.js'
|
||||
import UserClub from '../models/UserClub.js';
|
||||
import HttpError from '../exceptions/HttpError.js';
|
||||
import { config } from 'dotenv';
|
||||
|
||||
export const getUserByToken = async(token) => {
|
||||
try {
|
||||
@@ -32,6 +33,7 @@ export const hasUserClubAccess = async(userId, clubId) => {
|
||||
console.log(error);
|
||||
throw new HttpError('notfound', 500);
|
||||
}
|
||||
console.log('---- no user found');
|
||||
}
|
||||
|
||||
export const checkAccess = async(userToken, clubId) => {
|
||||
|
||||
Reference in New Issue
Block a user