fix: fix user pin response structure
This commit is contained in:
parent
96f85fce0b
commit
8945080477
|
@ -6,6 +6,9 @@ import (
|
||||||
"github.com/go-playground/validator/v10"
|
"github.com/go-playground/validator/v10"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type PinResponse struct {
|
||||||
|
CreatedAt string `json:"createdAt"`
|
||||||
|
}
|
||||||
type PinInput struct {
|
type PinInput struct {
|
||||||
Pin string `json:"pin" validate:"required,len=6,numeric"`
|
Pin string `json:"pin" validate:"required,len=6,numeric"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,10 +45,13 @@ func CreatePin(c *fiber.Ctx) error {
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
pinResponse := map[string]interface{}{
|
formattedCreatedAt := utils.FormatDateToIndonesianFormat(pin.CreatedAt)
|
||||||
"id": pin.ID,
|
// formattedUpdatedAt := utils.FormatDateToIndonesianFormat(pin.UpdatedAt)
|
||||||
"createdAt": pin.CreatedAt,
|
|
||||||
"updatedAt": pin.UpdatedAt,
|
pinResponse := dto.PinResponse{
|
||||||
|
// ID: pin.ID,
|
||||||
|
CreatedAt: formattedCreatedAt,
|
||||||
|
// UpdatedAt: formattedUpdatedAt,
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.Status(fiber.StatusOK).JSON(utils.FormatResponse(
|
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)
|
isPinValid := services.CheckPin(pin.Pin, input.Pin)
|
||||||
|
|
||||||
if isPinValid {
|
if isPinValid {
|
||||||
|
|
||||||
|
formattedCreatedAt := utils.FormatDateToIndonesianFormat(pin.CreatedAt)
|
||||||
|
formattedUpdatedAt := utils.FormatDateToIndonesianFormat(pin.UpdatedAt)
|
||||||
|
|
||||||
return c.Status(fiber.StatusOK).JSON(utils.FormatResponse(
|
return c.Status(fiber.StatusOK).JSON(utils.FormatResponse(
|
||||||
fiber.StatusOK,
|
fiber.StatusOK,
|
||||||
"PIN benar",
|
"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)
|
updatedPin, err := services.UpdatePin(userID, input.OldPin, input.NewPin)
|
||||||
if err != nil {
|
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(
|
return c.Status(fiber.StatusInternalServerError).JSON(utils.FormatResponse(
|
||||||
fiber.StatusInternalServerError,
|
fiber.StatusInternalServerError,
|
||||||
"Failed to update PIN",
|
"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(
|
return c.Status(fiber.StatusOK).JSON(utils.FormatResponse(
|
||||||
fiber.StatusOK,
|
fiber.StatusOK,
|
||||||
"PIN updated successfully",
|
"PIN updated successfully",
|
||||||
updatedPin,
|
map[string]interface{}{
|
||||||
|
"id": updatedPin.ID,
|
||||||
|
"updatedAt": formattedUpdatedAt,
|
||||||
|
},
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
|
@ -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) {
|
func UpdatePin(userID string, oldPin string, newPin string) (domain.UserPin, error) {
|
||||||
|
|
||||||
pin, err := repositories.GetPinByUserID(userID)
|
pin, err := repositories.GetPinByUserID(userID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return pin, errors.New("PIN tidak ditemukan")
|
return pin, errors.New("PIN tidak ditemukan")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := bcrypt.CompareHashAndPassword([]byte(pin.Pin), []byte(oldPin)); err != nil {
|
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)
|
updatedPin, err := repositories.UpdatePin(userID, newPin)
|
||||||
|
|
Loading…
Reference in New Issue