Skip to content

Commit

Permalink
Merge pull request #48 from cf15-t5/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
lutfianRhdn authored Aug 24, 2023
2 parents 677b636 + 8f3f295 commit db6e458
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 30 deletions.
17 changes: 16 additions & 1 deletion src/services/AuthService.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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:
Expand All @@ -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:
Expand Down
80 changes: 56 additions & 24 deletions src/services/TicketService.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -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:
Expand Down
7 changes: 2 additions & 5 deletions src/utils/sendMail.py
Original file line number Diff line number Diff line change
@@ -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],
Expand Down
73 changes: 73 additions & 0 deletions templates/html/registeredEoNotification.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>New Event Organizer Registration Notification</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
}

.container {
max-width: 600px;
margin: 0 auto;
padding: 20px;
background-color: #fff;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

h1 {
color: #333;
}

p {
color: #555;
}

.notification {
background-color: #007bff;
color: #fff;
padding: 10px;
text-align: center;
}

.footer {
text-align: center;
margin-top: 20px;
}

.activate-button {
display: inline-block;
padding: 10px 20px;
background-color: #007bff;
color: #fff;
text-decoration: none;
border-radius: 5px;
}
</style>
</head>
<body>
<div class="container">
<h1>New Event Organizer Registration Notification</h1>
<div class="notification">
<p>A new event organizer has registered on your website.</p>
</div>
<p>Event Organizer Information:</p>
<ul>
<li>Event Organizer Name: {{name}}</li>
<li>Email: {{email}}</li>
</ul>
<p>Please contact the event organizer for further details.</p>
<p>To activate this EO account, please click the button below:</p>
<a class="activate-button" href="https://seticket.aprnna.me">Activate EO Account</a>
<div class="footer">
<p>Thank you,</p>
<p>The Admin Team</p>
</div>
</div>
</body>
</html>
83 changes: 83 additions & 0 deletions templates/html/soldOutEventNotification.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Event Sold Out Notification</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
}
.container {
max-width: 600px;
margin: 0 auto;
background-color: #ffffff;
padding: 20px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
h1 {
color: #333;
}
p {
line-height: 1.6;
color: #666;
}
.event-details {
background-color: #f9f9f9;
padding: 10px;
border-radius: 5px;
}
.button {
display: inline-block;
padding: 10px 20px;
background-color: #007BFF;
color: #ffffff;
text-decoration: none;
border-radius: 5px;
}
.ticket-count {
background-color: #007BFF;
color: #ffffff;
padding: 10px;
border-radius: 5px;
margin-top: 20px;
}
.event-banner {
margin-bottom: 20px;
max-height: 20rem;
overflow: hidden;
}
</style>
</head>
<body>
<div class="container">
<div class="event-banner">
<img src="{{image_url}}" alt="Event Banner" width="100%" height="20vh">
</div>
<h1>Event Sold Out Notification</h1>
<p>Dear Event Organizer {{name}},</p>
<p>We regret to inform you that your event, {{event_name}}, has sold out. Here are the event details:</p>

<div class="event-details">
<h2>Event Details</h2>
<p><strong>Event Name:</strong> {{event_name}}</p>
<p><strong>Date:</strong> {{event_date}}</p>
<p><strong>Time:</strong> {{event_time}}</p>
<p><strong>Address:</strong> {{location}}</p>
<p><strong>Category:</strong> {{category}}</p>
</div>

<p>The event has reached its maximum ticket count of <strong>{{ticket_count}}</strong> tickets.</p>

<p>If you have any further questions or require assistance, please do not hesitate to contact us.</p>
<p>Thank you for using our platform for your event. We hope it is a great success!</p>

<p>Best Regards,<br>The SeTicket Team</p>

</div>
</body>
</html>

0 comments on commit db6e458

Please sign in to comment.