Skip to content

Feature/account #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"dev": "turbo run dev --no-cache --parallel --continue",
"lint": "turbo run lint",
"clean": "turbo run clean",
"clean-dep": "turbo run clean-dep && rimraf node_modules",
"format": "prettier --write \"**/*.{ts,tsx,md}\"",
"nuke": "docker-compose down -v && sh init_minio.sh && docker-compose up -d"
},
Expand Down
1 change: 1 addition & 0 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"prebuild": "npm run clean",
"build": "tsc -p tsconfig.json",
"clean": "rimraf dist",
"clean-dep": "rimraf node_modules",
"format": "prettier --write",
"lint": "eslint --fix"
},
Expand Down
229 changes: 229 additions & 0 deletions packages/api/src/account.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
import { Controller, Get, Logger, Query, Post, Body, Headers } from '@nestjs/common';
import { PrismaService, WechatApp, WecomApp } from '@senses-chat/operator-database';

@Controller('/api/account')
export class AccountApiController {
private readonly logger = new Logger(AccountApiController.name);

constructor(private readonly prisma: PrismaService) {}

@Get('/wechat')
async getWechatAppList(
@Query('skip') skip: number,
@Query('size') size: number,
@Headers('NEXT_AUTH_USER_ID') userId: string,
): Promise<WechatApp[]> {
const wechatApps = await this.prisma.wechatApp.findMany({
skip: +skip || 0,
take: +size || 10,
where: {
userId,
}
});
return wechatApps.map((item) => ({
...item,
appSecret: ''
}));
}

@Get('/wechat/count')
async getWechatAppCount(
@Headers('NEXT_AUTH_USER_ID') userId: string,
): Promise<number> {
const count = await this.prisma.wechatApp.count({
where: {
userId,
}
});
return count;
}

@Post('/wechat/remove')
async deleteWechatApp(
@Body('id') id: number,
@Headers('NEXT_AUTH_USER_ID') userId: string,
): Promise<boolean> {
if (!(await this.prisma.wechatApp.findFirst({
where: {
id,
userId,
}
}))) {
return true;
}

const wechatApp = await this.prisma.wechatApp.delete({
where: {
id,
}
});
return !!wechatApp;
}

@Post('/wechat/update')
async updateWechatApp(
@Body('id') id: number,
@Body('name') name: string,
@Body('appId') appId: string,
@Body('appSecret') appSecret: string,
@Body('token') token: string,
@Body('aesKey') aesKey: string,
@Headers('NEXT_AUTH_USER_ID') userId: string,
): Promise<WechatApp> {
if (!(await this.prisma.wechatApp.findFirst({
where: {
id,
userId,
}
}))) {
return null;
}

const wechatApp = await this.prisma.wechatApp.update({
where: {
id,
},
data: {
name,
appId,
appSecret: appSecret || undefined,
token,
aesKey,
updatedAt: new Date(),
}
});
return wechatApp;
}

@Post('/wechat/create')
async createWechatApp(
@Body('name') name: string,
@Body('appId') appId: string,
@Body('appSecret') appSecret: string,
@Body('token') token: string,
@Body('aesKey') aesKey: string,
@Headers('NEXT_AUTH_USER_ID') userId: string,
): Promise<WechatApp> {
const wechatApp = await this.prisma.wechatApp.create({
data: {
name,
appId,
appSecret: appSecret || undefined,
token,
aesKey,
userId,
}
});
return wechatApp;
}

@Get('/wecom')
async getWecomAppList(
@Query('skip') skip: number,
@Query('size') size: number,
@Headers('NEXT_AUTH_USER_ID') userId: string,
): Promise<WecomApp[]> {
const wecomApps = await this.prisma.wecomApp.findMany({
skip: +skip || 0,
take: +size || 10,
where: {
userId,
}
});
return wecomApps.map((item) => ({
...item,
corpSecret: ''
}));
}

@Get('/wecom/count')
async getWecomAppCount(
@Headers('NEXT_AUTH_USER_ID') userId: string,
): Promise<number> {
const count = await this.prisma.wecomApp.count({
where: {
userId,
}
});
return count;
}

@Post('/wecom/remove')
async deleteWecomApp(
@Body('id') id: number,
@Headers('NEXT_AUTH_USER_ID') userId: string,
): Promise<boolean> {
if (!(await this.prisma.wecomApp.findFirst({
where: {
id,
userId,
}
}))) {
return true;
}

const wecomApp = await this.prisma.wecomApp.delete({
where: {
id,
}
});
return !!wecomApp;
}

@Post('/wecom/update')
async updateWecomApp(
@Body('id') id: number,
@Body('name') name: string,
@Body('corpId') corpId: string,
@Body('corpSecret') corpSecret: string,
@Body('token') token: string,
@Body('aesKey') aesKey: string,
@Headers('NEXT_AUTH_USER_ID') userId: string,
): Promise<WecomApp> {
if (!(await this.prisma.wecomApp.findFirst({
where: {
id,
userId,
}
}))) {
return null;
}

const wecomApp = await this.prisma.wecomApp.update({
where: {
id,
},
data: {
name,
corpId,
corpSecret: corpSecret || undefined,
token,
aesKey,
updatedAt: new Date(),
}
});
return wecomApp;
}

@Post('/wecom/create')
async createWecomApp(
@Body('name') name: string,
@Body('corpId') corpId: string,
@Body('corpSecret') corpSecret: string,
@Body('token') token: string,
@Body('aesKey') aesKey: string,
@Headers('NEXT_AUTH_USER_ID') userId: string,
): Promise<WecomApp> {
const wecomApp = await this.prisma.wecomApp.create({
data: {
name,
corpId,
corpSecret: corpSecret || undefined,
token,
aesKey,
userId,
}
});
return wecomApp;
}
}
8 changes: 7 additions & 1 deletion packages/api/src/api.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import apiConfig from './config';
import { WxkfApiController } from './wxkf.controller';
import { HistoryController } from './history.controller';
import { BotConfigApiController } from './bot-config.controller';
import { AccountApiController } from './account.controller';

@Module({
imports: [
Expand All @@ -20,6 +21,11 @@ import { BotConfigApiController } from './bot-config.controller';
PrismaModule,
ConfigModule.forFeature(apiConfig),
],
controllers: [WxkfApiController, HistoryController, BotConfigApiController],
controllers: [
WxkfApiController,
HistoryController,
AccountApiController,
BotConfigApiController
],
})
export class ApiModule {}
Loading