Skip to content

Commit

Permalink
[add] User controller
Browse files Browse the repository at this point in the history
[add] Debugger configuration of VS Code
[optimize] replace Mobile Phone with User ID on Event Checking
[fix] some User detail bugs
  • Loading branch information
TechQuery committed Oct 1, 2023
1 parent 4f8b771 commit 2bd2790
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 28 deletions.
14 changes: 14 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch Server",
"type": "node",
"request": "launch",
"runtimeExecutable": "npm",
"runtimeArgs": ["run", "dev"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}
22 changes: 11 additions & 11 deletions src/controller/CheckEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,20 @@ export class CheckEventController {
@ResponseSchema(CheckEvent)
async createOne(
@CurrentUser() createdBy: User,
@Body() { mobilePhone, ...data }: CheckEventInput
@Body() { user, ...data }: CheckEventInput
) {
const user = await this.userStore.findOne({ where: { mobilePhone } });
if (createdBy.id === user.id)
throw new ForbiddenError('No self-checking');

if (!user) throw new BadRequestError('Invalid user: ' + mobilePhone);
user = await this.userStore.findOne({ where: user });

const oldOne = await this.store.findOne({
if (!user) throw new BadRequestError('Invalid user: ' + user.id);

const checked = await this.store.findOne({
where: { ...data, user: { id: user.id } }
});

if (oldOne) throw new ForbiddenError('No duplicated check');
if (checked) throw new ForbiddenError('No duplicated check');

return this.store.save({ ...data, createdBy, user });
}
Expand All @@ -50,19 +53,16 @@ export class CheckEventController {
async getSessionList(
@QueryParams()
{
mobilePhone,
user,
activityId,
agendaId,
pageSize = 10,
pageIndex = 1
}: CheckEventFilter
) {
const [list, count] = await this.store.findAndCount({
where: {
user: { mobilePhone },
activityId,
agendaId
},
where: { user, activityId, agendaId },
relations: ['user'],
skip: pageSize * (pageIndex - 1),
take: pageSize
});
Expand Down
17 changes: 12 additions & 5 deletions src/controller/Session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@ export class SessionController {
return { ...user, token: sign({ ...user }, AUTHING_APP_SECRET) };
}

static fixPhoneNumber(raw: string) {
return raw.startsWith('+') ? raw : `+86${raw}`;
}

static getAuthingUser(token: string): AuthingSession {
var { phone_number, ...session } = verify(
token.split(/\s+/)[1],
AUTHING_APP_SECRET
) as AuthingSession;

if (phone_number && !phone_number.startsWith('+86'))
phone_number = '+86' + phone_number;
if (phone_number) phone_number = this.fixPhoneNumber(phone_number);

return { ...session, phone_number };
}
Expand All @@ -50,9 +53,11 @@ export class SessionController {
if (!user) return;

if ('userpool_id' in user)
return dataSource
.getRepository(User)
.findOne({ where: { mobilePhone: user.phone_number } });
return dataSource.getRepository(User).findOne({
where: {
mobilePhone: this.fixPhoneNumber(user.phone_number)
}
});

delete user.iat;

Expand All @@ -72,6 +77,7 @@ export class SessionController {
@HeaderParam('Authorization', { required: true }) token: string
) {
const {
sub,
phone_number: mobilePhone,
nickname,
picture
Expand All @@ -81,6 +87,7 @@ export class SessionController {

const registered = await SessionController.register(this.store, {
...existed,
uuid: sub,
mobilePhone,
nickName: nickname,
avatar: picture
Expand Down
16 changes: 16 additions & 0 deletions src/controller/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Get, JsonController, OnNull, Param } from 'routing-controllers';
import { ResponseSchema } from 'routing-controllers-openapi';

import { User, dataSource } from '../model';

@JsonController('/user')
export class UserController {
store = dataSource.getRepository(User);

@Get('/:uuid')
@OnNull(404)
@ResponseSchema(User)
getOne(@Param('uuid') uuid: string) {
return this.store.findOne({ where: { uuid } });
}
}
8 changes: 7 additions & 1 deletion src/controller/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ import { isProduct } from '../utility';
import { CheckEventController } from './CheckEvent';
import { CrawlerController } from './Crawler';
import { SessionController } from './Session';
import { UserController } from './User';

export const { router, swagger, mocker } = createAPI({
mock: !isProduct,
controllers: [SessionController, CheckEventController, CrawlerController]
controllers: [
SessionController,
UserController,
CheckEventController,
CrawlerController
]
});
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ useKoaServer(app, {
!!(await SessionController.getSession(action))
);
},
currentUserChecker: SessionController.getSession
currentUserChecker: action => SessionController.getSession(action)
});

console.time('Server boot');
Expand Down
4 changes: 4 additions & 0 deletions src/model/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import {
} from 'typeorm';

export abstract class Base {
constructor(id: number) {
this.id = id;
}

@IsInt()
@PrimaryGeneratedColumn()
id: number;
Expand Down
17 changes: 8 additions & 9 deletions src/model/CheckEvent.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Type } from 'class-transformer';
import { Transform, Type } from 'class-transformer';
import {
IsInt,
IsLatLong,
IsMobilePhone,
IsOptional,
IsString,
Min,
Expand Down Expand Up @@ -42,15 +41,14 @@ export class CheckEvent extends UserBase {
agendaTitle: string;
}

export class CheckEventInput
implements Omit<UserInputData<CheckEvent>, 'user'>
{
export class CheckEventInput implements UserInputData<CheckEvent> {
@IsLatLong()
@IsOptional()
coordinate?: string;

@IsMobilePhone()
mobilePhone: string;
@Type(() => User)
@Transform(({ value }) => new User(value))
user: User;

@IsString()
activityId: string;
Expand All @@ -69,9 +67,10 @@ export class CheckEventFilter
extends BaseFilter
implements Partial<BaseFilter & CheckEventInput>
{
@IsMobilePhone()
@Type(() => User)
@Transform(({ value }) => new User(value))
@IsOptional()
mobilePhone?: string;
user?: User;

@IsString()
@IsOptional()
Expand Down
4 changes: 4 additions & 0 deletions src/model/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export enum Gender {

@Entity()
export class User extends Base {
@IsString()
@Column({ unique: true })
uuid: string;

@IsMobilePhone()
@Column({ unique: true })
mobilePhone: string;
Expand Down
2 changes: 1 addition & 1 deletion type/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kaiyuanshe/kys-service",
"version": "0.6.5",
"version": "0.6.7",
"types": "index.d.ts",
"files": [
"*.d.ts"
Expand Down

0 comments on commit 2bd2790

Please sign in to comment.