-
Notifications
You must be signed in to change notification settings - Fork 635
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add express and refactor webhook handler
- Loading branch information
1 parent
d634f9d
commit 0faab33
Showing
18 changed files
with
531 additions
and
282 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import express from 'express' | ||
import AppContext from '../context' | ||
|
||
export const createRouter = (ctx: AppContext): express.Router => { | ||
const router = express.Router() | ||
|
||
router.get('/_health', async function (_req, res) { | ||
res.send({ version: ctx.cfg.service.version }) | ||
}) | ||
|
||
return router | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import express, { RequestHandler } from 'express' | ||
import { AppContext } from '..' | ||
import { RevenueCatClient } from '../purchases' | ||
import { addPurchaseOperation, RcEventBody } from '../purchases' | ||
import { isValidDid } from '../routes/util' | ||
import { httpLogger as log } from '..' | ||
|
||
type AppContextWithRevenueCatClient = AppContext & { | ||
revenueCatClient: RevenueCatClient | ||
} | ||
|
||
const auth = | ||
(ctx: AppContextWithRevenueCatClient): RequestHandler => | ||
(req: express.Request, res: express.Response, next: express.NextFunction) => | ||
ctx.revenueCatClient.isWebhookAuthorizationValid( | ||
req.header('Authorization'), | ||
) | ||
? next() | ||
: res.status(403).send({ | ||
success: false, | ||
error: 'Forbidden: invalid authentication for RevenueCat webhook', | ||
}) | ||
|
||
const webhookHandler = | ||
(ctx: AppContextWithRevenueCatClient): RequestHandler => | ||
async (req, res) => { | ||
const { revenueCatClient } = ctx | ||
|
||
const body: RcEventBody = req.body | ||
|
||
try { | ||
const { app_user_id: actorDid } = body.event | ||
|
||
if (!isValidDid(actorDid)) { | ||
return res.status(400).send({ | ||
success: false, | ||
error: 'Bad request: invalid DID in app_user_id', | ||
}) | ||
} | ||
|
||
const entitlements = | ||
await revenueCatClient.getEntitlementIdentifiers(actorDid) | ||
|
||
const id = await addPurchaseOperation(ctx.db, actorDid, entitlements) | ||
|
||
res.send({ success: true, operationId: id }) | ||
} catch (error) { | ||
log.error(error) | ||
|
||
res.status(500).send({ | ||
success: false, | ||
error: | ||
'Internal server error: an error happened while processing the request', | ||
}) | ||
} | ||
} | ||
|
||
const assertAppContextWithRevenueCatClient: ( | ||
ctx: AppContext, | ||
) => asserts ctx is AppContextWithRevenueCatClient = (ctx: AppContext) => { | ||
if (!ctx.revenueCatClient) { | ||
throw new Error( | ||
'RevenueCat webhook was tried to be set up without configuring a RevenueCat client.', | ||
) | ||
} | ||
} | ||
|
||
export const createRouter = (ctx: AppContext): express.Router => { | ||
assertAppContextWithRevenueCatClient(ctx) | ||
|
||
const router = express.Router() | ||
router.use(auth(ctx)) | ||
router.use(express.json()) | ||
router.post('/', webhookHandler(ctx)) | ||
return router | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
packages/bsync/src/db/schema/subs_item.ts → ...ages/bsync/src/db/schema/purchase_item.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
import { ColumnType, Selectable } from 'kysely' | ||
|
||
export interface SubsItem { | ||
export interface PurchaseItem { | ||
actorDid: string | ||
// https://github.com/kysely-org/kysely/issues/137 | ||
entitlements: ColumnType<string[], string, string> | ||
fromId: number | ||
} | ||
|
||
export type SubsItemEntry = Selectable<SubsItem> | ||
export type PurchaseItemEntry = Selectable<PurchaseItem> | ||
|
||
export const tableName = 'subs_item' | ||
export const tableName = 'purchase_item' | ||
|
||
export type PartialDB = { [tableName]: SubsItem } | ||
export type PartialDB = { [tableName]: PurchaseItem } |
10 changes: 5 additions & 5 deletions
10
packages/bsync/src/db/schema/subs_op.ts → packages/bsync/src/db/schema/purchase_op.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
import { ColumnType, GeneratedAlways, Selectable } from 'kysely' | ||
|
||
export interface SubsOp { | ||
export interface PurchaseOp { | ||
id: GeneratedAlways<number> | ||
actorDid: string | ||
// https://github.com/kysely-org/kysely/issues/137 | ||
entitlements: ColumnType<string[], string, string> | ||
createdAt: GeneratedAlways<Date> | ||
} | ||
|
||
export type SubsOpEntry = Selectable<SubsOp> | ||
export type PurchaseOpEntry = Selectable<PurchaseOp> | ||
|
||
export const tableName = 'subs_op' | ||
export const tableName = 'purchase_op' | ||
|
||
export type PartialDB = { [tableName]: SubsOp } | ||
export type PartialDB = { [tableName]: PurchaseOp } | ||
|
||
export const createSubsOpChannel = 'subs_op_create' // used with listen/notify | ||
export const createPurchaseOpChannel = 'purchase_op_create' // used with listen/notify |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.