Skip to content

Commit

Permalink
Add Profile Sub
Browse files Browse the repository at this point in the history
  • Loading branch information
ingalls committed Feb 14, 2024
1 parent 3020138 commit 1ce6fd1
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 7 deletions.
3 changes: 2 additions & 1 deletion api/lib/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ export const Profile = pgTable('profile', {
});

export const ProfileSubscription = pgTable('profile_subscriptions', {
username: text('username').primaryKey(),
id: serial('id').primaryKey(),
username: text('username').notNull(),
created: timestamp('created', { withTimezone: true }).notNull().default(sql`Now()`),
updated: timestamp('updated', { withTimezone: true }).notNull().default(sql`Now()`),
mission: text('mission').notNull()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
CREATE TABLE IF NOT EXISTS "profile_subscriptions" (
"username" text PRIMARY KEY NOT NULL,
"id" serial PRIMARY KEY NOT NULL,
"username" text NOT NULL,
"created" timestamp with time zone DEFAULT Now() NOT NULL,
"updated" timestamp with time zone DEFAULT Now() NOT NULL,
"mission" text NOT NULL
Expand Down
10 changes: 8 additions & 2 deletions api/migrations/meta/0010_snapshot.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": "ac7d3eb1-13f2-45ec-aad2-13b04e149710",
"id": "b6417e62-7ce3-496f-b350-b67445209556",
"prevId": "c36f059c-c7a8-4e81-86a8-0b290d92979e",
"version": "5",
"dialect": "pg",
Expand Down Expand Up @@ -1106,10 +1106,16 @@
"name": "profile_subscriptions",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "serial",
"primaryKey": true,
"notNull": true
},
"username": {
"name": "username",
"type": "text",
"primaryKey": true,
"primaryKey": false,
"notNull": true
},
"created": {
Expand Down
4 changes: 2 additions & 2 deletions api/migrations/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@
{
"idx": 10,
"version": "5",
"when": 1707949658557,
"tag": "0010_gorgeous_lifeguard",
"when": 1707951512171,
"tag": "0010_striped_lila_cheney",
"breakpoints": true
}
]
Expand Down
34 changes: 33 additions & 1 deletion api/routes/profile-subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Response } from 'express';
import { AuthRequest } from '@tak-ps/blueprint-login';
import Config from '../lib/config.js';
import { AuthResource } from '@tak-ps/blueprint-login';
import { sql } from 'drizzle-orm';

export default async function router(schema: any, config: Config) {
await schema.get('/profile/sub', {
Expand Down Expand Up @@ -38,15 +39,46 @@ export default async function router(schema: any, config: Config) {
}, async (req: AuthRequest, res: Response) => {
try {
const user = await Auth.as_user(config.models, req);
const sub = await config.models.ProfileSubscription.commit({
const sub = await config.models.ProfileSubscription.generate({
...req.body,
username: user.email
});

// @TODO Subscribe to TAK mission

return res.json(sub);
} catch (err) {
return Err.respond(err, res);
}
});

await schema.delete('/profile/sub/:subid', {
name: 'Delete Subscription',
auth: 'user',
group: 'Profile',
':subid': 'integer',
description: 'delete a mission subscription',
res: 'res.Standard.json'
}, async (req: AuthRequest, res: Response) => {
try {
const user = await Auth.as_user(config.models, req);

const sub = await config.models.ProfileSubscription.from(parseInt(String(req.params.subid)));

if (sub.username !== user.email) {
throw new Err(403, null, 'Cannot delete anothers subscriptions');
}

await config.models.ProfileSubscription.delete(parseInt(String(req.params.subid)));

// @TODO Unsubscribe if TAK subscription is active

return res.json({
status: 200,
message: 'Subscription Deleted'
});
} catch (err) {
return Err.respond(err, res);
}
});
}
4 changes: 4 additions & 0 deletions api/schema/profile_subscriptions.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"type": "object",
"additionalProperties": false,
"properties": {
"id": {
"$ref": "./profile_subscriptions/id.json"
},
"username": {
"$ref": "./profile_subscriptions/username.json"
},
Expand All @@ -16,6 +19,7 @@
}
},
"required": [
"id",
"username",
"created",
"updated",
Expand Down
5 changes: 5 additions & 0 deletions api/schema/profile_subscriptions/id.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "number",
"description": "",
"$comment": "int4"
}

0 comments on commit 1ce6fd1

Please sign in to comment.