-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support manual sync/backfill (#6)
- Loading branch information
Showing
19 changed files
with
531 additions
and
114 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 |
---|---|---|
@@ -1,16 +1,18 @@ | ||
# Postgres database URL including auth and search path | ||
DATABASE_URL=postgres://postgres:postgres@host:5432/postgres?sslmode=disable&search_path=orb | ||
|
||
# optional | ||
# Optional | ||
SCHEMA=orb | ||
|
||
# Secret to validate signatures of Orb webhooks | ||
ORB_WEBHOOK_SECRET=whsec_ | ||
|
||
# optional, only needed when you want to actively sync data and call the Orb API, not needed for webhook processing | ||
# Access the Orb API | ||
ORB_API_KEY=test_ | ||
|
||
# optional | ||
PORT=8080 | ||
# Optional, API key to authorize requests against the sync endpoints. When calling a sync endpoint this value has to be provided via HTTP header "Authorization". | ||
#API_KEY_SYNC= | ||
|
||
# API key to access the endpoints of the Node.js app | ||
API_KEY=api_key_test | ||
# Optional | ||
PORT=8080 | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
import { Static, Type } from '@fastify/type-provider-typebox'; | ||
import type { FastifyInstance } from 'fastify'; | ||
import { verifyApiKey } from '../utils/verifyApiKey'; | ||
|
||
const SchemaRequestParamsSyncCreditNotes = Type.Object({ | ||
limit: Type.Optional(Type.Number({ minimum: 1, maximum: 100 })), | ||
}); | ||
|
||
const SchemaRequestParamsSyncCustomers = Type.Object({ | ||
limit: Type.Optional(Type.Number({ minimum: 1, maximum: 100 })), | ||
createdAtGt: Type.Optional( | ||
Type.String({ | ||
format: 'date-time', | ||
}) | ||
), | ||
createdAtGte: Type.Optional( | ||
Type.String({ | ||
format: 'date-time', | ||
}) | ||
), | ||
createdAtLt: Type.Optional( | ||
Type.String({ | ||
format: 'date-time', | ||
}) | ||
), | ||
createdAtLte: Type.Optional( | ||
Type.String({ | ||
format: 'date-time', | ||
}) | ||
), | ||
}); | ||
|
||
const SchemaRequestParamsSyncSubscriptions = Type.Object({ | ||
limit: Type.Optional(Type.Number({ minimum: 1, maximum: 100 })), | ||
createdAtGt: Type.Optional( | ||
Type.String({ | ||
format: 'date-time', | ||
}) | ||
), | ||
createdAtGte: Type.Optional( | ||
Type.String({ | ||
format: 'date-time', | ||
}) | ||
), | ||
createdAtLt: Type.Optional( | ||
Type.String({ | ||
format: 'date-time', | ||
}) | ||
), | ||
createdAtLte: Type.Optional( | ||
Type.String({ | ||
format: 'date-time', | ||
}) | ||
), | ||
}); | ||
|
||
const SchemaRequestParamsSyncInvoices = Type.Object({ | ||
limit: Type.Optional(Type.Number({ minimum: 1, maximum: 100 })), | ||
createdAtGt: Type.Optional( | ||
Type.String({ | ||
format: 'date-time', | ||
}) | ||
), | ||
createdAtGte: Type.Optional( | ||
Type.String({ | ||
format: 'date-time', | ||
}) | ||
), | ||
createdAtLt: Type.Optional( | ||
Type.String({ | ||
format: 'date-time', | ||
}) | ||
), | ||
createdAtLte: Type.Optional( | ||
Type.String({ | ||
format: 'date-time', | ||
}) | ||
), | ||
}); | ||
|
||
export default async function routes(fastify: FastifyInstance) { | ||
fastify.post<{ | ||
Querystring: Static<typeof SchemaRequestParamsSyncCreditNotes>; | ||
}>('/sync/credit_notes', { | ||
preHandler: [verifyApiKey], | ||
schema: { | ||
querystring: SchemaRequestParamsSyncCreditNotes, | ||
}, | ||
handler: async (request, reply) => { | ||
const query = request.query; | ||
|
||
const count = await fastify.orbSync.sync('credit_notes', { limit: query.limit }); | ||
|
||
return reply.send({ count }); | ||
}, | ||
}); | ||
|
||
fastify.post<{ | ||
Querystring: Static<typeof SchemaRequestParamsSyncCustomers>; | ||
}>('/sync/customers', { | ||
preHandler: [verifyApiKey], | ||
schema: { | ||
querystring: SchemaRequestParamsSyncCustomers, | ||
}, | ||
handler: async (request, reply) => { | ||
const query = request.query; | ||
|
||
const count = await fastify.orbSync.sync('customers', { | ||
limit: query.limit, | ||
createdAtGt: query.createdAtGt, | ||
createdAtGte: query.createdAtGte, | ||
createdAtLt: query.createdAtLt, | ||
createdAtLte: query.createdAtLte, | ||
}); | ||
|
||
return reply.send({ count }); | ||
}, | ||
}); | ||
|
||
fastify.post<{ | ||
Querystring: Static<typeof SchemaRequestParamsSyncSubscriptions>; | ||
}>('/sync/subscriptions', { | ||
preHandler: [verifyApiKey], | ||
schema: { | ||
querystring: SchemaRequestParamsSyncSubscriptions, | ||
}, | ||
handler: async (request, reply) => { | ||
const query = request.query; | ||
|
||
const count = await fastify.orbSync.sync('subscriptions', { | ||
limit: query.limit, | ||
createdAtGt: query.createdAtGt, | ||
createdAtGte: query.createdAtGte, | ||
createdAtLt: query.createdAtLt, | ||
createdAtLte: query.createdAtLte, | ||
}); | ||
|
||
return reply.send({ count }); | ||
}, | ||
}); | ||
|
||
fastify.post<{ | ||
Querystring: Static<typeof SchemaRequestParamsSyncInvoices>; | ||
}>('/sync/invoices', { | ||
preHandler: [verifyApiKey], | ||
schema: { | ||
querystring: SchemaRequestParamsSyncInvoices, | ||
}, | ||
handler: async (request, reply) => { | ||
const query = request.query; | ||
|
||
const count = await fastify.orbSync.sync('invoices', { | ||
limit: query.limit, | ||
createdAtGt: query.createdAtGt, | ||
createdAtGte: query.createdAtGte, | ||
createdAtLt: query.createdAtLt, | ||
createdAtLte: query.createdAtLte, | ||
}); | ||
|
||
return reply.send({ count }); | ||
}, | ||
}); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import dotenv from 'dotenv'; | ||
import assert from 'assert'; | ||
|
||
type configType = { | ||
/** Optional, API key to authorize requests against the sync endpoints */ | ||
API_KEY_SYNC?: string; | ||
|
||
/** Port number the API is running on, defaults to 8080 */ | ||
PORT: number; | ||
|
||
/** Postgres database URL including auth and search path */ | ||
DATABASE_URL: string | ||
|
||
/** Secret to validate signatures of Orb webhooks */ | ||
ORB_WEBHOOK_SECRET: string | ||
|
||
/** Defaults to Orb */ | ||
DATABASE_SCHEMA: string | ||
|
||
/** Access the Orb API */ | ||
ORB_API_KEY?: string | ||
}; | ||
|
||
function getConfigFromEnv(key: string, defaultValue?: string): string { | ||
const value = process.env[key]; | ||
return value || defaultValue!; | ||
} | ||
|
||
let config: configType; | ||
|
||
export function getConfig(): configType { | ||
if (config) return config; | ||
|
||
dotenv.config(); | ||
|
||
config = { | ||
API_KEY_SYNC: getConfigFromEnv('API_KEY_SYNC'), | ||
ORB_API_KEY: getConfigFromEnv('ORB_API_KEY'), | ||
DATABASE_SCHEMA: getConfigFromEnv('DATABASE_SCHEMA', 'orb'), | ||
DATABASE_URL: getConfigFromEnv('DATABASE_URL'), | ||
ORB_WEBHOOK_SECRET: getConfigFromEnv('ORB_WEBHOOK_SECRET'), | ||
PORT: Number(getConfigFromEnv('PORT', '8080')), | ||
}; | ||
|
||
assert(!Number.isNaN(config.PORT), 'PORT must be a number'); | ||
assert(config.DATABASE_URL, 'DATABASE_URL is required'); | ||
assert(config.ORB_WEBHOOK_SECRET, 'ORB_WEBHOOK_SECRET is required'); | ||
|
||
return config; | ||
} |
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,15 @@ | ||
import { FastifyReply, FastifyRequest, HookHandlerDoneFunction } from 'fastify'; | ||
import { getConfig } from './config'; | ||
|
||
export const verifyApiKey = (request: FastifyRequest, reply: FastifyReply, done: HookHandlerDoneFunction): unknown => { | ||
const config = getConfig(); | ||
|
||
if (!request.headers || !request.headers.authorization) { | ||
return reply.code(401).send('Unauthorized'); | ||
} | ||
const { authorization } = request.headers; | ||
if (authorization !== config.API_KEY_SYNC) { | ||
return reply.code(401).send('Unauthorized'); | ||
} | ||
done(); | ||
}; |
Oops, something went wrong.