Skip to content

Commit

Permalink
[optimze] Create unique & Query filter of Check Event (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
TechQuery authored Sep 17, 2023
1 parent c50f910 commit 2dc9f81
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 19 deletions.
60 changes: 50 additions & 10 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@

[![Deploy to Production environment](https://github.com/kaiyuanshe/KYS-service/actions/workflows/deploy-production.yml/badge.svg)][2]

## Technology stack

1. HTTP server: [Koa][3]
2. Controller framework: [Routing Controllers][4]
3. Model framework: [Class Transformer][5] & [Class Validator][6]
4. ORM framework: [TypeORM][7]
5. API document: [Swagger][8]
6. Mock API: [OpenAPI backend][9]

## API Usage

### Production environment
Expand All @@ -14,7 +23,7 @@ https://service.kaiyuanshe.cn/docs/

#### Sign in GitHub packages with NPM

1. Generate a [PAT][3] with `read:packages` authorization
1. Generate a [PAT][10] with `read:packages` authorization
2. Run Sign-in command in your terminal:

```shell
Expand All @@ -29,6 +38,17 @@ npm i pnpm -g
pnpm i @kaiyuanshe/kys-service
```

## Environment variables

| Name | Usage |
| :---------------------: | :---------------------------------------: |
| `DATABASE_URL` | [PostgreSQL][11] connection string |
| `AZURE_BLOB_CONNECTION` | [Azure Blob Storage][12] service |
| `WEB_HOOK_TOKEN` | `Authorization` token of Custom Web hooks |
| `AUTHING_APP_SECRET` | encrypt Password & Token |
| `LARK_APP_ID` | App ID of [Lark API][13] |
| `LARK_APP_SECRET` | App Secret of [Lark API][13] |

## Development

### Installation
Expand All @@ -38,30 +58,39 @@ npm i pnpm -g
pnpm i
```

### Start Dev Env
### Start Development environment

```shell
pnpm dev
```

### Database migration
or just press <kbd>F5</kbd> key in [VS Code][14].

### Migration

```shell
pnpm upgrade:dev
# or
pnpm upgrade:pro
```

### Build Docker Image
## Deployment

### Start Production environment

```shell
npm start
```

### Migration

```shell
pnpm dock
pnpm upgrade:pro
```

### Run Docker Image
### Docker

```shell
pnpm docker
pnpm pack-image
pnpm container
```

## Releasing
Expand All @@ -84,4 +113,15 @@ git push origin master --tags

[1]: https://kaiyuanshe.cn
[2]: https://github.com/kaiyuanshe/KYS-service/actions/workflows/deploy-production.yml
[3]: https://github.com/settings/tokens
[3]: https://koajs.com/
[4]: https://github.com/typestack/routing-controllers
[5]: https://github.com/typestack/class-transformer
[6]: https://github.com/typestack/class-validator
[7]: https://typeorm.io/
[8]: https://swagger.io/
[9]: https://github.com/anttiviljami/openapi-backend
[10]: https://github.com/settings/tokens
[11]: https://www.postgresql.org/
[12]: https://azure.microsoft.com/en-us/products/storage/blobs
[13]: https://open.feishu.cn/
[14]: https://code.visualstudio.com/
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kys-service",
"version": "0.6.0",
"version": "0.6.1",
"license": "AGPL-3.0",
"author": "[email protected]",
"description": "RESTful API service of KaiYuanShe",
Expand Down Expand Up @@ -78,7 +78,7 @@
"migration:run": "npm run typeorm -- migration:run",
"upgrade:dev": "npm run migration:generate -- migration/version && npm run migration:run",
"upgrade:pro": "cross-env NODE_ENV=production npm run migration:generate -- .tmp/version && npm run migration:run",
"dock": "docker build . -t kys-service:latest",
"docker": "docker rm -f kys-service && docker run --name kys-service -p 8080:8080 -d kys-service"
"pack-image": "docker build . -t kys-service:latest",
"container": "docker rm -f kys-service && docker run --name kys-service -p 8080:8080 -d kys-service"
}
}
17 changes: 13 additions & 4 deletions src/controller/CheckEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
Authorized,
Body,
CurrentUser,
ForbiddenError,
Get,
JsonController,
Post,
Expand All @@ -10,9 +11,9 @@ import {
import { ResponseSchema } from 'routing-controllers-openapi';

import {
BaseFilter,
CheckEvent,
CheckEventChunk,
CheckEventFilter,
CheckEventInput,
User,
dataSource
Expand All @@ -25,7 +26,14 @@ export class CheckEventController {
@Post()
@Authorized()
@ResponseSchema(CheckEvent)
createOne(@CurrentUser() createdBy: User, @Body() data: CheckEventInput) {
async createOne(
@CurrentUser() createdBy: User,
@Body() data: CheckEventInput
) {
const oldOne = await this.store.find({ where: { createdBy, ...data } });

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

return this.store.save({ ...data, createdBy });
}

Expand All @@ -34,10 +42,11 @@ export class CheckEventController {
@ResponseSchema(CheckEventChunk)
async getSessionList(
@CurrentUser() createdBy: User,
@QueryParams() { pageSize, pageIndex }: BaseFilter
@QueryParams()
{ activityId, agendaId, pageSize, pageIndex }: CheckEventFilter
) {
const [list, count] = await this.store.findAndCount({
where: { createdBy },
where: { createdBy, activityId, agendaId },
skip: pageSize * (pageIndex - 1),
take: pageSize
});
Expand Down
15 changes: 14 additions & 1 deletion src/model/CheckEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from 'class-validator';
import { Column, Entity } from 'typeorm';

import { ListChunk } from './Base';
import { BaseFilter, ListChunk } from './Base';
import { UserBase, UserInputData } from './User';

@Entity()
Expand Down Expand Up @@ -54,6 +54,19 @@ export class CheckEventInput implements UserInputData<CheckEvent> {
agendaTitle: string;
}

export class CheckEventFilter
extends BaseFilter
implements Partial<BaseFilter & CheckEventInput>
{
@IsString()
@IsOptional()
activityId?: string;

@IsString()
@IsOptional()
agendaId?: string;
}

export class CheckEventChunk implements ListChunk<CheckEvent> {
@IsInt()
@Min(0)
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.0",
"version": "0.6.1",
"types": "index.d.ts",
"dependencies": {
"@types/jsonwebtoken": "^9.0.3",
Expand Down

0 comments on commit 2dc9f81

Please sign in to comment.