repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

commit
8547f54
parent
7e8e16c
author
Eric Bower
date
2024-03-23 21:26:49 +0000 UTC
chore: add pico+ to latest expiration date instead of now

When a user eventually purchases another year of pico+, we don't want to
set the expiration date a year from purchase date.  Rather, we want to
set the expiration date to the expiration date of the feature + 1
year.
1 files changed,  +28, -9
M db/postgres/storage.go
+28, -9
 1@@ -1225,7 +1225,12 @@ func (me *PsqlDB) FindFeatureForUser(userID string, feature string) (*db.Feature
 2 
 3 func (me *PsqlDB) FindFeaturesForUser(userID string) ([]*db.FeatureFlag, error) {
 4 	var features []*db.FeatureFlag
 5-	query := "SELECT id, user_id, payment_history_id, name, data, created_at, expires_at FROM feature_flags WHERE user_id=$1"
 6+	// https://stackoverflow.com/a/16920077
 7+	query := `SELECT DISTINCT ON (name)
 8+			id, user_id, payment_history_id, name, data, created_at, expires_at
 9+		FROM feature_flags
10+		WHERE user_id=$1
11+		ORDER BY name, expires_at DESC;`
12 	rs, err := me.Db.Query(query, userID)
13 	if err != nil {
14 		return features, err
15@@ -1606,6 +1611,16 @@ func (me *PsqlDB) FindTokensForUser(userID string) ([]*db.Token, error) {
16 	return keys, nil
17 }
18 
19+func (me *PsqlDB) createFeatureExpiresAt(userID, name string) time.Time {
20+	ff, _ := me.FindFeatureForUser(userID, name)
21+	t := time.Now()
22+	extendDate := t.AddDate(1, 0, 0)
23+	if ff != nil {
24+		extendDate = ff.ExpiresAt.AddDate(1, 0, 0)
25+	}
26+	return extendDate
27+}
28+
29 func (me *PsqlDB) AddPicoPlusUser(username, paymentType, txId string) error {
30 	user, err := me.FindUserForName(username)
31 	if err != nil {
32@@ -1638,30 +1653,34 @@ func (me *PsqlDB) AddPicoPlusUser(username, paymentType, txId string) error {
33 		}
34 	}
35 
36+	pgs := me.createFeatureExpiresAt(user.ID, "pgs")
37 	pgsQuery := `INSERT INTO feature_flags (user_id, name, data, expires_at)
38-	VALUES ($1, 'pgs', '{"storage_max":10000000000, "file_max":50000000}'::jsonb, now() + '1 year'::interval);`
39-	_, err = tx.Exec(pgsQuery, user.ID)
40+	VALUES ($1, 'pgs', '{"storage_max":10000000000, "file_max":50000000}'::jsonb, $2);`
41+	_, err = tx.Exec(pgsQuery, user.ID, pgs)
42 	if err != nil {
43 		return err
44 	}
45 
46+	imgs := me.createFeatureExpiresAt(user.ID, "imgs")
47 	imgsQuery := `INSERT INTO feature_flags (user_id, name, data, expires_at)
48-	VALUES ($1, 'imgs', '{"storage_max":5000000000}'::jsonb, now() + '1 year'::interval);`
49-	_, err = tx.Exec(imgsQuery, user.ID)
50+	VALUES ($1, 'imgs', '{"storage_max":5000000000}'::jsonb, $2);`
51+	_, err = tx.Exec(imgsQuery, user.ID, imgs)
52 	if err != nil {
53 		return err
54 	}
55 
56+	prose := me.createFeatureExpiresAt(user.ID, "prose")
57 	proseQuery := `INSERT INTO feature_flags (user_id, name, data, expires_at)
58-	VALUES ($1, 'prose', '{"storage_max":1000000000, "file_max":50000000}'::jsonb, now() + '1 year'::interval);`
59-	_, err = tx.Exec(proseQuery, user.ID)
60+	VALUES ($1, 'prose', '{"storage_max":1000000000, "file_max":50000000}'::jsonb, $2);`
61+	_, err = tx.Exec(proseQuery, user.ID, prose)
62 	if err != nil {
63 		return err
64 	}
65 
66+	tuns := me.createFeatureExpiresAt(user.ID, "tuns")
67 	tunsQuery := `INSERT INTO feature_flags (user_id, name, expires_at)
68-	VALUES ($1, 'tuns', now() + '1 year'::interval);`
69-	_, err = tx.Exec(tunsQuery, user.ID)
70+	VALUES ($1, 'tuns', $2);`
71+	_, err = tx.Exec(tunsQuery, user.ID, tuns)
72 	if err != nil {
73 		return err
74 	}