Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NguyenHCP] feat: send mail template #129

Merged
merged 2 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
import com.swp.PodBookingSystem.dto.respone.ApiResponse;
import com.swp.PodBookingSystem.dto.respone.AccountResponse;
import com.swp.PodBookingSystem.dto.respone.Order.OrderManagementResponse;
import com.swp.PodBookingSystem.dto.respone.OrderDetail.OrderDetailFullInfoResponse;
import com.swp.PodBookingSystem.dto.respone.PaginationResponse;
import com.swp.PodBookingSystem.entity.Account;
import com.swp.PodBookingSystem.enums.AccountRole;
import com.swp.PodBookingSystem.exception.AppException;
import com.swp.PodBookingSystem.exception.ErrorCode;
import com.swp.PodBookingSystem.mapper.AccountMapper;
import com.swp.PodBookingSystem.service.AccountService;
import com.swp.PodBookingSystem.service.OrderDetailService;
import com.swp.PodBookingSystem.service.OrderService;
import com.swp.PodBookingSystem.service.SendEmailService;
import jakarta.mail.MessagingException;
Expand Down Expand Up @@ -48,6 +50,7 @@ public class AccountController {
SendEmailService sendEmailService;
JwtDecoder jwtDecoder;
OrderService orderService;
OrderDetailService orderDetailService;

@PostMapping
ApiResponse<AccountResponse> createAccount(@RequestBody @Valid AccountCreationRequest request) {
Expand Down Expand Up @@ -137,6 +140,16 @@ ApiResponse sendEmailOrder(@RequestBody SendMailOrderRequest request) throws Mes
.build();
}

@PostMapping("/send-email-order-amenity")
ApiResponse sendEmailOrderAmenity(@RequestBody SendOrderAmenityRequest request) throws MessagingException, IOException {
OrderDetailFullInfoResponse orderDetail = orderDetailService.getOrderDetailByOrderDetailId(request.getOrderDetailId());
sendEmailService.sendMailAmenityOrder(request.getEmail(), orderDetail, "Hóa đơn tại FlexiPod");
return ApiResponse.builder()
.message("Gửi lời mời đặt lịch thành công")
.code(200)
.build();
}

@GetMapping("/staff")
public ResponseEntity<List<AccountOrderResponse>> getAllStaffAccounts() {
return ResponseEntity.status(HttpStatus.OK).body(accountService.getAllStaffAccounts());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.swp.PodBookingSystem.dto.request.Account;

import lombok.*;
import lombok.experimental.FieldDefaults;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@FieldDefaults(level = AccessLevel.PRIVATE)
public class SendOrderAmenityRequest {
String email;
String orderDetailId;
}
2 changes: 1 addition & 1 deletion src/main/java/com/swp/PodBookingSystem/entity/Room.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Room {
String name;
String description;

@Column(nullable = true)
@Column(nullable = true, length = 1000)
String image;

@Enumerated(EnumType.STRING)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class RoomType {
int price;
int quantity;
int capacity;
@Column(nullable = true, length = 1000)
String image;

@JsonIgnore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import biweekly.ICalendar;
import biweekly.component.VEvent;
import biweekly.property.Method;
import biweekly.property.RecurrenceRule;
import biweekly.util.Duration;
import biweekly.util.Frequency;
import com.swp.PodBookingSystem.dto.request.CalendarRequest;
import com.swp.PodBookingSystem.dto.respone.Order.OrderManagementResponse;
import com.swp.PodBookingSystem.dto.respone.OrderDetail.OrderDetailFullInfoResponse;
import com.swp.PodBookingSystem.entity.OrderDetail;
import jakarta.activation.DataHandler;
import jakarta.activation.DataSource;
Expand All @@ -30,9 +33,6 @@

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -119,6 +119,40 @@ public void sendMailTemplate(String recipient, OrderManagementResponse order, St
log.info("Send email successfully");
}

public void sendMailAmenityOrder(String recipient, OrderDetailFullInfoResponse order, String subject) throws MessagingException, IOException {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
ClassPathResource resource = new ClassPathResource("templates/emailTemplate.html");
String content;
try (var inputStream = resource.getInputStream()) {
content = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
}

var roomHaveAmenities = order.getAmenities().stream().map(amenity -> amenity.getName()).collect(Collectors.toList());

double totalPriceAmenity = order.getAmenities().stream()
.mapToDouble(amenity -> amenity.getPrice() * amenity.getQuantity())
.sum();
double finalPrice = totalPriceAmenity * (1 - order.getServicePackage().getDiscountPercentage() / 100);

int integerAmount = (int) Math.round(finalPrice);
String formattedAmount = String.format("%,d", integerAmount).replace(",", ".");
content = content.replace("{{orderId}}", order.getId())
.replace("{{orderStartTime}}", order.getStartTime().format(DateTimeFormatter.ofPattern("HH:mm:ss dd-MM-yyyy")))
.replace("{{roomName}}", order.getRoomName())
.replace("{{status}}", order.getStatus())
.replace("{{amenity}}", roomHaveAmenities.isEmpty() ? "Không có" : String.join(",", roomHaveAmenities))
.replace("{{totalPrice}}", formattedAmount + " VND");

helper.setFrom(fromEmailId);
helper.setTo(recipient);
helper.setSubject(subject);
helper.setText(content, true);

javaMailSender.send(mimeMessage);
log.info("Send email successfully");
}

public void sendCalenderInvite(CalendarRequest calenderDto) throws IOException, MessagingException {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(calenderDto.getTo()));
Expand Down