Skip to content

Commit

Permalink
Merge pull request #13 from graywenn/feature/ui2.1
Browse files Browse the repository at this point in the history
Feature/ui2.1
  • Loading branch information
greywen authored Jul 15, 2022
2 parents e315257 + 92a7017 commit 23a9f2a
Show file tree
Hide file tree
Showing 34 changed files with 681 additions and 158 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"dependencies": {
"@alicloud/pop-core": "^1.7.10",
"@keycloak/keycloak-admin-client": "^18.0.2",
"@nestjs-modules/ioredis": "^1.0.0",
"@nestjs/common": "^8.0.0",
"@nestjs/core": "^8.0.0",
Expand All @@ -36,6 +37,7 @@
"ioredis": "^5.0.5",
"jsonwebtoken": "^8.5.1",
"moment": "^2.29.3",
"node-keycloak": "^0.1.5",
"openid-client": "^5.1.6",
"passport": "^0.5.2",
"passport-jwt": "^4.0.0",
Expand Down
7 changes: 6 additions & 1 deletion src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
import { UserService } from './services';

@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
constructor(
private readonly appService: AppService,
private readonly userService: UserService,
) {}

@Get('checkHealth')
async checkHealth(): Promise<string> {
// await this.userService.generateUserDepartment();
return this.appService.checkHealth();
}
}
40 changes: 26 additions & 14 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { RedisModule } from '@nestjs-modules/ioredis';
import { ScheduleModule } from '@nestjs/schedule';
import { JwtModule } from '@nestjs/jwt';
import { PassportModule } from '@nestjs/passport';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import {
Expand All @@ -13,36 +14,44 @@ import {
UserController,
InformController,
} from './controllers';
import { jwtModuleOptions, redisModuleOptions } from './modules';
import {
jwtModuleOptions,
redisModuleOptions,
typeOrmOptions,
} from './modules';
import { TimeSheetSocket } from './sockets';
import { TimeSheetSchedule } from './schedules';
import { TimeSheetSchedule, KeyCloakSchedule } from './schedules';
import {
AttendanceService,
AuthService,
DingTalkService,
ReportService,
UserService,
InformService,
TimeSheetService,
UserDepartmentService,
} from './services';
import { JwtStrategy, WsGuard } from './strategys';
import { TypeOrmModule } from '@nestjs/typeorm';
import { join } from 'path';
import config from '@config/config';
import { UserTimesheet } from './entities/timesheet.enetity';
import {
DataResource,
DataDepartment,
UserTimesheet,
UserDepartment,
} from './entities';

@Module({
imports: [
PassportModule,
JwtModule.register(jwtModuleOptions),
RedisModule.forRoot(redisModuleOptions),
ScheduleModule.forRoot(),
TypeOrmModule.forRoot({
type: 'postgres',
...config.postgresql,
entities: [join(__dirname, '**', '*.entity.{ts,js}')],
migrationsTableName: 'migration',
migrations: ['src/migration/*.ts'],
}),
TypeOrmModule.forFeature([UserTimesheet]),
TypeOrmModule.forRoot(typeOrmOptions),
TypeOrmModule.forFeature([
DataResource,
DataDepartment,
UserTimesheet,
UserDepartment,
]),
],
controllers: [
AppController,
Expand All @@ -63,8 +72,11 @@ import { UserTimesheet } from './entities/timesheet.enetity';
AuthService,
UserService,
InformService,
TimeSheetService,
UserDepartmentService,
TimeSheetSocket,
TimeSheetSchedule,
KeyCloakSchedule,
],
exports: [AuthService],
})
Expand Down
23 changes: 8 additions & 15 deletions src/config/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ServerEnvironment } from '@constants/server';
import { GrantTypes } from '@keycloak/keycloak-admin-client/lib/utils/auth';
import * as config from 'config';
interface IConfig {
server: {
Expand Down Expand Up @@ -31,6 +32,7 @@ interface IConfig {
attendanceRule: string;
reportRule: string;
saveTimeSheetRule: string;
keyCloakAuthRule: string;
};
redis: {
host: string;
Expand All @@ -46,6 +48,10 @@ interface IConfig {
scope: string;
grantType: string;
logoutRedirectUri: string;
clientBaseUrl: string;
username: string;
password: string;
clientGrantType: GrantTypes;
};
jwt: {
secret: string;
Expand Down Expand Up @@ -89,26 +95,13 @@ export default <IConfig>{
code: config.get('smsTemplate.code'),
signName: config.get('smsTemplate.signName'),
},
job: {
attendanceRule: config.get('job.attendanceRule'),
reportRule: config.get('job.reportRule'),
saveTimeSheetRule: config.get('job.saveTimeSheetRule'),
},
job: config.get('job'),
redis: {
host: config.get('redis.host'),
port: config.get('redis.port'),
password: config.get('redis.password'),
},
keycloak: {
realm: config.get('keycloak.realm'),
issuer: config.get('keycloak.issuer'),
clientId: config.get('keycloak.clientId'),
redirectUri: config.get('keycloak.redirectUri'),
scope: config.get('keycloak.scope'),
clientSecret: config.get('keycloak.clientSecret'),
grantType: config.get('keycloak.grantType'),
logoutRedirectUri: config.get('keycloak.logoutRedirectUri'),
},
keycloak: config.get('keycloak'),
jwt: {
secret: config.get('jwt.secret'),
expiresIn: config.get('jwt.expiresIn'),
Expand Down
2 changes: 2 additions & 0 deletions src/constants/dingTalk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export enum LeaveDurationUnitType {
* 未提交日志 X 6
* 加班 J 7
* 迟到 L 8
* 打卡时间 A 9
*/
export enum AttendanceState {
'O' = 1,
Expand All @@ -26,6 +27,7 @@ export enum AttendanceState {
'X' = 6,
'J' = 7,
'L' = 8,
'A' = 9,
}

export enum AttendanceCheckType {
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ import {
UseGuards,
Request,
} from '@nestjs/common';
import KcClient from '@utils/kcClient';
import { AuthService } from '@services/auth.service';
import { AuthGuard } from '@nestjs/passport';
import { NestRes } from '@interfaces/nestbase';
import NodeKeycloak from 'node-keycloak';

@Controller('auth')
export class AuthController {
constructor(private readonly authService: AuthService) {}
@Get('/url')
async getAuthUrl() {
return await KcClient.client.authorizationUrl();
return await NodeKeycloak.authorizationUrl();
}

@Post('/authentication')
async authentication(@Body() authDto: IAuthDto) {
const { code, state, session_state } = authDto;
return await this.authService.signin(code, state, session_state);
const { code, session_state } = authDto;
return await this.authService.signin(code, session_state);
}

@UseGuards(AuthGuard('jwt'))
Expand Down
13 changes: 6 additions & 7 deletions src/controllers/dingTalk.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import config from '@config/config';
import FileData from '@core/files.data';
import {
ICreateReportDto,
IGetReportTemplateByNameDto,
IUserCreateDto,
IUserUpdateDto,
} from '@dtos/dingTlak';
Expand Down Expand Up @@ -144,7 +143,7 @@ export class DingTalkController {
async createReport(@Body() Body: ICreateReportDto, @Request() req: NestRes) {
const templeDetail = await this.dingTalkService.getReportTemplateByName({
template_name: 'TIMESHEET',
userid: req.user.dingTalkUserId,
userid: req.user.dingUserId,
});
const contents = [];
contents[0] = {
Expand Down Expand Up @@ -197,7 +196,7 @@ export class DingTalkController {
to_chat: false,
to_cids: [config.dingTalk.conversationId],
dd_from: 'fenglin',
userid: req.user.dingTalkUserId,
userid: req.user.dingUserId,
};
templeDetail.result?.default_receivers &&
(params.to_userids = templeDetail.result.default_receivers.map((item) => {
Expand All @@ -213,14 +212,14 @@ export class DingTalkController {

@Get('/getReportTemplateByName')
async getReportTemplateByName(@Request() req: NestRes) {
const { dingTalkUserId } = req.user;
const { dingUserId } = req.user;
const _timesheet = await this.redis.get('timesheets');
const datas = <ITimeSheet[]>JSON.parse(_timesheet || '[]');
const userTimeSheet = datas.find((x) => x.userid === dingTalkUserId);
const userTimeSheet = datas.find((x) => x.userid === dingUserId);

const result = await this.dingTalkService.getReportTemplateByName({
template_name: 'TIMESHEET',
userid: dingTalkUserId,
userid: dingUserId,
});
result.result.value = userTimeSheet?.value;
return result.result;
Expand All @@ -232,7 +231,7 @@ export class DingTalkController {
start_time: moment().startOf('day').format('x'),
end_time: moment().endOf('day').format('x'),
template_name: 'TIMESHEET',
userid: req.user.dingTalkUserId,
userid: req.user.dingUserId,
cursor: 0,
size: 1,
});
Expand Down
11 changes: 7 additions & 4 deletions src/controllers/timesheet.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ import { Body, Controller, Get, Param, Put, UseGuards } from '@nestjs/common';
import FileData from '@core/files.data';
import { ITimeSheet } from '@interfaces/timesheet';
import { InjectRedis, Redis } from '@nestjs-modules/ioredis';
import { AuthGuard } from '@nestjs/passport';
import * as moment from 'moment';
import { AuthGuard } from '@nestjs/passport';
import { TimeSheetService } from '@services/timesheet.service';

@UseGuards(AuthGuard('jwt'))
@Controller('timesheet')
export class TimeSheetController {
constructor(@InjectRedis() private readonly redis: Redis) {}
constructor(
@InjectRedis() private readonly redis: Redis,
private readonly timesheetService: TimeSheetService,
) {}
@Get('/get/:dept_name/:date')
async getTimesheet(
@Param('dept_name') dept_name: string,
Expand All @@ -23,8 +27,7 @@ export class TimeSheetController {
datas = <ITimeSheet[]>JSON.parse(_timesheet || '[]');
} else {
try {
const result = await FileData.readTimeSheet(date);
datas = <ITimeSheet[]>JSON.parse(result).users;
datas = await this.timesheetService.getTimeSheetByDate(date);
} catch (err) {}
}
const timeSheetData = users
Expand Down
85 changes: 83 additions & 2 deletions src/controllers/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
import { NestRes } from '@interfaces/nestbase';
import { Controller, Get, UseGuards, Request } from '@nestjs/common';
import {
Controller,
Get,
UseGuards,
Request,
Param,
Put,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { UserService } from '@services/user.service';
import FileData from '@core/files.data';
import * as moment from 'moment';
import { UserDepartmentService } from '@services/department.service';
import { IUserMemberDto } from '@dtos/user';

@UseGuards(AuthGuard('jwt'))
@Controller('user')
export class UserController {
constructor(private readonly userService: UserService) {}
constructor(
private readonly userService: UserService,
private readonly userDepartmentService: UserDepartmentService,
) {}
@Get('today')
async getTodayInfo(@Request() req: NestRes) {
return {
Expand All @@ -16,4 +30,71 @@ export class UserController {
),
};
}

@Get('members')
async getUserMembers(@Request() req: NestRes): Promise<IUserMemberDto[]> {
const departmentMembers =
await this.userDepartmentService.getDepartmentMembers(
req.user.departmentIds,
);
return departmentMembers.map((x) => {
return <IUserMemberDto>{
username: x.username,
avatar: x.attributes.avatar && x.attributes.avatar[0],
};
});
}

@Get('attendance/:month')
async getUserAttendance(
@Param('month') month: string,
@Request() req: NestRes,
) {
if (
moment().diff(moment(month), 'months') === 1 ||
moment().format('YYYY-MM') === moment(month).format('YYYY-MM')
) {
const _month = moment(month).format('YYYY-MM');
const dingUserId = req.user.dingUserId;
const dingdingAttendances = await FileData.readAttendances(_month);
const customAttendances = await FileData.readCustomAttendances(_month);
const dAttendances = dingdingAttendances.find((x) => x.id === dingUserId);
const cAttendances = customAttendances.find((x) => x.id === dingUserId);
const attendances = dAttendances?.attendances.map((x, index) => {
const value = cAttendances.attendances[index];
if (value !== null) x = value;
return x;
});
return attendances || [];
}
return [];
}

@Put('resource')
async updateUser() {
const users = await this.userService.getUsers();
for (const user of users) {
let resourceIds = '';
if (user.attributes['departmentids'].includes('1')) {
resourceIds = '1,2';
} else if (
user.attributes['departmentids'].includes('2') ||
user.attributes['departmentids'].includes('3')
) {
resourceIds = '1';
} else if (
user.attributes['departmentids'].includes('4') ||
user.attributes['departmentids'].includes('5')
) {
resourceIds = '1,3';
} else {
resourceIds = '1,2,3';
}
// await this.userService.updateUserResource(user.id, resourceIds);
}
}
// @Get('users')
// async getUsers() {

// }
}
Loading

0 comments on commit 23a9f2a

Please sign in to comment.