diff --git a/src/main/java/com/safeqr/app/qrcode/entity/PhoneEntity.java b/src/main/java/com/safeqr/app/qrcode/entity/PhoneEntity.java index 863f24b..d240895 100644 --- a/src/main/java/com/safeqr/app/qrcode/entity/PhoneEntity.java +++ b/src/main/java/com/safeqr/app/qrcode/entity/PhoneEntity.java @@ -2,6 +2,7 @@ package com.safeqr.app.qrcode.entity; import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; import jakarta.persistence.*; import lombok.AllArgsConstructor; import lombok.Data; @@ -30,4 +31,8 @@ public class PhoneEntity { private UUID qrCodeId; private String phone; + + @JsonInclude(JsonInclude.Include.NON_EMPTY) + @Column(name = "remarks") + private String remarks; } diff --git a/src/main/java/com/safeqr/app/qrcode/model/PhoneModel.java b/src/main/java/com/safeqr/app/qrcode/model/PhoneModel.java index 9d59c50..c939703 100644 --- a/src/main/java/com/safeqr/app/qrcode/model/PhoneModel.java +++ b/src/main/java/com/safeqr/app/qrcode/model/PhoneModel.java @@ -38,6 +38,6 @@ public final class PhoneModel extends QRCodeModel { } @Override public String retrieveClassification() { - return ""; + return phoneVerificationService.checkPhoneNumber(details); } } \ No newline at end of file diff --git a/src/main/java/com/safeqr/app/qrcode/service/PhoneVerificationService.java b/src/main/java/com/safeqr/app/qrcode/service/PhoneVerificationService.java index e20fa59..5d28967 100644 --- a/src/main/java/com/safeqr/app/qrcode/service/PhoneVerificationService.java +++ b/src/main/java/com/safeqr/app/qrcode/service/PhoneVerificationService.java @@ -11,6 +11,8 @@ import org.springframework.stereotype.Service; import java.util.UUID; +import static com.safeqr.app.constants.CommonConstants.*; + @Service public class PhoneVerificationService { private final PhoneRepository phoneRepository; @@ -48,4 +50,30 @@ public class PhoneVerificationService { } + public String checkPhoneNumber(PhoneEntity phoneEntity) { + // Remove any spaces, dashes, parentheses, and trim the ends + String phoneNumber = phoneEntity.getPhone().replaceAll("[\\s\\-()]", "").trim(); + + // Check if the number starts with +65 or just 65 + if (phoneNumber.startsWith("+65")) { + phoneNumber = phoneNumber.substring(3); // Remove the "+65" + } else if (phoneNumber.startsWith("65")) { + phoneNumber = phoneNumber.substring(2); // Remove the "65" + } + + // Check if it's a valid Singapore mobile or landline number + if (phoneNumber.matches("^[689]\\d{7}$")) { + if (phoneNumber.startsWith("8") || phoneNumber.startsWith("9")) { + phoneEntity.setRemarks("Singapore mobile number - This number has not been scanned for scam. Please do not divulge your personal information."); + } else if (phoneNumber.startsWith("6")) { + phoneEntity.setRemarks("Singapore landline number - This phone number has not been scanned for scam. Please do not divulge your personal information."); + } + return CLASSIFY_UNKNOWN; + } + + // If it doesn't match mobile or landline pattern + phoneEntity.setRemarks("Warning: This is either an overseas number or an invalid Singapore number. Please exercise caution."); + return CLASSIFY_WARNING; + } + } \ No newline at end of file diff --git a/src/main/java/com/safeqr/app/qrcode/service/SMSVerificationService.java b/src/main/java/com/safeqr/app/qrcode/service/SMSVerificationService.java index 3e72015..606f671 100644 --- a/src/main/java/com/safeqr/app/qrcode/service/SMSVerificationService.java +++ b/src/main/java/com/safeqr/app/qrcode/service/SMSVerificationService.java @@ -2,6 +2,7 @@ package com.safeqr.app.qrcode.service; import com.safeqr.app.exceptions.InvalidFormatExceptions; import com.safeqr.app.exceptions.ResourceNotFoundExceptions; +import com.safeqr.app.qrcode.entity.PhoneEntity; import com.safeqr.app.qrcode.entity.SMSEntity; import com.safeqr.app.qrcode.repository.SMSRepository; import org.slf4j.Logger; @@ -12,8 +13,7 @@ import org.springframework.transaction.annotation.Transactional; import java.util.*; -import static com.safeqr.app.constants.CommonConstants.CLASSIFY_SAFE; -import static com.safeqr.app.constants.CommonConstants.CLASSIFY_WARNING; +import static com.safeqr.app.constants.CommonConstants.*; @Service public class SMSVerificationService { @@ -84,7 +84,6 @@ public class SMSVerificationService { throw new InvalidFormatExceptions("Invalid SMSTO format. Expected format: SMSTO::"); } } - @Transactional public String getClassification (SMSEntity smsEntity) { String lowerCaseSms = smsEntity.getMessage().toLowerCase(); @@ -105,8 +104,28 @@ public class SMSVerificationService { } } - // If no phishing keywords are found - return CLASSIFY_SAFE; + // If no phishing keywords are found, sent for local phone number checks + return checkPhoneNumber(smsEntity.getPhone()); + } + + private String checkPhoneNumber(String phoneNumber) { + // Remove any spaces, dashes, parentheses, and trim the ends + phoneNumber = phoneNumber.replaceAll("[\\s\\-()]", "").trim(); + + // Check if the number starts with +65 or just 65 + if (phoneNumber.startsWith("+65")) { + phoneNumber = phoneNumber.substring(3); // Remove the "+65" + } else if (phoneNumber.startsWith("65")) { + phoneNumber = phoneNumber.substring(2); // Remove the "65" + } + + // Check if it's a valid Singapore mobile or landline number + if (phoneNumber.matches("^[689]\\d{7}$") && (phoneNumber.startsWith("8") || phoneNumber.startsWith("9"))) { + return CLASSIFY_SAFE; + } + + // If it doesn't match mobile + return CLASSIFY_UNSAFE; } } \ No newline at end of file