Skip to content

Commit

Permalink
Merge pull request #137 from MTUHIDE/1.1
Browse files Browse the repository at this point in the history
1.1
  • Loading branch information
Ghosts authored Jan 22, 2018
2 parents 98a67f3 + fe51485 commit a0f7b7a
Show file tree
Hide file tree
Showing 21 changed files with 559 additions and 345 deletions.
1 change: 0 additions & 1 deletion app/assets/stylesheets/partials/_footer.sass
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ footer
position: fixed
bottom: 0
background-color: $secondary-color
height: 35px
color: $primary-color
ul
padding-left: 0
Expand Down
7 changes: 6 additions & 1 deletion app/assets/stylesheets/partials/_sidebar.sass
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

//Sidebar
.sidebar
img
padding: 5px
z-index: 99
position: fixed
transition: 0.2s linear all
text-align: center
background-color: $primary-color
border-right: 2px solid lighten($primary-color, 5%)
float: left
height: 100vh
width: 64px
Expand Down Expand Up @@ -61,3 +62,7 @@
color: $secondary-color
&:hover
color: darken($secondary-color, 15%)


nav
overflow-y: auto
38 changes: 38 additions & 0 deletions app/controllers/ApplicationComponents/EmailScheduler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package controllers.ApplicationComponents;

import com.google.inject.Singleton;
import controllers.Databases.AppointmentsDB;
import models.AppointmentsModel;
import play.Logger;

import java.util.*;
import java.util.concurrent.TimeUnit;

@Singleton
public class EmailScheduler {
public EmailScheduler(){
Calendar today = Calendar.getInstance();
today.set(Calendar.HOUR_OF_DAY, 8);
today.set(Calendar.MINUTE, 0);
today.set(Calendar.SECOND, 0);
Timer timer = new Timer();
Logger.info("Email Reminder Service Started for - " + today.getTime().toString());
timer.schedule(new AppointmentEmailTask(), today.getTime(), TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS));
}

private class AppointmentEmailTask extends TimerTask {

@Override
public void run() {
Calendar startDay = Calendar.getInstance();
startDay.set(Calendar.HOUR_OF_DAY, 0);
Calendar endDay = Calendar.getInstance();
endDay.set(Calendar.HOUR_OF_DAY, 24);
Logger.info("Checking for daily appointments.");
List<AppointmentsModel> appointments = AppointmentsDB.getAppointmentsByDate(startDay.getTime(), endDay.getTime());
for(AppointmentsModel a : appointments) {
MailerService.sendAppointmentReminder(a);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package controllers;
package controllers.ApplicationComponents;

import com.google.inject.Inject;
import controllers.Application;
import controllers.SettingsController;
import models.AppointmentsModel;
import models.SettingsModel;
import play.api.Play;
import play.libs.mailer.Email;
import play.libs.mailer.MailerClient;

Expand All @@ -18,7 +19,7 @@ public class MailerService {
MailerService.mailerClient = mailerClient;
}

static void sendEmail(String subject, String fromName, String fromEmail, String toName, String toEmail, String bodyHtml) {
public static void sendEmail(String subject, String fromName, String fromEmail, String toName, String toEmail, String bodyHtml) {
Email email = new Email()
.setSubject(subject)
.setFrom(fromName +"<"+fromEmail+">")
Expand All @@ -27,7 +28,51 @@ static void sendEmail(String subject, String fromName, String fromEmail, String
mailerClient.send(email);
}

static void sendAppointmentConfirmation(AppointmentsModel appointment) {
static void sendAppointmentReminder(AppointmentsModel appointment) {
SettingsModel settings = SettingsController.getSettings();
/* Coach Email */
Email email = new Email()
.setSubject("Appointment Reminder for " + DateFormat.getDateTimeInstance().format(appointment.getStartDate()) + " at the " + settings.getCenterName() )
.setFrom("Project Burgundy <"+ Application.getConfig().getString("play.mailer.user") +">")
.addTo(appointment.getCoachName() + "<"+appointment.getCoachEmail()+">")
.setBodyHtml("<p style=\"font-size:48px;color:#17C671;text-align:center;\">&#x23F3;</p><h1 style=\"text-align:center;\">Appointment Reminder</h1> <h3>Details:</h3>" +
"<div style=\"background-color:#EFF0F1; border-radius:4px; padding: 10px;\">"+
appointment.toHTMLString() +
"</div>" +
"<br/>" +
"<a style=\"background-color:#0067F4; color: white; border-radius: 4px; padding: 10px; margin:0 auto; display:block; width: 90%; text-align: center; font-size: 18px; text-decoration:none; \" href=\"" +
"https://www.google.com/calendar/render?action=TEMPLATE" +
"&text=" + settings.getCenterName().replaceAll(" ", "+") + "+Appointment+With+" + appointment.getStudentName().replaceAll(" ", "+")+
"&dates=" +appointment.getStartDate().toInstant().toString().replaceAll("-","").replaceAll(":","")+"/"+appointment.getEndDate().toInstant().toString().replaceAll("-","").replaceAll(":","")+
"&details=" + appointment.toString().replace("\n", "%0A").replace(" ","+") +
"&location=" + settings.getCenterName().replaceAll(" ","+") +
"&sf=true" +
"&output=xml\"" +
"target=\"_blank\" rel=\"nofollow\">Add to my calendar</a> <br/>");
mailerClient.send(email);
/* Student Email */
email = new Email()
.setSubject("Appointment Reminder for " + DateFormat.getDateTimeInstance().format(appointment.getStartDate()) + " at the " + settings.getCenterName() )
.setFrom("Project Burgundy <"+ Application.getConfig().getString("play.mailer.user") +">")
.addTo(appointment.getStudentName() + "<"+appointment.getStudentEmail()+">")
.setBodyHtml("<p style=\"font-size:48px;color:#17C671;text-align:center;\">&#x23F3;</p><h1 style=\"text-align:center;\">Appointment Reminder</h1> <h3>Details:</h3>" +
"<div style=\"background-color:#EFF0F1; border-radius:4px; padding: 10px;\">"+
appointment.toHTMLString() +
"</div>" +
"<br/>" +
"<a style=\"background-color:#0067F4; color: white; border-radius: 4px; padding: 10px; margin:0 auto; display:block; width: 90%; text-align: center; font-size: 18px; text-decoration:none; \" href=\"" +
"https://www.google.com/calendar/render?action=TEMPLATE" +
"&text=" + settings.getCenterName().replaceAll(" ", "+") + "+Appointment+With+" + appointment.getCoachName().replaceAll(" ", "+")+
"&dates=" +appointment.getStartDate().toInstant().toString().replaceAll("-","").replaceAll(":","")+"/"+appointment.getEndDate().toInstant().toString().replaceAll("-","").replaceAll(":","")+
"&details=" + appointment.toString().replace("\n", "%0A").replace(" ","+") +
"&location=" + settings.getCenterName().replaceAll(" ","+") +
"&sf=true" +
"&output=xml\"" +
"target=\"_blank\" rel=\"nofollow\">Add to my calendar</a> <br/>");
mailerClient.send(email);
}

public static void sendAppointmentConfirmation(AppointmentsModel appointment) {
SettingsModel settings = SettingsController.getSettings();
/* Coach Email */
Email email = new Email()
Expand Down Expand Up @@ -71,7 +116,7 @@ static void sendAppointmentConfirmation(AppointmentsModel appointment) {
mailerClient.send(email);
}

static void sendAppointmentCancellation(AppointmentsModel appointment) {
public static void sendAppointmentCancellation(AppointmentsModel appointment) {
SettingsModel settings = SettingsController.getSettings();
/* Coach Email */
Email email = new Email()
Expand Down
40 changes: 37 additions & 3 deletions app/controllers/AppointmentsController.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package controllers;

import com.fasterxml.jackson.databind.JsonNode;
import controllers.ApplicationComponents.MailerService;
import controllers.Databases.AppointmentsDB;
import controllers.Databases.SettingsDB;
import models.AppointmentsModel;
import play.libs.Json;
import play.mvc.Controller;
Expand Down Expand Up @@ -32,6 +34,8 @@ public Result createAppointment() {
/* Get user object from request */
JsonNode json = request().body().asJson();
/* Get user from json request */
String uniqueId = UUID.randomUUID().toString();

AppointmentsModel appointment = new AppointmentsModel();
appointment.setCoachId(json.findPath("coachId").textValue());
appointment.setStudentId(json.findPath("studentId").textValue());
Expand All @@ -42,8 +46,36 @@ public Result createAppointment() {
appointment.setPresent(Boolean.getBoolean(json.findPath("present").textValue()));
appointment.setServiceType(json.findPath("serviceType").textValue());
appointment.setWeekly(json.findPath("weekly").asBoolean());
/* Check if user is in DB */
appointment.setWeeklyId(uniqueId);

appointment = AppointmentsDB.addAppointment(appointment);
if ( appointment.isWeekly() ) {
Calendar currentDate = DatatypeConverter.parseDateTime(json.findPath("startDate").textValue());
Calendar endDate = DatatypeConverter.parseDateTime(json.findPath("endDate").textValue());
Calendar semesterEnd = Calendar.getInstance();
semesterEnd.setTime(SettingsDB.getSettings().getSemesterEnd());
currentDate.add(Calendar.DAY_OF_YEAR, 7);
endDate.add(Calendar.DAY_OF_YEAR, 7);

while ( currentDate.before(semesterEnd) ){
AppointmentsModel newAppointment = new AppointmentsModel();
newAppointment.setCoachId(json.findPath("coachId").textValue());
newAppointment.setStudentId(json.findPath("studentId").textValue());
newAppointment.setAppointmentType(json.findPath("appointmentType").textValue());
newAppointment.setStartDate(currentDate.getTime());
newAppointment.setEndDate(endDate.getTime());
newAppointment.setAppointmentNotes(json.findPath("appointmentNotes").textValue());
newAppointment.setPresent(Boolean.getBoolean(json.findPath("present").textValue()));
newAppointment.setServiceType(json.findPath("serviceType").textValue());
newAppointment.setWeekly(json.findPath("weekly").asBoolean());
newAppointment.setWeeklyId(uniqueId);
AppointmentsDB.addAppointment(newAppointment);
currentDate.add(Calendar.DAY_OF_YEAR, 7);
endDate.add(Calendar.DAY_OF_YEAR, 7);
}
}

/* Check if user is in DB */
AppointmentsModel finalAppointment = appointment;
new Thread(() -> MailerService.sendAppointmentConfirmation(finalAppointment)).start();
return ok();
Expand All @@ -67,8 +99,10 @@ public Result updateCoachNotes(){
return ok();
}

public Result appointmentsForUser(String role, String userId) {
List<AppointmentsModel> appointments = AppointmentsDB.getAppointmentsForUser(role, userId);
public Result appointmentsForUser(String role, String userId, String start, String end) {
Date startDate = DatatypeConverter.parseDateTime(start).getTime();
Date endDate = DatatypeConverter.parseDateTime(end).getTime();
List<AppointmentsModel> appointments = AppointmentsDB.getAppointmentsByUserAndDate(userId, startDate, endDate);
return ok(Json.toJson(appointments));
}
}
39 changes: 27 additions & 12 deletions app/controllers/Databases/AppointmentsDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public static AppointmentsModel getAppointment(String appointmentId) {
document.getBoolean("present"),
document.getString("appointment_type"),
document.getString("service_type"),
document.getBoolean("weekly"));
document.getBoolean("weekly"),
document.getString("weeklyId"));
} else {
/* Log something */
}
Expand Down Expand Up @@ -89,7 +90,8 @@ public static synchronized List<AppointmentsModel> getAppointmentsForUser(String
document.getBoolean("present"),
document.getString("appointment_type"),
document.getString("service_type"),
document.getBoolean("weekly"));
document.getBoolean("weekly"),
document.getString("weeklyId"));
appointmentList.add(appointment);
}
for (DocumentSnapshot document : documentsStudent) {
Expand All @@ -110,7 +112,8 @@ public static synchronized List<AppointmentsModel> getAppointmentsForUser(String
document.getBoolean("present"),
document.getString("appointment_type"),
document.getString("service_type"),
document.getBoolean("weekly"));
document.getBoolean("weekly"),
document.getString("weeklyId"));
appointmentList.add(appointment);
}
return appointmentList;
Expand Down Expand Up @@ -154,7 +157,8 @@ public static synchronized List<AppointmentsModel> getNextFiveAppointmentsForUse
document.getBoolean("present"),
document.getString("appointment_type"),
document.getString("service_type"),
document.getBoolean("weekly"));
document.getBoolean("weekly"),
document.getString("weeklyId"));
appointmentList.add(appointment);
}
for (DocumentSnapshot document : documentsStudent) {
Expand All @@ -175,20 +179,28 @@ public static synchronized List<AppointmentsModel> getNextFiveAppointmentsForUse
document.getBoolean("present"),
document.getString("appointment_type"),
document.getString("service_type"),
document.getBoolean("weekly"));
document.getBoolean("weekly"),
document.getString("weeklyId"));
appointmentList.add(appointment);
}
Collections.sort(appointmentList, new Comparator<AppointmentsModel>() {
public int compare(AppointmentsModel app1, AppointmentsModel app2) {
return app1.getStartDate().compareTo(app2.getStartDate());
}
});
appointmentList.sort(Comparator.comparing(AppointmentsModel::getStartDate));
if ( appointmentList.size() > 5 ) {
appointmentList = appointmentList.subList(0, 5);
}
return appointmentList;
}

public static synchronized List<AppointmentsModel> getAppointmentsByUserAndDate(String userId, Date start, Date end) {
UsersModel user = UserDB.getUser(userId);
List<AppointmentsModel> appointmentList = getAppointmentsByDate(start, end);
if( user.getRole().equals("Coach") || (user.isCoach() != null && user.isCoach())){
appointmentList.removeIf(i -> !i.getCoachId().equals(user.getUid()));
} else {
appointmentList.removeIf(i -> !i.getStudentId().equals(user.getUid()));
}
return appointmentList;
}

public static synchronized List<AppointmentsModel> getAppointmentsByDate(Date start, Date end) {
List<AppointmentsModel> appointmentList = new ArrayList<>();
/* Asynchronously retrieve all appointments */
Expand Down Expand Up @@ -222,7 +234,8 @@ public static synchronized List<AppointmentsModel> getAppointmentsByDate(Date st
document.getBoolean("present"),
document.getString("appointment_type"),
document.getString("service_type"),
document.getBoolean("weekly"));
document.getBoolean("weekly"),
document.getString("weeklyId"));
appointmentList.add(appointment);
} else {
break;
Expand Down Expand Up @@ -263,7 +276,8 @@ public static synchronized List<AppointmentsModel> getAppointments() {
document.getBoolean("present"),
document.getString("appointment_type"),
document.getString("service_type"),
document.getBoolean("weekly"));
document.getBoolean("weekly"),
document.getString("weeklyId"));
appointmentList.add(appointment);
}
return appointmentList;
Expand Down Expand Up @@ -297,6 +311,7 @@ public static synchronized AppointmentsModel addAppointment(AppointmentsModel ap
data.put("appointment_type", appointment.getAppointmentType());
data.put("service_type",appointment.getServiceType());
data.put("weekly", appointment.isWeekly());
data.put("weeklyId", appointment.getWeeklyId());
/* Asynchronously write appointment into DB */
ApiFuture<WriteResult> result = docRef.set(data);

Expand Down
1 change: 1 addition & 0 deletions app/controllers/FeedbackController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers;

import com.fasterxml.jackson.databind.JsonNode;
import controllers.ApplicationComponents.MailerService;
import play.mvc.Controller;
import play.mvc.Result;

Expand Down
8 changes: 7 additions & 1 deletion app/models/AppointmentsModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class AppointmentsModel {
private String appointmentType;
private String serviceType;
private boolean weekly;
private String weeklyId;

public AppointmentsModel( ) {

Expand All @@ -43,7 +44,8 @@ public AppointmentsModel(
boolean present,
String appointmentType,
String serviceType,
boolean weekly) {
boolean weekly,
String weeklyId) {
this.appointmentId = appointmentId;
this.startDate = startDate;
this.endDate = endDate;
Expand Down Expand Up @@ -183,6 +185,10 @@ public void setServiceType(String serviceType) {
this.serviceType = serviceType;
}

public String getWeeklyId() { return weeklyId; }

public void setWeeklyId(String weeklyId) { this.weeklyId = weeklyId; }

@Override
public String toString() {

Expand Down
4 changes: 3 additions & 1 deletion app/modules/StartupModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import com.google.inject.AbstractModule;
import controllers.Application;
import controllers.ApplicationComponents.EmailScheduler;
import controllers.ApplicationComponents.Roles;
import controllers.Databases.FirestoreDB;
import controllers.MailerService;
import controllers.ApplicationComponents.MailerService;

public class StartupModule extends AbstractModule {

Expand All @@ -13,6 +14,7 @@ protected void configure() {
bind(FirestoreDB.class).asEagerSingleton();
bind(Application.class).asEagerSingleton();
bind(MailerService.class).asEagerSingleton();
bind(EmailScheduler.class).asEagerSingleton();
/* Define default roles for project */
new Roles("Coach");
new Roles("Admin");
Expand Down
Loading

0 comments on commit a0f7b7a

Please sign in to comment.