feat(server): init user module (#2018)

This commit is contained in:
Himself65
2023-04-18 18:14:25 -05:00
committed by GitHub
parent c6be29f944
commit 3a053af50c
16 changed files with 540 additions and 9 deletions

View File

@@ -19,7 +19,7 @@ export const getDefaultAFFiNEConfig: () => AFFiNEConfig = () => ({
},
https: false,
host: 'localhost',
port: 3000,
port: 3010,
path: '',
get origin() {
return this.dev

View File

@@ -1,3 +1,4 @@
import { UsersModule } from './users';
import { WorkspaceModule } from './workspaces';
export const BusinessModules = [WorkspaceModule];
export const BusinessModules = [WorkspaceModule, UsersModule];

View File

@@ -0,0 +1,8 @@
import { Module } from '@nestjs/common';
import { UserResolver } from './resolver';
@Module({
providers: [UserResolver],
})
export class UsersModule {}

View File

@@ -0,0 +1,37 @@
import { Args, Field, ID, ObjectType, Query, Resolver } from '@nestjs/graphql';
import type { users } from '@prisma/client';
import { PrismaService } from '../../prisma/service';
@ObjectType()
export class User implements users {
@Field(() => ID)
id!: string;
@Field({ description: 'User name' })
name!: string;
@Field({ description: 'User email' })
email!: string;
@Field({ description: 'User password', nullable: true })
password!: string;
@Field({ description: 'User avatar url', nullable: true })
avatar_url!: string;
@Field({ description: 'User token nonce', nullable: true })
token_nonce!: number;
@Field({ description: 'User created date', nullable: true })
created_at!: Date;
}
@Resolver(() => User)
export class UserResolver {
constructor(private readonly prisma: PrismaService) {}
@Query(() => User, {
name: 'user',
description: 'Get user by email',
})
async user(@Args('email') email: string) {
return this.prisma.users.findUnique({
where: { email },
});
}
}

View File

@@ -1,6 +1,10 @@
import { randomUUID } from 'node:crypto';
import {
Args,
Field,
ID,
Mutation,
ObjectType,
Query,
registerEnumType,
@@ -17,11 +21,20 @@ export enum WorkspaceType {
registerEnumType(WorkspaceType, {
name: 'WorkspaceType',
description: 'Workspace type',
valuesMap: {
Normal: {
description: 'Normal workspace',
},
Private: {
description: 'Private workspace',
},
},
});
@ObjectType()
export class Workspace implements workspaces {
@Field()
@Field(() => ID)
id!: string;
@Field({ description: 'is Public workspace' })
public!: boolean;
@@ -53,4 +66,20 @@ export class WorkspaceResolver {
where: { id },
});
}
// create workspace
@Mutation(() => Workspace, {
name: 'createWorkspace',
description: 'Create workspace',
})
async createWorkspace() {
return this.prisma.workspaces.create({
data: {
id: randomUUID(),
type: WorkspaceType.Private,
public: false,
created_at: new Date(),
},
});
}
}

View File

@@ -0,0 +1,100 @@
import { equal, ok } from 'node:assert';
import { afterEach, beforeEach, describe, test } from 'node:test';
import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import request from 'supertest';
import { AppModule } from '../app';
import { getDefaultAFFiNEConfig } from '../config/default';
const gql = '/graphql';
globalThis.AFFiNE = getDefaultAFFiNEConfig();
// please run `ts-node-esm ./scripts/init-db.ts` before running this test
describe('AppModule', () => {
let app: INestApplication;
beforeEach(async () => {
const module = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = module.createNestApplication();
await app.init();
});
afterEach(async () => {
await app.close();
});
test('should init app', async () => {
ok(typeof app === 'object');
await request(app.getHttpServer())
.post(gql)
.send({
query: `
query {
error
}
`,
})
.expect(400);
await request(app.getHttpServer())
.post(gql)
.send({
query: `
mutation {
createWorkspace {
id
type
public
created_at
}
}
`,
})
.expect(200)
.expect(res => {
ok(
typeof res.body.data.createWorkspace === 'object',
'res.body.data.createWorkspace is not an object'
);
ok(
typeof res.body.data.createWorkspace.id === 'string',
'res.body.data.createWorkspace.id is not a string'
);
ok(
typeof res.body.data.createWorkspace.type === 'string',
'res.body.data.createWorkspace.type is not a string'
);
ok(
typeof res.body.data.createWorkspace.public === 'boolean',
'res.body.data.createWorkspace.public is not a boolean'
);
ok(
typeof res.body.data.createWorkspace.created_at === 'string',
'res.body.data.createWorkspace.created_at is not a string'
);
});
});
test('should find default user', async () => {
await request(app.getHttpServer())
.post(gql)
.send({
query: `
query {
user(email: "alex.yang@example.org") {
email
avatar_url
}
}
`,
})
.expect(200)
.expect(res => {
equal(res.body.data.user.email, 'alex.yang@example.org');
});
});
});