Skip to content

Commit

Permalink
feat: sync orb plans (#10)
Browse files Browse the repository at this point in the history
Co-authored-by: Thomas <31189692+ecktoteckto@users.noreply.github.com>
  • Loading branch information
kevcodez and ecktoteckto authored Jul 9, 2024
1 parent 8250767 commit e242a8f
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 102 deletions.
6 changes: 3 additions & 3 deletions apps/node-fastify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
"author": "Supabase",
"license": "MIT",
"dependencies": {
"@fastify/autoload": "^5.9.0",
"@fastify/autoload": "^5.10.0",
"@fastify/type-provider-typebox": "^4.0.0",
"dotenv": "^16.4.5",
"fastify": "^4.27.0",
"fastify": "^4.28.1",
"orb-sync-lib": "*",
"pino": "^9.2.0"
},
"devDependencies": {
"pino-pretty": "^11.2.1",
"tsx": "^4.15.2"
"tsx": "^4.16.2"
}
}
46 changes: 46 additions & 0 deletions apps/node-fastify/src/routes/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,30 @@ const SchemaRequestParamsSyncInvoices = Type.Object({
),
});

const SchemaRequestParamsSyncPlans = 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>;
Expand Down Expand Up @@ -160,4 +184,26 @@ export default async function routes(fastify: FastifyInstance) {
return reply.send({ count });
},
});

fastify.post<{
Querystring: Static<typeof SchemaRequestParamsSyncPlans>;
}>('/sync/plans', {
preHandler: [verifyApiKey],
schema: {
querystring: SchemaRequestParamsSyncPlans,
},
handler: async (request, reply) => {
const query = request.query;

const count = await fastify.orbSync.sync('plans', {
limit: query.limit,
createdAtGt: query.createdAtGt,
createdAtGte: query.createdAtGte,
createdAtLt: query.createdAtLt,
createdAtLte: query.createdAtLte,
});

return reply.send({ count });
},
});
}
36 changes: 36 additions & 0 deletions db/migrations/0004_plans.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
create type orb.plan_status as enum('active', 'archived', 'draft');

create table if not exists
orb.plans (
id varchar(255) primary key,
name text,
description text,
maximum_amount text,
minimum_amount text,
created_at timestamp not null,
status orb.plan_status,
maximum jsonb,
minimum jsonb,
discount jsonb,
product jsonb,
version integer,
trial_config jsonb,
plan_phases json,
base_plan jsonb,
base_plan_id text,
external_plan_id text,
currency text,
invoicing_currency text,
net_terms integer,
default_invoice_memo text,
prices json,
metadata jsonb,
updated_at timestamptz default timezone ('utc'::text, now()) not null
);


create trigger handle_updated_at before
update on orb.plans for each row
execute function orb.set_updated_at ();

create index if not exists plans_external_plan_id_idx on orb.plans (external_plan_id);
Loading

0 comments on commit e242a8f

Please sign in to comment.