Änderungen am TAgebuch
This commit is contained in:
@@ -4,10 +4,15 @@ import User from '../models/User.js';
|
||||
import { sendActivationEmail } from './emailService.js';
|
||||
|
||||
const register = async (email, password) => {
|
||||
const activationCode = Math.random().toString(36).substring(2, 15);
|
||||
const user = await User.create({ email, password, activationCode });
|
||||
await sendActivationEmail(email, activationCode);
|
||||
return user;
|
||||
try {
|
||||
const activationCode = Math.random().toString(36).substring(2, 15);
|
||||
const user = await User.create({ email, password, activationCode });
|
||||
await sendActivationEmail(email, activationCode);
|
||||
return user;
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
const activateUser = async (activationCode) => {
|
||||
@@ -21,11 +26,12 @@ const activateUser = async (activationCode) => {
|
||||
|
||||
const login = async (email, password) => {
|
||||
const user = await User.findOne({ where: { email } });
|
||||
if (!user || !user.isActive) throw new Error('Invalid email or password');
|
||||
if (!user || !user.isActive) throw new Error('Invalid email or password.');
|
||||
const isPasswordValid = await bcrypt.compare(password, user.password);
|
||||
if (!isPasswordValid) throw new Error('Invalid email or password');
|
||||
if (!isPasswordValid) throw new Error('Invalid email or password!');
|
||||
const token = jwt.sign({ userId: user.hashedId }, process.env.JWT_SECRET, { expiresIn: '1h' });
|
||||
await user.update({ hashedId: token });
|
||||
user.authCode = token;
|
||||
await user.save();
|
||||
return { token };
|
||||
};
|
||||
|
||||
|
||||
70
backend/services/diaryService.js
Normal file
70
backend/services/diaryService.js
Normal file
@@ -0,0 +1,70 @@
|
||||
import DiaryDate from '../models/DiaryDates.js';
|
||||
import Club from '../models/Club.js';
|
||||
import { checkAccess } from '../utils/userUtils.js';
|
||||
import HttpError from '../exceptions/HttpError.js';
|
||||
|
||||
class DiaryService {
|
||||
async getDatesForClub(userToken, clubId) {
|
||||
console.log('[DiaryService::getDatesForClub] - Check user access');
|
||||
await checkAccess(userToken, clubId);
|
||||
console.log('[DiaryService::getDatesForClub] - Validate club existence');
|
||||
const club = await Club.findByPk(clubId);
|
||||
if (!club) {
|
||||
throw new HttpError('Club not found', 404);
|
||||
}
|
||||
console.log('[DiaryService::getDatesForClub] - Load diary dates');
|
||||
const dates = await DiaryDate.findAll({
|
||||
where: { clubId },
|
||||
order: [['date', 'ASC'], ['trainingStart', 'ASC']]
|
||||
});
|
||||
|
||||
return dates;
|
||||
}
|
||||
|
||||
async createDateForClub(userToken, clubId, date, trainingStart, trainingEnd) {
|
||||
console.log('[DiaryService::createDateForClub] - Check user access');
|
||||
await checkAccess(userToken, clubId);
|
||||
console.log('[DiaryService::createDateForClub] - Validate club existence');
|
||||
const club = await Club.findByPk(clubId);
|
||||
if (!club) {
|
||||
throw new HttpError('Club not found', 404);
|
||||
}
|
||||
console.log('[DiaryService::createDateForClub] - Validate date');
|
||||
const parsedDate = new Date(date);
|
||||
if (isNaN(parsedDate.getTime())) {
|
||||
throw new HttpError('Invalid date format', 400);
|
||||
}
|
||||
if (trainingStart && trainingEnd && trainingStart >= trainingEnd) {
|
||||
throw new HttpError('Training start time must be before training end time', 400);
|
||||
}
|
||||
console.log('[DiaryService::createDateForClub] - Create new diary date');
|
||||
const newDate = await DiaryDate.create({
|
||||
date: parsedDate,
|
||||
clubId,
|
||||
trainingStart: trainingStart || null,
|
||||
trainingEnd: trainingEnd || null,
|
||||
});
|
||||
|
||||
return newDate;
|
||||
}
|
||||
|
||||
async updateTrainingTimes(userToken, clubId, date, trainingStart, trainingEnd) {
|
||||
console.log('[DiaryService::updateTrainingTimes] - Check user access');
|
||||
await checkAccess(userToken, clubId);
|
||||
console.log('[DiaryService::updateTrainingTimes] - Validate date');
|
||||
const diaryDate = await DiaryDate.findOne({ where: { clubId, date } });
|
||||
if (!diaryDate) {
|
||||
throw new HttpError('Diary entry not found', 404);
|
||||
}
|
||||
if (trainingStart && trainingEnd && trainingStart >= trainingEnd) {
|
||||
throw new HttpError('Training start time must be before training end time', 400);
|
||||
}
|
||||
console.log('[DiaryService::updateTrainingTimes] - Update training times');
|
||||
diaryDate.trainingStart = trainingStart || null;
|
||||
diaryDate.trainingEnd = trainingEnd || null;
|
||||
await diaryDate.save();
|
||||
return diaryDate;
|
||||
}
|
||||
}
|
||||
|
||||
export default DiaryService;
|
||||
@@ -3,17 +3,17 @@ import nodemailer from 'nodemailer';
|
||||
const transporter = nodemailer.createTransport({
|
||||
service: 'Gmail',
|
||||
auth: {
|
||||
user: process.env.EMAIL_USER,
|
||||
pass: process.env.EMAIL_PASS,
|
||||
user: process.env.EMAIL_USER,
|
||||
pass: process.env.EMAIL_PASS,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
const sendActivationEmail = async (email, activationCode) => {
|
||||
const mailOptions = {
|
||||
from: process.env.EMAIL_USER,
|
||||
to: email,
|
||||
subject: 'Account Activation',
|
||||
text: `Activate your account by clicking the following link: ${process.env.BASE_URL}/api/auth/activate/${activationCode}`,
|
||||
text: `Activate your account by clicking the following link: ${process.env.BASE_URL}/activate/${activationCode}`,
|
||||
};
|
||||
await transporter.sendMail(mailOptions);
|
||||
};
|
||||
|
||||
@@ -1,19 +1,81 @@
|
||||
import UserClub from "../models/UserClub.js";
|
||||
import { checkAccess } from "../utils/userUtils.js";
|
||||
import { getUserByToken } from "../utils/userUtils.js";
|
||||
import HttpError from "../exceptions/HttpError.js";
|
||||
import Member from "../models/Member.js";
|
||||
import { response } from "express";
|
||||
|
||||
class MemberService {
|
||||
async getApprovalRequests(userToken, clubId) {
|
||||
checkAccess(userToken, clubId);
|
||||
return UserClub.findAll({
|
||||
console.log('[MemberService::getApprovalRequest] - Check user access');
|
||||
await checkAccess(userToken, clubId);
|
||||
console.log('[MemberService::getApprovalRequest] - Load user');
|
||||
const user = await getUserByToken(userToken);
|
||||
console.log('[MemberService::getApprovalRequest] - Load userclub');
|
||||
return await UserClub.findAll({
|
||||
where: {
|
||||
clubId: clubId,
|
||||
approved: false
|
||||
approved: false,
|
||||
userId: user.id
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async getClubMembers(clubId) {
|
||||
|
||||
async getClubMembers(userToken, clubId) {
|
||||
console.log('[getClubMembers] - Check access');
|
||||
await checkAccess(userToken, clubId);
|
||||
console.log('[getClubMembers] - Find members');
|
||||
const members = await Member.findAll({ where: { clubId: clubId }});
|
||||
console.log('[getClubMembers] - return members');
|
||||
return members;
|
||||
}
|
||||
|
||||
async setClubMember(userToken, clubId, memberId, firstName, lastName, street, city, birthdate, phone, email) {
|
||||
try {
|
||||
console.log('[setClubMembers] - Check access');
|
||||
await checkAccess(userToken, clubId);
|
||||
console.log('[setClubMembers] - set default member');
|
||||
let member = null;
|
||||
console.log('[setClubMembers] - load member if possible');
|
||||
if (memberId) {
|
||||
member = await Member.findOne({ where: { id: memberId }});
|
||||
}
|
||||
console.log('[setClubMembers] - set member');
|
||||
if (member) {
|
||||
member.firstName = firstName;
|
||||
member.lastName = lastName;
|
||||
member.street = street;
|
||||
member.city = city;
|
||||
member.birthDate = birthdate;
|
||||
member.phone = phone;
|
||||
member.email = email;
|
||||
await member.save();
|
||||
} else {
|
||||
await Member.create({
|
||||
firstName: firstName,
|
||||
lastName: lastName,
|
||||
street: street,
|
||||
city: city,
|
||||
birthDate: birthdate,
|
||||
phone: phone,
|
||||
email: email,
|
||||
clubId: clubId,
|
||||
});
|
||||
}
|
||||
console.log('[setClubMembers] - load club members');
|
||||
const members = await this.getClubMembers(userToken, clubId);
|
||||
console.log('[setClubMembers] - return response');
|
||||
return {
|
||||
status: 200,
|
||||
response: members,
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return {
|
||||
status: error.statusCode || 500,
|
||||
response: { error: "nocreation" }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user