diff --git a/backend/internal/handlers/pay_handler.go b/backend/internal/handlers/pay_handler.go index 6084a5f..80c3d93 100644 --- a/backend/internal/handlers/pay_handler.go +++ b/backend/internal/handlers/pay_handler.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "net/http" + "strconv" "strings" "github.com/gin-gonic/gin" @@ -63,16 +64,28 @@ func PostPay(c *gin.Context) { gin.H{"error": "failed to get db client"}) return } + + //parse query.JobOfferID to an int64 + jobOfferID, err := strconv.ParseInt(query.JobOfferID, 10, 64) + if err != nil { + log.Error(err) + c.JSON(http.StatusInternalServerError, + gin.H{"error": "failed to parse JobOfferID"}) + return + } + url := fmt.Sprintf("%s%s-%d", payment.OPEN_COLLECTIVE_CONTRIBUTE, response.CreateTier.Slug, response.CreateTier.LegacyID) err = db.CreatePaymentRecord(&v1beta.PaymentRecord{ - TierId: response.CreateTier.ID, - LegacyId: response.CreateTier.LegacyID, - Slug: response.CreateTier.Slug, - Email: query.Email, - TierUrl: url, + TierId: response.CreateTier.ID, + JobOfferID: jobOfferID, + LegacyId: response.CreateTier.LegacyID, + Slug: response.CreateTier.Slug, + Email: query.Email, + TierUrl: url, }) if err != nil { log.Error(err) + //TODO: if we fail to record the payment, we should delete the tier c.JSON(http.StatusInternalServerError, gin.H{"error": "could not find job titles"}) return diff --git a/backend/migrations/20230521013641_new.create_payment_record_table.sql b/backend/migrations/20230521013641_new.create_payment_record_table.sql index d12045c..8cf71c7 100644 --- a/backend/migrations/20230521013641_new.create_payment_record_table.sql +++ b/backend/migrations/20230521013641_new.create_payment_record_table.sql @@ -3,12 +3,16 @@ CREATE TABLE IF NOT EXISTS payment_records ( id BIGSERIAL NOT NULL PRIMARY KEY, email VARCHAR(255) NOT NULL, + job_offer_id BIGINT NOT NULL, tier_id VARCHAR(255) NOT NULL, legacy_id INTEGER NOT NULL, slug VARCHAR(255) NOT NULL, tier_url VARCHAR(255) NOT NULL, createdat DATE, - updatedat DATE + updatedat DATE, + CONSTRAINT fk_job_offers + FOREIGN KEY(job_offer_id) + REFERENCES job_offers(id) ); -- +goose StatementEnd diff --git a/backend/pkg/models/v1beta/pay.go b/backend/pkg/models/v1beta/pay.go index 23604ce..c824242 100644 --- a/backend/pkg/models/v1beta/pay.go +++ b/backend/pkg/models/v1beta/pay.go @@ -7,8 +7,9 @@ import ( type PayPostQuery struct { // The email of the user making the payment - Email string `json:"email"` - Tier string `json:"tier"` + Email string `json:"email"` + Tier string `json:"tier"` + JobOfferID string `json:"job_offer_id"` } // Validate check if the mandatory fields are filled diff --git a/backend/pkg/models/v1beta/payment_record.go b/backend/pkg/models/v1beta/payment_record.go index d80d2b4..1792e07 100644 --- a/backend/pkg/models/v1beta/payment_record.go +++ b/backend/pkg/models/v1beta/payment_record.go @@ -3,12 +3,13 @@ package v1beta import "time" type PaymentRecord struct { - Id int64 `json:"id" gorm:"column:id;auto_increment;primary_key"` - Email string `json:"email" gorm:"column:email"` - TierId string `json:"tier_id" gorm:"column:tier_id"` - LegacyId int64 `json:"legacy_id" gorm:"column:legacy_id"` - Slug string `json:"slug" gorm:"column:slug"` - TierUrl string `json:"tier_url" gorm:"column:tier_url"` - CreatedAt time.Time `json:"created_at" gorm:"column:createdat"` - UpdatedAt time.Time `json:"updated_at" gorm:"column:updatedat"` + Id int64 `json:"id" gorm:"column:id;auto_increment;primary_key"` + Email string `json:"email" gorm:"column:email"` + TierId string `json:"tier_id" gorm:"column:tier_id"` + JobOfferID int64 `json:"job_offer_id" gorm:"column:job_offer_id"` + LegacyId int64 `json:"legacy_id" gorm:"column:legacy_id"` + Slug string `json:"slug" gorm:"column:slug"` + TierUrl string `json:"tier_url" gorm:"column:tier_url"` + CreatedAt time.Time `json:"created_at" gorm:"column:createdat"` + UpdatedAt time.Time `json:"updated_at" gorm:"column:updatedat"` }