From 9ced895772ebecb2fa330ae2eecd42060abf2a7b Mon Sep 17 00:00:00 2001 From: Joe Carstairs <65492573+Sycamost@users.noreply.github.com> Date: Wed, 29 Nov 2023 18:15:14 +0000 Subject: WIP --- README.md | 25 ++++- api/_postgres.ts | 4 + api/create-subscription.ts | 26 +++++ src/components/LallansIssue.astro | 4 +- src/components/PayPalButtons.astro | 150 ------------------------- src/components/ProductPayPalButtons.astro | 150 +++++++++++++++++++++++++ src/components/Scotsoun.astro | 4 +- src/components/SubscriptionPayPalButtons.astro | 112 ++++++++++++++++++ src/pages/[locale]/created-subscription.astro | 7 ++ src/pages/[locale]/index.astro | 4 + 10 files changed, 330 insertions(+), 156 deletions(-) create mode 100644 api/_postgres.ts create mode 100644 api/create-subscription.ts delete mode 100644 src/components/PayPalButtons.astro create mode 100644 src/components/ProductPayPalButtons.astro create mode 100644 src/components/SubscriptionPayPalButtons.astro create mode 100644 src/pages/[locale]/created-subscription.astro diff --git a/README.md b/README.md index f24a853..27d9722 100644 --- a/README.md +++ b/README.md @@ -101,8 +101,8 @@ Follow this guidance alongside the [general guidance](#general-guidance). ## Infrastructure -The deployment infrastructure for the website is in four parts: the domains, the SSL certificates, -the hosting, and the CI/CD. +The deployment infrastructure for the website is in five parts: the domains, the SSL certificates, +the hosting, the CI/CD and the database. ### Domains @@ -151,3 +151,24 @@ deploy, and you've checked that everything looks good on [make a Pull Request to merge deployment-staging into deployment-production](https://github.com/Sycamost/lallans-wabsteid-astro/compare/deployment-production...deployment-staging). Once that Pull Request is approved by a code owner, merge it in and it should update production in under five minutes. + +### Database + +We have a database. It is a +[Postgres database hosted on Vercel](https://vercel.com/docs/storage/vercel-postgres/quickstart). + +It has two tables: + +- subscriptions + - id: VARCHAR(255) + - email_address: VARCHAR(255) +- dev_subscriptions + - id: VARCHAR(255) + - email_address: VARCHAR(255) + +One table is there so that users can look up their subscriptions, and thereby +retrieve the subscription ID so that they can amend or cancel their subscription. +The second table is there to test out the Postgres integration in staging +with dummy data. + +The easiest way to view, update, create and delete tables is on the Vercel dashboard. diff --git a/api/_postgres.ts b/api/_postgres.ts new file mode 100644 index 0000000..70b25c8 --- /dev/null +++ b/api/_postgres.ts @@ -0,0 +1,4 @@ +import env from './_env'; + +export const tablePrefix = env.ENVIRONMENT === 'prod' ? '' : 'dev_'; +export const tableSubscriptions = `${tablePrefix}subscriptions`; diff --git a/api/create-subscription.ts b/api/create-subscription.ts new file mode 100644 index 0000000..70b1499 --- /dev/null +++ b/api/create-subscription.ts @@ -0,0 +1,26 @@ +import type { VercelRequest, VercelResponse } from '@vercel/node'; + +import { sql } from '@vercel/postgres'; +import { tableSubscriptions } from '_postgres'; + +export default async function handler(request: VercelRequest, response: VercelResponse) { + const { subscriptionId, emailAddress }: Partial = request.body; + + if (!subscriptionId) throw new Error('Missing parameter: subscriptionId.'); + if (!emailAddress) throw new Error('Missing parameter: emailAddress.'); + + try { + const result = await sql` + INSERT INTO ${tableSubscriptions} (id, email_address) + VALUES (${subscriptionId}, ${emailAddress}) + ;`; + return response.status(200).json({ result }); + } catch (error) { + return response.status(500).json({ error }); + } +} + +type RequestBody = { + subscriptionId: string, + emailAddress: string, +}; diff --git a/src/components/LallansIssue.astro b/src/components/LallansIssue.astro index c8bc6eb..9fe686a 100644 --- a/src/components/LallansIssue.astro +++ b/src/components/LallansIssue.astro @@ -7,7 +7,7 @@ import type LallansIssue from '$types/LallansIssue'; import { Image } from 'astro:assets'; import LallansIssueContributions from './LallansIssueContributions.astro'; import LallansIssueContributors from './LallansIssueContributors.astro'; -import PayPalButtons from './PayPalButtons.astro'; +import ProductPayPalButtons from './ProductPayPalButtons.astro'; interface Props { issue: LallansIssue; @@ -27,7 +27,7 @@ const t = translate(locale);
- -
- - - diff --git a/src/components/ProductPayPalButtons.astro b/src/components/ProductPayPalButtons.astro new file mode 100644 index 0000000..952e6a8 --- /dev/null +++ b/src/components/ProductPayPalButtons.astro @@ -0,0 +1,150 @@ +--- +import env from '$lib/env'; +import type Price from '$types/Price'; + +interface Props { + successPageUrl: string; + productDescription: string; + shortDescription: string; + totalPrice: Price; +} + +const { successPageUrl, productDescription, shortDescription, totalPrice } = Astro.props; + +const PAYPAL_CLIENT_ID = + env.ENVIRONMENT === 'prod' ? env.PAYPAL_LIVE_CLIENT_ID : env.PAYPAL_SANDBOX_CLIENT_ID; +--- + + +
+
+ + diff --git a/src/components/Scotsoun.astro b/src/components/Scotsoun.astro index bbf9730..8252e71 100644 --- a/src/components/Scotsoun.astro +++ b/src/components/Scotsoun.astro @@ -4,7 +4,7 @@ import translate from '$i18n/translate'; import tSite from '$i18n/translations/site'; import tComponent from '$i18n/translations/components/scotsoun'; import type Scotsoun from '$types/Scotsoun'; -import PayPalButtons from './PayPalButtons.astro'; +import ProductPayPalButtons from './ProductPayPalButtons.astro'; import relativePath from '$lib/relativePath'; import { getLocaleFromPathOrThrow } from '$lib/getLocaleFromPath'; @@ -27,7 +27,7 @@ const t = translate(locale);
- +
+ + + diff --git a/src/pages/[locale]/created-subscription.astro b/src/pages/[locale]/created-subscription.astro new file mode 100644 index 0000000..f913932 --- /dev/null +++ b/src/pages/[locale]/created-subscription.astro @@ -0,0 +1,7 @@ +--- +import Page from '$layouts'; +--- + + +

TODO

+ diff --git a/src/pages/[locale]/index.astro b/src/pages/[locale]/index.astro index c67ded0..e5ba6b7 100644 --- a/src/pages/[locale]/index.astro +++ b/src/pages/[locale]/index.astro @@ -64,6 +64,10 @@ const mostRecentNewsItem = await getMostRecentNewsItem(locale);

+ +