Skip to content

Commit

Permalink
refactor: store driver commission, rename into platform fee
Browse files Browse the repository at this point in the history
  • Loading branch information
ermasavior committed Nov 21, 2024
1 parent ef0e1ce commit 429f875
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 22 deletions.
5 changes: 3 additions & 2 deletions internal/rides/model/ride.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ func (r *RideData) SetStatus(statusNum int) {
}

type StoreRideCommissionRequest struct {
RideID int64
Commission float64
RideID int64
PlatformFee float64
DriverCommission float64
}

type UpdateRideDataRequest struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/rides/repository/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (r *ridesRepo) UpdateRideData(ctx context.Context, req model.UpdateRideData

func (r *ridesRepo) StoreRideCommission(ctx context.Context, req model.StoreRideCommissionRequest) error {
values := []interface{}{
req.RideID, req.Commission,
req.RideID, req.PlatformFee, req.DriverCommission,
}
_, err := r.db.ExecContext(ctx, queryInsertRideCommission, values...)
if err != nil {
Expand Down
9 changes: 5 additions & 4 deletions internal/rides/repository/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,15 +545,16 @@ func TestRepository_StoreRideCommission(t *testing.T) {

ctx := context.Background()
req := model.StoreRideCommissionRequest{
RideID: 1111,
Commission: 20000,
RideID: 1111,
PlatformFee: 2000,
DriverCommission: 20000,
}

expectedQuery := queryInsertRideCommission

t.Run("should execute insert query", func(t *testing.T) {
sqlMock.ExpectExec(expectedQuery).
WithArgs(req.RideID, req.Commission).
WithArgs(req.RideID, req.PlatformFee, req.DriverCommission).
WillReturnResult(sqlmock.NewResult(1, 1))

err := repoMock.StoreRideCommission(ctx, req)
Expand All @@ -564,7 +565,7 @@ func TestRepository_StoreRideCommission(t *testing.T) {
t.Run("should return error when error from db", func(t *testing.T) {
rowErr := errors.New("error from db")
sqlMock.ExpectExec(expectedQuery).
WithArgs(req.RideID, req.Commission).
WithArgs(req.RideID, req.PlatformFee, req.DriverCommission).
WillReturnError(rowErr)

err := repoMock.StoreRideCommission(ctx, req)
Expand Down
9 changes: 5 additions & 4 deletions internal/rides/usecase/driver_confirm_payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ func (u *ridesUsecase) DriverConfirmPayment(ctx context.Context, req model.Drive
}

func (u *ridesUsecase) processPayment(ctx context.Context, rideID int64, finalPrice float64, payerMSISDN, payeeMSISDN string) error {
commission := finalPrice * float64(u.RideFeePercentage) / 100
netPrice := finalPrice - commission
platformFee := finalPrice * float64(u.RideFeePercentage) / 100
netPrice := finalPrice - platformFee

err := u.paymentRepo.DeductCredit(ctx, model.DeductCreditRequest{
MSISDN: payerMSISDN,
Expand All @@ -125,8 +125,9 @@ func (u *ridesUsecase) processPayment(ctx context.Context, rideID int64, finalPr
return err
}
err = u.ridesRepo.StoreRideCommission(ctx, model.StoreRideCommissionRequest{
RideID: rideID,
Commission: commission,
RideID: rideID,
DriverCommission: netPrice,
PlatformFee: platformFee,
})
if err != nil {
logger.Error(ctx, "error store ride commission", map[string]interface{}{
Expand Down
26 changes: 15 additions & 11 deletions internal/rides/usecase/driver_confirm_payment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func TestUsecase_DriverConfirmPayment(t *testing.T) {
distance = float64(3)
customPrice = float64(13000)
finalPrice = float64(13000)
commission = customPrice * float64(cfg.RideFeePercentage) / 100
netPrice = customPrice - commission
platformFee = customPrice * float64(cfg.RideFeePercentage) / 100
netPrice = customPrice - platformFee

rideData = model.RideData{
RideID: 111,
Expand Down Expand Up @@ -84,8 +84,9 @@ func TestUsecase_DriverConfirmPayment(t *testing.T) {
Value: netPrice,
}).Return(nil)
ridesRepoMock.EXPECT().StoreRideCommission(ctx, model.StoreRideCommissionRequest{
RideID: req.RideID,
Commission: commission,
RideID: req.RideID,
PlatformFee: platformFee,
DriverCommission: netPrice,
}).Return(nil)

ridesRepoMock.EXPECT().UpdateRideData(ctx, model.UpdateRideDataRequest{
Expand Down Expand Up @@ -184,7 +185,7 @@ func TestUsecase_DriverConfirmPayment(t *testing.T) {
assert.Equal(t, pkgError.ErrInternalErrorCode, err.GetCode())
})

t.Run("failed - failed store commission", func(t *testing.T) {
t.Run("failed - failed store platformFee", func(t *testing.T) {
ridesRepoMock.EXPECT().GetRideData(ctx, req.RideID).Return(rideData, nil)

ridesRepoMock.EXPECT().GetDriverMSISDNByID(ctx, driverID).Return(driverMSISDN, nil)
Expand All @@ -199,8 +200,9 @@ func TestUsecase_DriverConfirmPayment(t *testing.T) {
Value: netPrice,
}).Return(nil)
ridesRepoMock.EXPECT().StoreRideCommission(ctx, model.StoreRideCommissionRequest{
RideID: req.RideID,
Commission: commission,
RideID: req.RideID,
PlatformFee: platformFee,
DriverCommission: netPrice,
}).Return(expectedErr)

_, err := usecaseMock.DriverConfirmPayment(ctx, req)
Expand All @@ -222,8 +224,9 @@ func TestUsecase_DriverConfirmPayment(t *testing.T) {
Value: netPrice,
}).Return(nil)
ridesRepoMock.EXPECT().StoreRideCommission(ctx, model.StoreRideCommissionRequest{
RideID: req.RideID,
Commission: commission,
RideID: req.RideID,
PlatformFee: platformFee,
DriverCommission: netPrice,
}).Return(nil)

ridesRepoMock.EXPECT().UpdateRideData(ctx, model.UpdateRideDataRequest{
Expand Down Expand Up @@ -251,8 +254,9 @@ func TestUsecase_DriverConfirmPayment(t *testing.T) {
Value: netPrice,
}).Return(nil)
ridesRepoMock.EXPECT().StoreRideCommission(ctx, model.StoreRideCommissionRequest{
RideID: req.RideID,
Commission: commission,
RideID: req.RideID,
PlatformFee: platformFee,
DriverCommission: netPrice,
}).Return(nil)

ridesRepoMock.EXPECT().UpdateRideData(ctx, model.UpdateRideDataRequest{
Expand Down

0 comments on commit 429f875

Please sign in to comment.