Skip to content

Commit 84f175e

Browse files
committed
feat: add custom duration subscription endpoint
1 parent aa98727 commit 84f175e

2 files changed

Lines changed: 63 additions & 1 deletion

File tree

api/v1/client/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ func ClientDelete() {
434434

435435
func AutoClientDelete() {
436436
// Run the function once at startup if needed
437-
ClientDelete()
437+
// ClientDelete()
438438

439439
// Set up a ticker to run every hour
440440
ticker := time.NewTicker(1 * time.Hour)

api/v1/subscription/subscription.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func ApplyRoutes(r *gin.RouterGroup) {
3434
g.POST("/trial", TrialSubscription)
3535
g.POST("/create-payment", CreatePaymentIntent)
3636
g.GET("", CheckSubscription)
37+
g.POST("/custom_duration", SubscriptionForCustomDuration)
3738

3839
}
3940
}
@@ -194,3 +195,64 @@ func HandleCanceledOrFailedPaymentIntent(eventDataRaw json.RawMessage) error {
194195

195196
return nil
196197
}
198+
199+
func SubscriptionForCustomDuration(c *gin.Context) {
200+
201+
var (
202+
oneMonth = time.Now().AddDate(0, 1, 0)
203+
threeMonth = time.Now().AddDate(0, 3, 0)
204+
oneYear = time.Now().AddDate(1, 0, 0)
205+
)
206+
207+
// Map duration type to end time
208+
209+
var request struct {
210+
DurationType int `json:"duration_type"`
211+
}
212+
err := c.BindJSON(&request)
213+
if err != nil {
214+
httpo.NewErrorResponse(http.StatusBadRequest, fmt.Sprintf("payload is invalid %s", err)).SendD(c)
215+
return
216+
}
217+
218+
var endTime time.Time
219+
switch request.DurationType {
220+
case 1:
221+
endTime = oneMonth
222+
case 2:
223+
endTime = threeMonth
224+
case 3:
225+
endTime = oneYear
226+
default:
227+
httpo.NewErrorResponse(http.StatusBadRequest, "invalid duration type").SendD(c)
228+
return
229+
}
230+
231+
userId := c.GetString(paseto.CTX_USER_ID)
232+
233+
// Check if there is already an active trial subscription for the user
234+
var existingSubscription models.Subscription
235+
db := dbconfig.GetDb()
236+
if err := db.Where("user_id = ? AND type = ? AND end_time > ?", userId, "trial", time.Now()).First(&existingSubscription).Error; err == nil {
237+
// There is already an active trial subscription for the user
238+
c.JSON(http.StatusBadRequest, gin.H{"error": "You already have an active trial subscription"})
239+
return
240+
}
241+
242+
// Create a new trial subscription
243+
subscription := models.Subscription{
244+
UserId: userId,
245+
StartTime: time.Now(),
246+
EndTime: endTime,
247+
Type: "TrialSubscription",
248+
}
249+
250+
// Save the new trial subscription to the database
251+
if err := db.Model(models.Subscription{}).Create(&subscription).Error; err != nil {
252+
logwrapper.Errorf("Error creating subscription: %v", err)
253+
c.Status(http.StatusInternalServerError)
254+
return
255+
}
256+
257+
c.JSON(http.StatusOK, gin.H{"status": "subscription created"})
258+
}

0 commit comments

Comments
 (0)