diff --git a/dto/userpin.go b/dto/userpin.go index 772eddb..f7b1b47 100644 --- a/dto/userpin.go +++ b/dto/userpin.go @@ -6,6 +6,9 @@ import ( "github.com/go-playground/validator/v10" ) +type PinResponse struct { + CreatedAt string `json:"createdAt"` +} type PinInput struct { Pin string `json:"pin" validate:"required,len=6,numeric"` } @@ -41,4 +44,4 @@ func (p *PinUpdateInput) ValidateUpdate() error { } } return nil -} \ No newline at end of file +} diff --git a/internal/controllers/userpin.go b/internal/controllers/userpin.go index e48c020..48612d1 100644 --- a/internal/controllers/userpin.go +++ b/internal/controllers/userpin.go @@ -45,10 +45,13 @@ func CreatePin(c *fiber.Ctx) error { )) } - pinResponse := map[string]interface{}{ - "id": pin.ID, - "createdAt": pin.CreatedAt, - "updatedAt": pin.UpdatedAt, + formattedCreatedAt := utils.FormatDateToIndonesianFormat(pin.CreatedAt) + // formattedUpdatedAt := utils.FormatDateToIndonesianFormat(pin.UpdatedAt) + + pinResponse := dto.PinResponse{ + // ID: pin.ID, + CreatedAt: formattedCreatedAt, + // UpdatedAt: formattedUpdatedAt, } return c.Status(fiber.StatusOK).JSON(utils.FormatResponse( @@ -81,10 +84,17 @@ func GetPin(c *fiber.Ctx) error { isPinValid := services.CheckPin(pin.Pin, input.Pin) if isPinValid { + + formattedCreatedAt := utils.FormatDateToIndonesianFormat(pin.CreatedAt) + formattedUpdatedAt := utils.FormatDateToIndonesianFormat(pin.UpdatedAt) + return c.Status(fiber.StatusOK).JSON(utils.FormatResponse( fiber.StatusOK, "PIN benar", - true, + map[string]interface{}{ + "createdAt": formattedCreatedAt, + "updatedAt": formattedUpdatedAt, + }, )) } @@ -117,6 +127,15 @@ func UpdatePin(c *fiber.Ctx) error { updatedPin, err := services.UpdatePin(userID, input.OldPin, input.NewPin) if err != nil { + + if err.Error() == "PIN lama salah" { + return c.Status(fiber.StatusUnauthorized).JSON(utils.FormatResponse( + fiber.StatusUnauthorized, + "PIN lama salah", + nil, + )) + } + return c.Status(fiber.StatusInternalServerError).JSON(utils.FormatResponse( fiber.StatusInternalServerError, "Failed to update PIN", @@ -124,9 +143,14 @@ func UpdatePin(c *fiber.Ctx) error { )) } + formattedUpdatedAt := utils.FormatDateToIndonesianFormat(updatedPin.UpdatedAt) + return c.Status(fiber.StatusOK).JSON(utils.FormatResponse( fiber.StatusOK, "PIN updated successfully", - updatedPin, + map[string]interface{}{ + "id": updatedPin.ID, + "updatedAt": formattedUpdatedAt, + }, )) } diff --git a/internal/repositories/userpin.go b/internal/repositories/userpin.go index f48285a..6f1a344 100644 --- a/internal/repositories/userpin.go +++ b/internal/repositories/userpin.go @@ -45,4 +45,4 @@ func UpdatePin(userID string, newPin string) (domain.UserPin, error) { } return pin, nil -} \ No newline at end of file +} diff --git a/internal/services/userpin.go b/internal/services/userpin.go index 4e9f3f0..464f6cd 100644 --- a/internal/services/userpin.go +++ b/internal/services/userpin.go @@ -38,14 +38,13 @@ func CreatePin(userID string, input dto.PinInput) (domain.UserPin, error) { } func UpdatePin(userID string, oldPin string, newPin string) (domain.UserPin, error) { - pin, err := repositories.GetPinByUserID(userID) if err != nil { return pin, errors.New("PIN tidak ditemukan") } if err := bcrypt.CompareHashAndPassword([]byte(pin.Pin), []byte(oldPin)); err != nil { - return pin, errors.New("PIN lama tidak cocok") + return pin, errors.New("PIN lama salah") } updatedPin, err := repositories.UpdatePin(userID, newPin)