package com.drinkool.services; import com.drinkool.Jwt; import com.drinkool.Role; import com.drinkool.Url; import com.drinkool.dtos.*; import com.drinkool.entities.CustomerEntity; import jakarta.inject.Inject; import jakarta.transaction.Transactional; import jakarta.ws.rs.*; import jakarta.ws.rs.core.*; @Path("") public class AuthenticationService { @Inject OtpService otpService; @POST @Path(Url.Signup) @Transactional public Response signup(Signup input) { CustomerEntity newCustomer = new CustomerEntity(); newCustomer.signup(input.name, input.phone, input.password); return Response.status(Response.Status.CREATED) .header(Jwt.userId, newCustomer.id.toString()) .header(Jwt.role, Role.Customer) .build(); } @POST @Path(Url.QuickSignup) @Transactional public Response quickSignup(QuickSignup input) { CustomerEntity newCustomer = new CustomerEntity(); newCustomer.signup(input.phone); return Response.status(Response.Status.CREATED) .header(Jwt.userId, newCustomer.id.toString()) .header(Jwt.role, Role.Customer) .build(); } @POST @Path(Url.Login) public Response login(Login input) { CustomerEntity customer = CustomerEntity.find( "phone", input.phone ).firstResult(); if (customer == null) return Response.status(Response.Status.BAD_REQUEST) .entity("InvalidPhoneNumber") .build(); if (!customer.login(input.password)) return Response.status( Response.Status.BAD_REQUEST ) .entity("InvalidPassword") .build(); return Response.accepted() .header(Jwt.userId, customer.id.toString()) .header(Jwt.role, Role.Customer) .build(); } @POST @Path(Url.QuickLogin) public Response quickLogin(QuickLogin input) { if (!otpService.verifyOtp(input.phone, input.otp)) { return Response.status(Response.Status.UNAUTHORIZED) .entity("InvalidOTP") .build(); } CustomerEntity customer = CustomerEntity.find( "phone", input.phone ).firstResult(); return Response.accepted() .header(Jwt.userId, customer.id.toString()) .header(Jwt.role, Role.Customer) .build(); } @POST @Path(Url.SmsOtp) public Response smsOtp(SmsOtp input) { CustomerEntity customer = CustomerEntity.find( "phone", input.phone ).firstResult(); if (customer == null) return Response.status(Response.Status.BAD_REQUEST) .entity("InvalidPhoneNumber") .build(); String otp = otpService.generateAndSaveOtp(input.phone); // Ở đây bạn sẽ gọi Kafka để gửi SMS thực tế System.out.println("OTP cho " + input.phone + " là: " + otp); return Response.accepted().build(); } }