Skip to content

Commit

Permalink
Feature: Integrated patient appointment.
Browse files Browse the repository at this point in the history
  • Loading branch information
elwyncrestha committed Jul 10, 2020
1 parent 4569854 commit 03afa4c
Show file tree
Hide file tree
Showing 12 changed files with 341 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
import org.springframework.stereotype.Service;

import com.pemits.webcare.api.appointment.entity.Appointment;
import com.pemits.webcare.api.appointment.repository.AppointmentRepository;
import com.pemits.webcare.api.appointment.repository.spec.AppointmentSpecBuilder;
import com.pemits.webcare.core.repository.BaseRepository;
import com.pemits.webcare.api.patient.entity.Patient;
import com.pemits.webcare.api.patient.service.PatientService;
import com.pemits.webcare.api.user.entity.User;
import com.pemits.webcare.api.user.service.UserService;
import com.pemits.webcare.core.enums.UserType;
import com.pemits.webcare.core.repository.BaseSpecBuilder;
import com.pemits.webcare.core.service.BaseServiceImpl;

Expand All @@ -18,8 +23,35 @@
public class AppointmentServiceImpl extends BaseServiceImpl<Appointment, Long> implements
AppointmentService {

public AppointmentServiceImpl(BaseRepository<Appointment, Long> repository) {
private final AppointmentRepository repository;
private final UserService userService;
private final PatientService patientService;

public AppointmentServiceImpl(
AppointmentRepository repository,
UserService userService,
PatientService patientService) {
super(repository);

this.repository = repository;
this.userService = userService;
this.patientService = patientService;
}

@Override
public Appointment save(Appointment appointment) {
Patient patient = appointment.getPatient();
if (null == patient.getId()) {
User user = patient.getUser();
user.setUserType(UserType.PATIENT);
user.setUsername(String.valueOf(System.currentTimeMillis()));
User savedUser = userService.save(user);
Patient newPatient = patientService.findByUserId(savedUser.getId());
newPatient.setAge(patient.getAge());
Patient savedPatient = patientService.save(newPatient);
appointment.setPatient(savedPatient);
}
return repository.save(appointment);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import lombok.NoArgsConstructor;

import com.pemits.webcare.api.user.entity.User;
import com.pemits.webcare.core.entity.AppUser;
import com.pemits.webcare.core.entity.BaseEntity;
import com.pemits.webcare.core.enums.UserType;

/**
* @author Elvin Shrestha on 7/5/2020
Expand All @@ -20,11 +22,16 @@
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class Patient extends BaseEntity<Long> {
public class Patient extends BaseEntity<Long> implements AppUser {

@OneToOne
private User user;

@NotEmpty(message = "Age is required")
private int age;

@Override
public UserType getUserType() {
return UserType.PATIENT;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.stereotype.Repository;

import com.pemits.webcare.api.patient.entity.Patient;
import com.pemits.webcare.api.user.entity.User;
import com.pemits.webcare.core.repository.BaseRepository;

/**
Expand All @@ -11,4 +12,5 @@
@Repository
public interface PatientRepository extends BaseRepository<Patient, Long> {

Patient findPatientByUser(User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
*/
public interface PatientService extends BaseService<Patient, Long> {

Patient findByUserId(long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.pemits.webcare.api.patient.entity.Patient;
import com.pemits.webcare.api.patient.repository.PatientRepository;
import com.pemits.webcare.api.patient.repository.spec.PatientSpecBuilder;
import com.pemits.webcare.api.user.entity.User;
import com.pemits.webcare.core.repository.BaseSpecBuilder;
import com.pemits.webcare.core.service.BaseServiceImpl;

Expand All @@ -29,4 +30,11 @@ protected PatientServiceImpl(
protected BaseSpecBuilder<Patient> getSpec(Map<String, String> filterParams) {
return new PatientSpecBuilder(filterParams);
}

@Override
public Patient findByUserId(long userId) {
User user = new User();
user.setId(userId);
return repository.findPatientByUser(user);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@

import com.pemits.webcare.api.doctor.entity.Doctor;
import com.pemits.webcare.api.doctor.service.DoctorService;
import com.pemits.webcare.api.patient.entity.Patient;
import com.pemits.webcare.api.patient.service.PatientService;
import com.pemits.webcare.api.user.entity.User;
import com.pemits.webcare.api.user.repository.UserRepository;
import com.pemits.webcare.api.user.repository.spec.UserSpecBuilder;
import com.pemits.webcare.core.constant.EmailConstant.Template;
import com.pemits.webcare.core.dto.EmailDto;
import com.pemits.webcare.core.entity.AppUser;
import com.pemits.webcare.core.enums.Status;
import com.pemits.webcare.core.enums.UserType;
import com.pemits.webcare.core.repository.BaseSpecBuilder;
import com.pemits.webcare.core.service.BaseServiceImpl;
import com.pemits.webcare.core.service.EmailService;
Expand All @@ -38,17 +41,20 @@ public class UserServiceImpl extends BaseServiceImpl<User, Long> implements User
private final PasswordEncoder passwordEncoder;
private final EmailService emailService;
private final DoctorService doctorService;
private final PatientService patientService;

protected UserServiceImpl(
UserRepository repository,
PasswordEncoder passwordEncoder,
EmailService emailService,
DoctorService doctorService) {
DoctorService doctorService,
PatientService patientService) {
super(repository);
this.repository = repository;
this.passwordEncoder = passwordEncoder;
this.emailService = emailService;
this.doctorService = doctorService;
this.patientService = patientService;
}

@Transactional
Expand All @@ -74,25 +80,37 @@ public User save(User user) {

// in case of new user
if (password != null) {
// create associate entity
AppUser appUser = null;
switch (saved.getUserType()) {
case DOCTOR:
Doctor doctor = new Doctor();
doctor.setUser(saved);
appUser = saveAppUser(doctor);
break;
case PATIENT:
Patient patient = new Patient();
patient.setUser(saved);
appUser = saveAppUser(patient);
break;
}

// send email
EmailDto emailDto = EmailDto.builder()
.template(Template.REGISTRATION_CREDENTIALS)
.to(user.getEmail())
.toName(user.getName())
.username(user.getUsername())
.password(password)
.webUrl(webUrl)
.build();
emailService.send(emailDto);

// create associate entity
switch (saved.getUserType()) {
case DOCTOR:
Doctor doctor = new Doctor();
doctor.setUser(saved);
saveAppUser(doctor);
break;
if (saved.getUserType().equals(UserType.PATIENT)) {
emailDto.setPatientId(String.valueOf(((Patient) appUser).getId()));
emailDto.setTemplate(Template.PATIENT_REGISTRATION);
} else {
emailDto.setTemplate(Template.REGISTRATION_CREDENTIALS);
}
emailService.send(emailDto);
}

return saved;
Expand Down Expand Up @@ -129,6 +147,9 @@ public AppUser saveAppUser(AppUser appUser) {
case DOCTOR:
savedAppUser = doctorService.save((Doctor) appUser);
break;
case PATIENT:
savedAppUser = patientService.save((Patient) appUser);
break;
default:
savedAppUser = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
public final class EmailConstant {

public enum Template {
REGISTRATION_CREDENTIALS("Registration Credentials!!!");
REGISTRATION_CREDENTIALS("Registration Credentials!!!"),
PATIENT_REGISTRATION("Patient Registration Credentials!!!");

private String subject;
private final String subject;

Template(String subject) {
this.subject = subject;
Expand All @@ -25,6 +26,7 @@ public String get() {

public static final Map<Template, String> MAIL = ImmutableMap.<Template, String>builder()
.put(Template.REGISTRATION_CREDENTIALS, "/mail/registration-credentials")
.put(Template.PATIENT_REGISTRATION, "/mail/patient-registration")
.build();

private EmailConstant() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class EmailDto {
private String password;
private String username;
private String webUrl;
private String patientId;

private Template template;
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public void configure(HttpSecurity http) throws Exception {
.permitAll()
.antMatchers(HttpMethod.POST, "/v1/doctors/list/all")
.permitAll()
.antMatchers(HttpMethod.GET, "/v1/patient/*")
.permitAll()
.antMatchers(HttpMethod.POST, "/v1/patient")
.anonymous()
.antMatchers("/v1/**")
.authenticated()
.and()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.pemits.webcare.web.patient;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.pemits.webcare.api.patient.entity.Patient;
import com.pemits.webcare.api.patient.service.PatientService;
import com.pemits.webcare.core.controller.BaseController;

/**
* @author Elvin Shrestha on 7/10/2020
*/
@RestController
@RequestMapping(PatientController.URL)
@Slf4j
public class PatientController extends BaseController<Patient, Long> {

static final String URL = "/v1/patient";

protected PatientController(
PatientService service) {
super(service, log.getClass());
}
}
69 changes: 69 additions & 0 deletions pemits-web/src/main/resources/db/changelog/changelog-master.xml
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,73 @@
</createTable>
</changeSet>

<changeSet id="8-create-doctor" author="Elvin Shrestha">
<preConditions onFail="MARK_RAN">
<not>
<tableExists tableName="patient"/>
</not>
</preConditions>

<createTable tableName="patient">
<column name="id" type="BIGINT" autoIncrement="true">
<constraints nullable="false" primaryKey="true" primaryKeyName="PK_PATIENT"/>
</column>
<column name="created_at" type="DATETIME">
<constraints nullable="false"/>
</column>
<column name="last_modified_at" type="DATETIME">
<constraints nullable="false"/>
</column>
<column name="created_by" type="BIGINT"/>
<column name="last_modified_by" type="BIGINT"/>
<column name="version" type="INT"/>
<column name="user_id" type="BIGINT">
<constraints foreignKeyName="FK_PATIENT_USER_ID_USER_ID"
referencedTableName="users"
referencedColumnNames="id"/>
</column>
<column name="age" type="INT"/>
</createTable>
</changeSet>

<changeSet id="8-create-appointment" author="Elvin Shrestha">
<preConditions onFail="MARK_RAN">
<not>
<tableExists tableName="appointment"/>
</not>
</preConditions>

<createTable tableName="appointment">
<column name="id" type="BIGINT" autoIncrement="true">
<constraints nullable="false" primaryKey="true" primaryKeyName="PK_APPOINTMENT"/>
</column>
<column name="created_at" type="DATETIME">
<constraints nullable="false"/>
</column>
<column name="last_modified_at" type="DATETIME">
<constraints nullable="false"/>
</column>
<column name="created_by" type="BIGINT"/>
<column name="last_modified_by" type="BIGINT"/>
<column name="version" type="INT"/>
<column name="patient_id" type="BIGINT">
<constraints foreignKeyName="FK_APPOINTMENT_PATIENT_ID_PATIENT_ID"
referencedTableName="patient"
referencedColumnNames="id"/>
</column>
<column name="department_id" type="BIGINT">
<constraints foreignKeyName="FK_APPOINTMENT_DEPARTMENT_ID_DEPARTMENT_ID"
referencedTableName="department"
referencedColumnNames="id"/>
</column>
<column name="doctor_id" type="BIGINT">
<constraints foreignKeyName="FK_APPOINTMENT_DOCTOR_ID_DOCTOR_ID"
referencedTableName="doctor"
referencedColumnNames="id"/>
</column>
<column name="appointment_date" type="DATE"/>
<column name="appointment_time" type="TIME"/>
</createTable>
</changeSet>

</databaseChangeLog>
Loading

0 comments on commit 03afa4c

Please sign in to comment.