Skip to content

Commit

Permalink
Merge pull request #42 from kimanimichael/fix/handle_possible_duplicates
Browse files Browse the repository at this point in the history
Handle possible duplicates
  • Loading branch information
kimanimichael authored Sep 19, 2024
2 parents a1f80a0 + d356fb1 commit 4f7c605
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 7 deletions.
33 changes: 33 additions & 0 deletions handler_payments.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const MinCashPaid = 1000
// MaxCashPaid prevents entry errors e.g. 400000 entry instead of 40000
const MaxCashPaid = 100000

const IdenticalTransactionInterval = 2 * time.Minute

func (apiCfg *apiConfig) handlerCreatePayment(w http.ResponseWriter, r *http.Request, user database.User) {
type parameters struct {
Cash int32 `json:"cash_paid"`
Expand Down Expand Up @@ -56,6 +58,37 @@ func (apiCfg *apiConfig) handlerCreatePayment(w http.ResponseWriter, r *http.Req
return
}

mostRecentPayment, err := apiCfg.DB.GetMostRecentPayment(r.Context())
if err != nil {
respondWithError(w, 404, fmt.Sprintf("Couldn't find most recent payment: %v", err))
}

currentTime := time.Now()
//fix stored time to EAT
correctedRecentPaymentTime := time.Date(
mostRecentPayment.CreatedAt.Year(),
mostRecentPayment.CreatedAt.Month(),
mostRecentPayment.CreatedAt.Day(),
mostRecentPayment.CreatedAt.Hour(),
mostRecentPayment.CreatedAt.Minute(),
mostRecentPayment.CreatedAt.Second(),
mostRecentPayment.CreatedAt.Nanosecond(),
time.FixedZone("EAT", 3*60*60),
)
durationSinceLastPayment := currentTime.Sub(correctedRecentPaymentTime)

if mostRecentPayment.FarmerID == farmer.ID {
if mostRecentPayment.CashPaid == params.Cash {
if mostRecentPayment.PricePerChickenPaid == params.PricePerChicken {
if durationSinceLastPayment < IdenticalTransactionInterval {
fmt.Printf("Identical transactions in less than %ds attempted", int(IdenticalTransactionInterval.Seconds()))
respondWithError(w, 404, fmt.Sprintf("Identical transaction made for Farmer %s. Wait for %d seconds", farmer.Name, int(IdenticalTransactionInterval.Seconds()-durationSinceLastPayment.Seconds())))
return
}
}
}
}

payment, err := apiCfg.DB.CreatePayment(r.Context(), database.CreatePaymentParams{
ID: uuid.New(),
CreatedAt: time.Now(),
Expand Down
28 changes: 28 additions & 0 deletions handler_purchases.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,34 @@ func (apiCfg *apiConfig) handerCreatePurchases(w http.ResponseWriter, r *http.Re
respondWithError(w, 500, fmt.Sprintf("Error getting farmer: %v", err))
return
}
mostRecentPurchase, err := apiCfg.DB.GetMostRecentPurchase(r.Context())
if err != nil {
respondWithError(w, 500, fmt.Sprintf("Error getting most recent purchase: %v", err))
}
currentTime := time.Now()
correctedRecentPurchaseTime := time.Date(
mostRecentPurchase.CreatedAt.Year(),
mostRecentPurchase.CreatedAt.Month(),
mostRecentPurchase.CreatedAt.Day(),
mostRecentPurchase.CreatedAt.Hour(),
mostRecentPurchase.CreatedAt.Minute(),
mostRecentPurchase.CreatedAt.Second(),
mostRecentPurchase.CreatedAt.Nanosecond(),
time.FixedZone("EAT", 3*60*60),
)
durationSinceLastPayment := currentTime.Sub(correctedRecentPurchaseTime)

if mostRecentPurchase.FarmerID == farmer.ID {
if mostRecentPurchase.Chicken == params.Chicken {
if mostRecentPurchase.PricePerChicken == params.Price {
if durationSinceLastPayment < IdenticalTransactionInterval {
fmt.Printf("Identical transactions in less than %ds attempted", int(IdenticalTransactionInterval.Seconds()))
respondWithError(w, 404, fmt.Sprintf("Similar transaction for %s. Wait for %ds", farmer.Name, int(IdenticalTransactionInterval.Seconds()-durationSinceLastPayment.Seconds())))
return
}
}
}
}

purchase, err := apiCfg.DB.CreatePurchase(r.Context(), database.CreatePurchaseParams{
ID: uuid.New(),
Expand Down
2 changes: 1 addition & 1 deletion internal/database/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/database/farmers.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/database/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 22 additions & 1 deletion internal/database/payments.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 22 additions & 1 deletion internal/database/purchases.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/database/users.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion sql/queries/payments.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ SELECT * FROM payments
WHERE id = $1;

-- name: DeletePayments :exec
DELETE FROM payments WHERE id = $1;
DELETE FROM payments WHERE id = $1;

-- name: GetMostRecentPayment :one
SELECT * FROM payments
ORDER BY created_at DESC
LIMIT 1;
5 changes: 5 additions & 0 deletions sql/queries/purchases.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ WHERE id = $1;

DELETE FROM purchases WHERE id = $1;

-- name: GetMostRecentPurchase :one
SELECT * FROM purchases
ORDER BY created_at DESC
LIMIT 1;

0 comments on commit 4f7c605

Please sign in to comment.