Skip to content

Commit

Permalink
feat(db): add notNull constraint to expiresAt field in schema
Browse files Browse the repository at this point in the history
fix(db): add missing license and customer info in license query

feat(api): add license validation logic in POST /license/validate
  • Loading branch information
flemzord committed May 2, 2024
1 parent a1d43c0 commit 44afa45
Show file tree
Hide file tree
Showing 5 changed files with 355 additions and 4 deletions.
43 changes: 40 additions & 3 deletions apps/web/server/api/license/validate.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,33 @@ import { eq } from 'drizzle-orm';
import { zh } from 'h3-zod';
import { z } from 'zod';

interface License {
name: string;
token: string;
productName: string;
customerName: string;
expirationDate: Date;
createdAt: Date;
updatedAt: Date;
}

const checkLicenseIsValid = async (license: License) => {
if (!license.expirationDate) {
throw createError({
statusCode: 400,
statusMessage: 'License has no expiration date',
});
}

if (license.expirationDate <= new Date()) {
throw createError({
statusCode: 400,
statusMessage: 'License expired',
});
}
return license;
};

export default defineEventHandler(async (event) => {
const body = await zh.useSafeValidatedBody(
event,
Expand All @@ -18,26 +45,36 @@ export default defineEventHandler(async (event) => {
});
}

const getLicense = await useDB()
const license: License[] = await useDB()
.select({
name: schema.license.name,
token: schema.license.token,
productName: schema.product.name,
customerName: schema.customer.name,
expirationDate: schema.license.expiresAt,
createdAt: schema.license.createdAt,
updatedAt: schema.license.updatedAt,
})
.from(schema.license)
.innerJoin(
schema.customer,
eq(schema.license.customerId, schema.customer.id),
)
.innerJoin(schema.product, eq(schema.license.productId, schema.product.id))
.where(eq(schema.license.token, body.data.license))
.limit(1)
.execute();

if (getLicense.length === 0) {
if (license.length === 0) {
throw createError({
statusCode: 404,
statusMessage: 'License not found',
});
}

await checkLicenseIsValid(license[0]);

return {
data: getLicense[0],
data: license[0],
};
});
1 change: 1 addition & 0 deletions internal/db/src/migrations/0001_funny_trish_tilby.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "License" ALTER COLUMN "expires_at" SET NOT NULL;
306 changes: 306 additions & 0 deletions internal/db/src/migrations/meta/0001_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,306 @@
{
"id": "e7c6b701-a6f1-4473-b140-98c3349ca2f8",
"prevId": "91d720f3-8462-4101-a0d0-a7733a84162e",
"version": "5",
"dialect": "pg",
"tables": {
"Customer": {
"name": "Customer",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"user_id": {
"name": "user_id",
"type": "uuid",
"primaryKey": false,
"notNull": true
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": true
},
"metadata": {
"name": "metadata",
"type": "jsonb",
"primaryKey": false,
"notNull": false,
"default": "'{}'::jsonb"
},
"created_at": {
"name": "created_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"default": "now()"
},
"updated_at": {
"name": "updated_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {},
"foreignKeys": {
"Customer_user_id_User_id_fk": {
"name": "Customer_user_id_User_id_fk",
"tableFrom": "Customer",
"tableTo": "User",
"columnsFrom": [
"user_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"License": {
"name": "License",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"user_id": {
"name": "user_id",
"type": "uuid",
"primaryKey": false,
"notNull": true
},
"product_id": {
"name": "product_id",
"type": "uuid",
"primaryKey": false,
"notNull": true
},
"customer_id": {
"name": "customer_id",
"type": "uuid",
"primaryKey": false,
"notNull": true
},
"license": {
"name": "license",
"type": "text",
"primaryKey": false,
"notNull": true
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": true
},
"metadata": {
"name": "metadata",
"type": "jsonb",
"primaryKey": false,
"notNull": false,
"default": "'{}'::jsonb"
},
"expires_at": {
"name": "expires_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true
},
"created_at": {
"name": "created_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"default": "now()"
},
"updated_at": {
"name": "updated_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {},
"foreignKeys": {
"License_user_id_User_id_fk": {
"name": "License_user_id_User_id_fk",
"tableFrom": "License",
"tableTo": "User",
"columnsFrom": [
"user_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
},
"License_product_id_Product_id_fk": {
"name": "License_product_id_Product_id_fk",
"tableFrom": "License",
"tableTo": "Product",
"columnsFrom": [
"product_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
},
"License_customer_id_Customer_id_fk": {
"name": "License_customer_id_Customer_id_fk",
"tableFrom": "License",
"tableTo": "Customer",
"columnsFrom": [
"customer_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"Product": {
"name": "Product",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"user_id": {
"name": "user_id",
"type": "uuid",
"primaryKey": false,
"notNull": true
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": true
},
"metadata": {
"name": "metadata",
"type": "jsonb",
"primaryKey": false,
"notNull": false,
"default": "'{}'::jsonb"
},
"created_at": {
"name": "created_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"default": "now()"
},
"updated_at": {
"name": "updated_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {},
"foreignKeys": {
"Product_user_id_User_id_fk": {
"name": "Product_user_id_User_id_fk",
"tableFrom": "Product",
"tableTo": "User",
"columnsFrom": [
"user_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"User": {
"name": "User",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"email": {
"name": "email",
"type": "text",
"primaryKey": false,
"notNull": true
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": false
},
"githubId": {
"name": "githubId",
"type": "text",
"primaryKey": false,
"notNull": false
},
"created_at": {
"name": "created_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"default": "now()"
},
"updated_at": {
"name": "updated_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true,
"default": "now()"
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {},
"schemas": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}
7 changes: 7 additions & 0 deletions internal/db/src/migrations/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
"when": 1714640966914,
"tag": "0000_modern_captain_flint",
"breakpoints": true
},
{
"idx": 1,
"version": "5",
"when": 1714678164827,
"tag": "0001_funny_trish_tilby",
"breakpoints": true
}
]
}
Loading

0 comments on commit 44afa45

Please sign in to comment.