forked from wangziqi/gongxue-base
fix: switch DingTalk attendance API from getcolumnval to listRecord
This commit is contained in:
@@ -2,15 +2,17 @@ import { Module } from '@nestjs/common';
|
||||
import { ScheduleModule } from '@nestjs/schedule';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { IntegrationModule } from '../integration/integration.module';
|
||||
import { SyncLog, SyncState } from '../entities';
|
||||
import { AttendanceModule } from '../attendance/attendance.module';
|
||||
import { SyncLog, SyncState, UserDingMapping } from '../entities';
|
||||
import { SyncService } from './sync.service';
|
||||
import { SyncController } from './sync.controller';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
ScheduleModule.forRoot(),
|
||||
TypeOrmModule.forFeature([SyncLog, SyncState]),
|
||||
TypeOrmModule.forFeature([SyncLog, SyncState, UserDingMapping]),
|
||||
IntegrationModule,
|
||||
AttendanceModule,
|
||||
],
|
||||
controllers: [SyncController],
|
||||
providers: [SyncService],
|
||||
|
||||
@@ -2,22 +2,25 @@ import { Injectable, Logger } from '@nestjs/common';
|
||||
import { Cron } from '@nestjs/schedule';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { SyncLog, SyncState } from '../entities';
|
||||
import { SyncLog, SyncState, UserDingMapping } from '../entities';
|
||||
import type { SyncPlatform, SyncType, SyncStatus } from '../entities/sync-log.entity';
|
||||
import { DingTalkService } from '../integration/dingtalk.service';
|
||||
import { WeComService } from '../integration/wecom.service';
|
||||
import { AttendanceImportService } from '../attendance/attendance-import.service';
|
||||
|
||||
@Injectable()
|
||||
export class SyncService {
|
||||
private readonly logger = new Logger(SyncService.name);
|
||||
|
||||
constructor(
|
||||
@InjectRepository(SyncLog)
|
||||
private readonly syncLogRepo: Repository<SyncLog>,
|
||||
@InjectRepository(SyncState)
|
||||
private readonly syncStateRepo: Repository<SyncState>,
|
||||
@InjectRepository(UserDingMapping)
|
||||
private readonly mappingRepo: Repository<UserDingMapping>,
|
||||
private readonly dingTalkService: DingTalkService,
|
||||
private readonly weComService: WeComService,
|
||||
private readonly attendanceImportService: AttendanceImportService,
|
||||
) {}
|
||||
|
||||
// ── Scheduled cron: daily at 2 AM ──
|
||||
@@ -151,9 +154,44 @@ export class SyncService {
|
||||
await this.syncLogRepo.save(log);
|
||||
}
|
||||
|
||||
private async performDingTalkSync(_lastSyncAt: Date | null): Promise<number> {
|
||||
private async performDingTalkSync(lastSyncAt: Date | null): Promise<number> {
|
||||
// Stage 1: Sync departments and users
|
||||
const result = await this.dingTalkService.syncAll();
|
||||
return result.deptCount + result.userCount;
|
||||
let total = result.deptCount + result.userCount;
|
||||
|
||||
// Stage 2: Import attendance data (last 7 days or since last sync)
|
||||
try {
|
||||
const endDate = new Date();
|
||||
const startDate = new Date();
|
||||
// If never synced, import last 7 days; otherwise import since last sync
|
||||
if (lastSyncAt) {
|
||||
startDate.setTime(lastSyncAt.getTime());
|
||||
} else {
|
||||
startDate.setDate(startDate.getDate() - 7);
|
||||
}
|
||||
|
||||
const start = startDate.toISOString().slice(0, 10);
|
||||
const end = endDate.toISOString().slice(0, 10);
|
||||
|
||||
this.logger.log(`Importing DingTalk attendance: ${start} ~ ${end}`);
|
||||
const mappings = await this.mappingRepo.find();
|
||||
const userIds = mappings.map((m) => m.dingUserId).filter(Boolean);
|
||||
const importResult = await this.attendanceImportService.importFromDingTalk({
|
||||
startDate: start,
|
||||
endDate: end,
|
||||
userIds: userIds.length > 0 ? userIds : undefined,
|
||||
autoMatch: true,
|
||||
});
|
||||
|
||||
total += importResult.imported;
|
||||
this.logger.log(`DingTalk attendance import: ${importResult.imported} imported, ${importResult.skipped} skipped`);
|
||||
} catch (err: unknown) {
|
||||
// Attendance import failure should not block the sync
|
||||
const msg = err instanceof Error ? err.message : String(err);
|
||||
this.logger.warn(`DingTalk attendance import failed (non-fatal): ${msg}`);
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
private async performWeComSync(_lastSyncAt: Date | null): Promise<number> {
|
||||
|
||||
Reference in New Issue
Block a user