Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions api/dispute/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,37 @@ func CreateDispute(c *gin.Context) {
return
}

disputeCount, err := q.CheckDisputeExistsByTxnIdQuery(ctx, tx, txnId)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dispute check must happen before we grabeventIdByTxnId

if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Oops! Something happened. Please try again later",
})
pkg.Log.ErrorCtx(c, "[DISPUTE-ERROR]: Failed to check if dispute exists by transaction ID", err)
return
}
if disputeCount > 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't send bad request ? Send conflict ? Will it cause issue with the frontend team ?

c.JSON(http.StatusBadRequest, gin.H{
"message": "Dispute already exists for this transaction",
})
pkg.Log.ErrorCtx(c, "[DISPUTE-ERROR]: Attempted to create duplicate dispute for transaction ID", nil)
return
}

studentEmail, err := q.GetEmailByTxnIdQuery(ctx, tx, txnId)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once the dispute check is pushed to the top, the dispute creation flow can happen in a single shot with a sub-query that joins on student table, event table using transaction id and then runs the insert. No need to use separate queries.

if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Oops! Something happened. Please try again later",
})
pkg.Log.ErrorCtx(c, "[DISPUTE-ERROR]: Failed to fetch student email by transaction ID", err)
return
}

if event.EventStatus == "ACTIVE" {

err = q.CreateDisputeQuery(ctx, tx, db.CreateDisputeQueryParams{
EventID: event.EventID,
TxnID: txnId,
EventID: event.EventID,
TxnID: txnId,
StudentEmail: pkg.ToPgText(studentEmail),
})
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
Expand Down Expand Up @@ -136,9 +162,8 @@ func UpdateDispute(c *gin.Context) {
q := db.New()

row, err := q.UpdateDisputeQuery(ctx, conn, db.UpdateDisputeQueryParams{
ID: disputeId,
StudentEmail: pkg.ToPgText(req.StudentEmail),
Description: pkg.ToPgText(req.Description),
ID: disputeId,
Description: pkg.ToPgText(req.Description),
})
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
Expand Down
51 changes: 40 additions & 11 deletions db/gen/dispute-for-admin.sql.go

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

22 changes: 17 additions & 5 deletions db/queries/dispute-for-admin.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,19 @@ SELECT id,
FROM dispute
WHERE id = $1;

-- name: CheckDisputeExistsByTxnIdQuery :one
SELECT COUNT(*) AS count
FROM dispute
WHERE txn_id = $1
AND dispute_status = 'OPEN';


-- name: CreateDisputeQuery :exec
INSERT INTO dispute (
txn_id,
event_id
) VALUES ($1, $2);
event_id,
student_email
) VALUES ($1, $2, $3);

-- name: GetEventIdByTxnIdQuery :one
SELECT
Expand Down Expand Up @@ -49,8 +57,7 @@ WHERE id = $1;

-- name: UpdateDisputeQuery :execrows
UPDATE dispute
SET student_email = $2,
description = $3,
SET description = $2,
updated_at = NOW()
WHERE id = $1;

Expand All @@ -66,4 +73,9 @@ SET dispute_status = 'CLOSED_AS_FALSE',
updated_at = NOW()
WHERE id = $1;


-- name: GetEmailByTxnIdQuery :one
SELECT s.email
FROM student s
INNER JOIN bookings b
ON s.id = b.student_id
WHERE b.txn_id = $1;
5 changes: 1 addition & 4 deletions models/dispute.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@ package models

import (
v "github.com/go-ozzo/ozzo-validation/v4"
"github.com/go-ozzo/ozzo-validation/v4/is"
)

type UpdateDisputeStatusInput struct {
StudentEmail string `json:"student_email"`
Description string `json:"description"`
Description string `json:"description"`
}

func (u UpdateDisputeStatusInput) Validate() error {
if err := v.ValidateStruct(&u,
v.Field(&u.StudentEmail, is.Email),
v.Field(&u.Description),
); err != nil {
return err
Expand Down