8 Commits
1.1.0 ... 1.1.2

Author SHA1 Message Date
William Oldham
1bb344ec2f Merge pull request #16 from movie-web/dev
v1.1.2: Fix Settings
2023-11-22 23:23:00 +00:00
William Oldham
b30623c483 Merge pull request #15 from movie-web/fix-user-settings
Fix user settings
2023-11-22 23:21:26 +00:00
William Oldham
233cb11ac6 Bump version 2023-11-22 23:17:29 +00:00
William Oldham
d3aa4847f8 Update UserSettings primary key to text, to match user 2023-11-22 23:17:29 +00:00
William Oldham
4bf2e658f7 Fix ESLint Ignore for migrations 2023-11-22 23:17:29 +00:00
William Oldham
c18ff489c3 Merge pull request #14 from movie-web/dev
Fix @me endpoint
2023-11-19 20:00:54 +00:00
William Oldham
fcc3dc60d9 Merge branch 'master' into dev 2023-11-19 20:00:06 +00:00
William Oldham
c7a706cdf0 Fix @me endpoint 2023-11-19 19:59:02 +00:00
6 changed files with 41 additions and 20 deletions

View File

@@ -13,7 +13,7 @@ module.exports = {
sourceType: 'module',
},
plugins: ['@typescript-eslint'],
ignorePatterns: ['./src/db/migrations/**/*'],
ignorePatterns: ['src/db/migrations/**/*'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',

View File

@@ -1,6 +1,6 @@
{
"name": "backend",
"version": "1.1.0",
"version": "1.1.2",
"private": true,
"homepage": "https://github.com/movie-web/backend",
"engines": {

View File

@@ -587,12 +587,12 @@
"columns": {
"id": {
"name": "id",
"type": "uuid",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "uuid"
"mappedType": "text"
},
"application_theme": {
"name": "application_theme",

View File

@@ -0,0 +1,14 @@
import { Migration } from '@mikro-orm/migrations';
export class Migration20231122231620 extends Migration {
async up(): Promise<void> {
this.addSql('alter table "user_settings" alter column "id" type text using ("id"::text);');
}
async down(): Promise<void> {
this.addSql('alter table "user_settings" alter column "id" drop default;');
this.addSql('alter table "user_settings" alter column "id" type uuid using ("id"::text::uuid);');
}
}

View File

@@ -1,10 +1,9 @@
import { Entity, PrimaryKey, Property } from '@mikro-orm/core';
import { randomUUID } from 'crypto';
@Entity({ tableName: 'user_settings' })
export class UserSettings {
@PrimaryKey({ name: 'id', type: 'uuid' })
id: string = randomUUID();
@PrimaryKey({ name: 'id', type: 'text' })
id!: string;
@Property({ name: 'application_theme', nullable: true })
applicationTheme?: string | null;

View File

@@ -1,4 +1,4 @@
import { Session, formatSession } from '@/db/models/Session';
import { formatSession } from '@/db/models/Session';
import { User, formatUser } from '@/db/models/User';
import { StatusError } from '@/services/error';
import { handle } from '@/services/handler';
@@ -6,6 +6,24 @@ import { makeRouter } from '@/services/router';
import { z } from 'zod';
export const userGetRouter = makeRouter((app) => {
app.get(
'/users/@me',
handle(async ({ auth, em }) => {
await auth.assert();
const user = await em.findOne(User, { id: auth.user.id });
if (!user) throw new StatusError('User does not exist', 404);
const session = await auth.getSession();
if (!session) throw new StatusError('Session does not exist', 400);
return {
user: formatUser(user),
session: formatSession(session),
};
}),
);
app.get(
'/users/:uid',
{
@@ -17,25 +35,15 @@ export const userGetRouter = makeRouter((app) => {
},
handle(async ({ auth, params, em }) => {
await auth.assert();
let uid = params.uid;
if (uid === '@me') uid = auth.user.id;
if (auth.user.id !== uid)
if (auth.user.id !== params.uid)
throw new StatusError('Cannot access users other than yourself', 403);
const user = await em.findOne(User, { id: uid });
const user = await em.findOne(User, { id: params.uid });
if (!user) throw new StatusError('User does not exist', 404);
let session: Session | undefined = undefined;
if (uid === '@me') {
session = (await auth.getSession()) ?? undefined;
if (!session) throw new StatusError('Session does not exist', 400);
}
return {
user: formatUser(user),
session: session ? formatSession(session) : undefined,
};
}),
);