diff --git a/src/routes/blog.js b/src/routes/blog.js index f0673d7..5319dfd 100644 --- a/src/routes/blog.js +++ b/src/routes/blog.js @@ -77,7 +77,7 @@ export const blogRoutes = (fastify, _, done) => { views: articlesTable.views, is_public: articlesTable.is_public, created_at: articlesTable.created_at, - }).from(articlesTable).where(and(eq(articlesTable.status, "done"), clause)).limit(3).offset(request.query.offset || 0).orderBy(desc(articlesTable.created_at)); + }).from(articlesTable).where(and(eq(articlesTable.status, "done"), clause)).limit(10).offset(request.query.offset || 0).orderBy(desc(articlesTable.created_at)); const [{ total }] = await db.select({ total: sql`COUNT(*)` diff --git a/src/routes/me.js b/src/routes/me.js index 8712ceb..41ccb76 100644 --- a/src/routes/me.js +++ b/src/routes/me.js @@ -30,7 +30,8 @@ export const meRoutes = (fastify, _, done) => { response.send({ success: true, - user: user[0] + user: user[0], + is_subscribed: new Date(user[0].subscribed_until) > new Date(), }); } catch (e) { response.status(400).send({ @@ -63,6 +64,7 @@ export const meRoutes = (fastify, _, done) => { try { const [user] = await db.select().from(users).where(eq(users.id, req.session.user_id)); if (!user) throw new Error("user not found"); + if(new Date(user.subscribed_until) > new Date()) return reply.redirect(303, "/me/billing"); const checkout_url = await createCheckout("price_1PVrA2FrdGTeTMwd5ZDjQKDZ", user.stripe_id, { user_id: user.id }, { diff --git a/src/utils/stripe.js b/src/utils/stripe.js index 0bd3c4e..13c9a07 100644 --- a/src/utils/stripe.js +++ b/src/utils/stripe.js @@ -2,7 +2,7 @@ import loadStripe from 'stripe'; import { env } from './env'; import { db } from '../db'; import { users } from '../db/schemas'; -import { eq } from 'drizzle-orm'; +import { eq, sql } from 'drizzle-orm'; const stripe = await loadStripe('sk_test_51MRcs0FrdGTeTMwdCgd6Z3I1913esqFD2W171b1W4PnRsdxfOCDrwtawiKFgS7R2ZDkWTSqfxp5Gl1GTd8aospWv00vFXvO6iC'); @@ -95,8 +95,9 @@ export async function handleWebhook(body, signature) { await db.update(users).set({ subscribed_until: nextMonthDate, - subscription_tier: "basic" - }); + subscription_tier: "basic", + tokens: sql`${users.tokens} + 30` + }).where(eq(users.stripe_id, data.object.customer)); break; case 'invoice.payment_failed': console.log("broke boy!") diff --git a/yarn.lock b/yarn.lock index d7bf478..eba9cdc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -53,15 +53,15 @@ "@esbuild-kit/core-utils" "^3.0.0" get-tsconfig "^4.4.0" -"@esbuild/linux-arm64@0.17.19": +"@esbuild/win32-x64@0.17.19": version "0.17.19" - resolved "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.17.19.tgz" - integrity sha512-ct1Tg3WGwd3P+oZYqic+YZF4snNl2bsnMKRkb3ozHmnM0dGWuxcPTTntAF6bOP0Sp4x0PjSF+4uHQ1xvxfRKqg== + resolved "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.17.19.tgz" + integrity sha512-lAhycmKnVOuRYNtRtatQR1LPQf2oYCkRGkSFnseDAKPl8lu5SOsK/e1sXe5a0Pc5kHIHe6P2I/ilntNv2xf3cA== -"@esbuild/linux-arm64@0.19.12": +"@esbuild/win32-x64@0.19.12": version "0.19.12" - resolved "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.19.12.tgz" - integrity sha512-EoTjyYyLuVPfdPLsGVVVC8a0p1BFFvtpQDB/YLEhaXyf/5bczaGeN15QkR+O4S5LeJ92Tqotve7i1jn35qwvdA== + resolved "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.19.12.tgz" + integrity sha512-T1QyPSDCyMXaO3pzBkF96E8xMkiRYbUEZADd29SyPGabqxMViNoii+NcK7eWJAEoU6RZyEm5lVSIjTmcdoB9HA== "@eslint-community/eslint-utils@^4.1.2", "@eslint-community/eslint-utils@^4.2.0", "@eslint-community/eslint-utils@^4.4.0": version "4.4.0" @@ -1401,13 +1401,6 @@ binary-extensions@^2.0.0: resolved "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.2.0.tgz" integrity sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA== -bindings@1.5.0: - version "1.5.0" - resolved "https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz" - integrity sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ== - dependencies: - file-uri-to-path "1.0.0" - bplist-parser@^0.2.0: version "0.2.0" resolved "https://registry.npmjs.org/bplist-parser/-/bplist-parser-0.2.0.tgz" @@ -1648,11 +1641,6 @@ cookie@^0.5.0: resolved "https://registry.npmjs.org/cookie/-/cookie-0.5.0.tgz" integrity sha512-YZ3GUyn/o8gfKJlnlX7g7xq4gyO6OSuhGPKaaGssGB2qgDUS0gPgtTvoyZLTt9Ab6dC4hfc9dV5arkvc/OCmrw== -core-util-is@~1.0.0: - version "1.0.3" - resolved "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz" - integrity sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ== - cosmiconfig@^8.2.0: version "8.3.6" resolved "https://registry.npmjs.org/cosmiconfig/-/cosmiconfig-8.3.6.tgz" @@ -2579,11 +2567,6 @@ file-entry-cache@^6.0.1: dependencies: flat-cache "^3.0.4" -file-uri-to-path@1.0.0: - version "1.0.0" - resolved "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz" - integrity sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw== - fill-range@^7.0.1: version "7.0.1" resolved "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz" @@ -3116,7 +3099,7 @@ inflight@^1.0.4: once "^1.3.0" wrappy "1" -inherits@~2.0.1, inherits@2, inherits@2.0.4: +inherits@2, inherits@2.0.4: version "2.0.4" resolved "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz" integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ== @@ -3401,11 +3384,6 @@ isarray@^2.0.5: resolved "https://registry.npmjs.org/isarray/-/isarray-2.0.5.tgz" integrity sha512-xHjhDr3cNBK0BzdUJSPXZntQUx/mwMS5Rw4A7lPJ90XGAO6ISP/ePDNuo0vhqOZU+UD5JoodwCAAoZQd3FeAKw== -isarray@0.0.1: - version "0.0.1" - resolved "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz" - integrity sha512-D2S+3GLxWH+uhrNEcoh/fnmYeP8E8/zHl644d/jdA0g2uyXvy3sb0qxotE+ne0LtccHknQzWwZEzhak7oJ0COQ== - isexe@^2.0.0: version "2.0.0" resolved "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz" @@ -3536,14 +3514,6 @@ levn@^0.4.1: prelude-ls "^1.2.1" type-check "~0.4.0" -libpq@1.8.13: - version "1.8.13" - resolved "https://registry.npmjs.org/libpq/-/libpq-1.8.13.tgz" - integrity sha512-t1wpnGVgwRIFSKoe4RFUllAFj953kNMcdXhGvFJwI0r6lJQqgSwTeiIciaCinjOmHk0HnFeWQSMC6Uw2591G4A== - dependencies: - bindings "1.5.0" - nan "2.19.0" - light-my-request@^5.11.0: version "5.11.0" resolved "https://registry.npmjs.org/light-my-request/-/light-my-request-5.11.0.tgz" @@ -3764,11 +3734,6 @@ mylas@^2.1.9: resolved "https://registry.npmjs.org/mylas/-/mylas-2.1.13.tgz" integrity sha512-+MrqnJRtxdF+xngFfUUkIMQrUUL0KsxbADUkn23Z/4ibGg192Q+z+CQyiYwvWTsYjJygmMR8+w3ZDa98Zh6ESg== -nan@2.19.0: - version "2.19.0" - resolved "https://registry.npmjs.org/nan/-/nan-2.19.0.tgz" - integrity sha512-nO1xXxfh/RWNxfd/XPfbIfFk5vgLsAxUR9y5O0cHMJu/AW9U95JLXqthYHjEp+8gQ5p96K9jUp8nbVOxCdRbtw== - natural-compare@^1.4.0: version "1.4.0" resolved "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz" @@ -4078,15 +4043,6 @@ pg-int8@1.0.1: resolved "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz" integrity sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw== -pg-native@>=3.0.1: - version "3.1.0" - resolved "https://registry.npmjs.org/pg-native/-/pg-native-3.1.0.tgz" - integrity sha512-nhMf/d6ypOc6jtUYlfGkLyPkYK+boor0FziOcsnqVxQZlGIXjBlSp3bNkW27O1RN46JlupjziIr1NP3aCCyWOw== - dependencies: - libpq "1.8.13" - pg-types "^1.12.1" - readable-stream "1.0.31" - pg-numeric@1.0.2: version "1.0.2" resolved "https://registry.npmjs.org/pg-numeric/-/pg-numeric-1.0.2.tgz" @@ -4102,17 +4058,6 @@ pg-protocol@*, pg-protocol@^1.6.1: resolved "https://registry.npmjs.org/pg-protocol/-/pg-protocol-1.6.1.tgz" integrity sha512-jPIlvgoD63hrEuihvIg+tJhoGjUsLPn6poJY9N5CnlPd91c2T18T/9zBtLxZSb1EhYxBRoZJtzScCaWlYLtktg== -pg-types@^1.12.1: - version "1.13.0" - resolved "https://registry.npmjs.org/pg-types/-/pg-types-1.13.0.tgz" - integrity sha512-lfKli0Gkl/+za/+b6lzENajczwZHc7D5kiUCZfgm914jipD2kIOIvEkAhZ8GrW3/TUoP9w8FHjwpPObBye5KQQ== - dependencies: - pg-int8 "1.0.1" - postgres-array "~1.0.0" - postgres-bytea "~1.0.0" - postgres-date "~1.0.0" - postgres-interval "^1.1.0" - pg-types@^2.1.0, pg-types@^2.2.0: version "2.2.0" resolved "https://registry.npmjs.org/pg-types/-/pg-types-2.2.0.tgz" @@ -4230,11 +4175,6 @@ pluralize@^8.0.0: resolved "https://registry.npmjs.org/pluralize/-/pluralize-8.0.0.tgz" integrity sha512-Nc3IT5yHzflTfbjgqWcCPpo7DaKy4FnpB0l/zCAW0Tc7jxAiuqSxHasntB3D7887LSrA93kDJ9IXovxJYxyLCA== -postgres-array@~1.0.0: - version "1.0.3" - resolved "https://registry.npmjs.org/postgres-array/-/postgres-array-1.0.3.tgz" - integrity sha512-5wClXrAP0+78mcsNX3/ithQ5exKvCyK5lr5NEEEeGwwM6NJdQgzIJBVxLvRW+huFpX92F2QnZ5CcokH0VhK2qQ== - postgres-array@~2.0.0: version "2.0.0" resolved "https://registry.npmjs.org/postgres-array/-/postgres-array-2.0.0.tgz" @@ -4257,7 +4197,7 @@ postgres-bytea@~3.0.0: dependencies: obuf "~1.1.2" -postgres-date@~1.0.0, postgres-date@~1.0.4: +postgres-date@~1.0.4: version "1.0.7" resolved "https://registry.npmjs.org/postgres-date/-/postgres-date-1.0.7.tgz" integrity sha512-suDmjLVQg78nMK2UZ454hAG+OAW+HQPZ6n++TNDUX+L0+uUlLywnoxJKDou51Zm+zTCjrCl0Nq6J9C5hP9vK/Q== @@ -4407,16 +4347,6 @@ readable-stream@^4.0.0: events "^3.3.0" process "^0.11.10" -readable-stream@1.0.31: - version "1.0.31" - resolved "https://registry.npmjs.org/readable-stream/-/readable-stream-1.0.31.tgz" - integrity sha512-tco/Dwv1f/sgIgN6CWdj/restacPKNskK6yps1981ivH2ZmLYcs5o5rVzL3qaO/cSkhN8hYOMWs7+glzOLSgRg== - dependencies: - core-util-is "~1.0.0" - inherits "~2.0.1" - isarray "0.0.1" - string_decoder "~0.10.x" - readdirp@~3.6.0: version "3.6.0" resolved "https://registry.npmjs.org/readdirp/-/readdirp-3.6.0.tgz" @@ -4814,11 +4744,6 @@ stream-wormhole@^1.1.0: resolved "https://registry.npmjs.org/stream-wormhole/-/stream-wormhole-1.1.0.tgz" integrity sha512-gHFfL3px0Kctd6Po0M8TzEvt3De/xu6cnRrjlfYNhwbhLPLwigI2t1nc6jrzNuaYg5C4YF78PPFuQPzRiqn9ew== -string_decoder@~0.10.x: - version "0.10.31" - resolved "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz" - integrity sha512-ev2QzSzWPYmy9GuqfIVildA4OdcGLeFZQrq5ys6RtiuF+RQQiZWr8TZNyAcuVXyQRYfEO+MsoB/1BuQVhOJuoQ== - string-width@^4.2.0: version "4.2.3" resolved "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz"