diff --git a/src/services/AuthService.py b/src/services/AuthService.py index 49e1bd4..8e89f15 100644 --- a/src/services/AuthService.py +++ b/src/services/AuthService.py @@ -2,6 +2,8 @@ from src.utils.errorHandler import errorHandler from src.repositories.UserRepository import UserRepository import bcrypt +from flask import render_template +from src.utils.sendMail import sendMail import src.utils.jwt as jwt from src.utils.convert import queryResultToDict user_repository = UserRepository() @@ -14,6 +16,18 @@ def failedOrSuccessRequest(status, code, data): "code": code, 'data': data, } + def _sendNotification(self,data): + templates = render_template( + 'html/registeredEoNotification.html', + name=data.name, + email=data.email, + ) + sendMail( + templates=templates, + subject="Ticket Event", + to=data.user.email + ) + return True def registerUser(self, data): try: @@ -24,8 +38,9 @@ def registerUser(self, data): validate = RegisterValidator(**data) if not validate: return self.failedOrSuccessRequest('failed', 400, 'Validation failed') - newUser = user_repository.createNewUser(data) + if(newUser.role == 'EVENT_ORGANIZER'): + self._sendNotification(newUser) return self.failedOrSuccessRequest('success', 201, queryResultToDict([newUser])[0]) except ValueError as e: diff --git a/src/services/TicketService.py b/src/services/TicketService.py index c9b3601..6814c59 100644 --- a/src/services/TicketService.py +++ b/src/services/TicketService.py @@ -9,6 +9,7 @@ from src.utils.errorHandler import errorHandler from src.utils.sendMail import sendMail from datetime import datetime +from flask import render_template ticketRepository = TicketRepository() userRepository = UserRepository() @@ -23,7 +24,50 @@ def failedOrSuccessRequest(status, code, data): "code": code, 'data': data, } - + def _validationCreateNewTicket(self,event,user): + if(not event): + return self.failedOrSuccessRequest('failed', 400, 'event not found') + if (len(event.tickets) >= event.number_of_ticket): + return self.failedOrSuccessRequest('failed', 400, 'ticket sold out') + today = datetime.today() + if (event.date_of_event.date() < today.date()): + return self.failedOrSuccessRequest('failed', 400, 'event has passed') + if([item for item in event.tickets if item.user_id ==user.user_id]): + return self.failedOrSuccessRequest('failed', 400, 'you already have ticket for this event') + if user.balance < event.price: + return self.failedOrSuccessRequest('failed', 400, 'balance not enough') + return True + def _purchaseTicket(self,data): + date = data.event.date_of_event + event_date = date.strftime("%d %B %Y") + event_time = date.strftime("%H:%M") + return render_template( + 'html/mail.html', + code=data.ticket_code, + event_name=data.event.title, + location=data.event.address, + name=data.user.name, + date=event_date, + time=event_time) + def _soldOutEvent(self,data): + return render_template( + 'html/soldOutEventNotification.html', + event_name=data.title, + location=data.address, + event_date=data.date_of_event.strftime("%d %B %Y"), + event_time=data.date_of_event.strftime("%H:%M"), + ticket_count=len(data.tickets), + category=data.category.name, + name=data.user.name, + image_url=f"https://api-seticket.aprnna.me/{data.poster_path.replace('public/','')}" + ) + def _sendNotification(self,templates,to,subject): + sendMail( + templates=templates, + subject=subject, + to=to + ) + return True def getAllTickets(self): try: data = ticketRepository.getAllTickets() @@ -34,23 +78,14 @@ def getAllTickets(self): def createNewTicket(self,data,user_id): try: validate = CreateNewTicketValidator(**data) + if(not validate): return self.failedOrSuccessRequest('failed', 400, validate.errors()) - - user = userRepository.getUserById(user_id) event = eventRepository.getEventById(data['event_id']) - if(not event): - return self.failedOrSuccessRequest('failed', 400, 'event not found') - if (len(event.tickets) >= event.number_of_ticket): - return self.failedOrSuccessRequest('failed', 400, 'ticket sold out') - today = datetime.today() - if (event.date_of_event.date() < today.date()): - return self.failedOrSuccessRequest('failed', 400, 'event has passed') - if([item for item in event.tickets if item.user_id ==user_id]): - return self.failedOrSuccessRequest('failed', 400, 'you already have ticket for this event') - - if user.balance < event.price: - return self.failedOrSuccessRequest('failed', 400, 'balance not enough') + user = userRepository.getUserById(user_id) + + validation = self._validationCreateNewTicket(event,user) + if(validation != True): return validation userRepository.updateBalance(id=user_id,nominal=event.price,operator='minus') userRepository.updateBalance(id=event.user_id,nominal=event.price,operator='plus') data = ticketRepository.createNewTicket(data,user_id) @@ -60,15 +95,12 @@ def createNewTicket(self,data,user_id): nominal=event.price, ticket_id=data.ticket_id ) - sendMail( - name=data.user.name, - code=data.ticket_code, - date=data.event.date_of_event, - event_name=data.event.title, - location=data.event.address, - subject="Ticket Event", - to=data.user.email - ) + # if(): + # print() + if(len(event.tickets) >= event.number_of_ticket-1): + + self._sendNotification(subject="Event Sold Out" , templates=self._soldOutEvent(event),to=event.user.email) + self._sendNotification(subject="Ticket Event",templates=self._purchaseTicket(data),to=user.email) return self.failedOrSuccessRequest('success', 200, queryResultToDict([data])[0]) except ValueError as e: diff --git a/src/utils/sendMail.py b/src/utils/sendMail.py index b48ee78..a833133 100644 --- a/src/utils/sendMail.py +++ b/src/utils/sendMail.py @@ -1,12 +1,9 @@ from flask_mail import Message -from flask import render_template from src.server.main import mail from datetime import datetime -def sendMail(to,subject,code,event_name,location, date:datetime,name): +def sendMail(to,subject,templates): # split date and time - event_date = date.strftime("%d %B %Y") - event_time = date.strftime("%H:%M") - templates = render_template('html/mail.html',code=code,event_name=event_name,location=location,name=name, date=event_date,time=event_time) + msg = Message( subject, recipients=[to], diff --git a/templates/html/registeredEoNotification.html b/templates/html/registeredEoNotification.html new file mode 100644 index 0000000..2506372 --- /dev/null +++ b/templates/html/registeredEoNotification.html @@ -0,0 +1,73 @@ + + + + + + New Event Organizer Registration Notification + + + +
+

New Event Organizer Registration Notification

+
+

A new event organizer has registered on your website.

+
+

Event Organizer Information:

+ +

Please contact the event organizer for further details.

+

To activate this EO account, please click the button below:

+ Activate EO Account + +
+ + diff --git a/templates/html/soldOutEventNotification.html b/templates/html/soldOutEventNotification.html new file mode 100644 index 0000000..fe6a529 --- /dev/null +++ b/templates/html/soldOutEventNotification.html @@ -0,0 +1,83 @@ + + + + + + Event Sold Out Notification + + + +
+
+ Event Banner +
+

Event Sold Out Notification

+

Dear Event Organizer {{name}},

+

We regret to inform you that your event, {{event_name}}, has sold out. Here are the event details:

+ +
+

Event Details

+

Event Name: {{event_name}}

+

Date: {{event_date}}

+

Time: {{event_time}}

+

Address: {{location}}

+

Category: {{category}}

+
+ +

The event has reached its maximum ticket count of {{ticket_count}} tickets.

+ +

If you have any further questions or require assistance, please do not hesitate to contact us.

+

Thank you for using our platform for your event. We hope it is a great success!

+ +

Best Regards,
The SeTicket Team

+ +
+ + +