Skip to content

Commit b24451a

Browse files
committed
fix: auto schedule export members
1 parent 5907022 commit b24451a

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

src/exports/exports-members.service.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { InjectQueue } from '@nestjs/bull';
1515
import { Inject, Injectable } from '@nestjs/common';
1616
import { Queue } from 'bull';
1717
import { sum } from 'lodash';
18+
import moment from 'moment';
1819
import { Db } from 'mongodb';
1920
import { Collections, MONGODB_TOKEN, PlayerLinksEntity } from '../db';
2021
import { ExportMembersInput, ExportSheetInputDto } from './dto';
@@ -231,7 +232,9 @@ export class ExportsMembersService {
231232
clanTags: input.clanTags,
232233
sheets,
233234
guildId: input.guildId,
234-
label: 'Clan Members',
235+
label: input.scheduled
236+
? `Clan Members [${moment().format('YYYY-MM-DD HH:mm')}]`
237+
: 'Clan Members',
235238
scheduled: input.scheduled,
236239
sheetType: SheetType.CLAN_MEMBERS,
237240
});

src/exports/exports.service.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Db } from 'mongodb';
88
import { Collections, MONGODB_TOKEN, RedisService } from '../db';
99
import { ExportMembersInput } from './dto';
1010
import { ExportsMembersService } from './exports-members.service';
11+
import { ReusableSheetService, SheetType } from './services/reusable-sheet.service';
1112

1213
@Injectable()
1314
export class ExportsService {
@@ -17,6 +18,7 @@ export class ExportsService {
1718
private clashClientService: ClashClientService,
1819
private redisService: RedisService,
1920
private exportsMembersService: ExportsMembersService,
21+
private reusableSheetService: ReusableSheetService,
2022
) {}
2123

2224
@CronTab('55 4 * * 0', {
@@ -34,6 +36,14 @@ export class ExportsService {
3436
}
3537

3638
async exportClanMembers(input: ExportMembersInput) {
39+
const scheduled = await this.reusableSheetService.getSheet({
40+
scheduled: true,
41+
clanTags: input.clanTags,
42+
guildId: input.guildId,
43+
sheetType: SheetType.CLAN_MEMBERS,
44+
});
45+
if (scheduled) return scheduled;
46+
3747
return this.exportsMembersService.exportClanMembers(input);
3848
}
3949

src/exports/services/reusable-sheet.service.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,19 @@ export class ReusableSheetService {
4747
if (sheet) {
4848
try {
4949
spreadsheet = await this.googleSheetService.updateGoogleSheet(sheet.spreadsheetId, sheets, {
50-
title: `[${label}]`,
50+
title: `${label}`,
5151
clear: true,
5252
recreate: sheet && sheetHash !== sheet.sheetHash,
5353
});
5454
} catch (error) {
5555
if (/invalid requests/i.test(error.message)) {
56-
spreadsheet = await this.googleSheetService.createGoogleSheet(`[${label}]`, sheets);
56+
spreadsheet = await this.googleSheetService.createGoogleSheet(`${label}`, sheets);
5757
} else {
5858
throw error;
5959
}
6060
}
6161
} else {
62-
spreadsheet = await this.googleSheetService.createGoogleSheet(`[${label}]`, sheets);
62+
spreadsheet = await this.googleSheetService.createGoogleSheet(`${label}`, sheets);
6363
}
6464

6565
await this.googleSheets.updateOne(
@@ -90,10 +90,30 @@ export class ReusableSheetService {
9090
};
9191
}
9292

93+
public async getSheet(input: {
94+
guildId: string;
95+
clanTags: string[];
96+
sheetType: SheetType;
97+
scheduled: boolean;
98+
}) {
99+
if (!input.scheduled) return null;
100+
101+
const sheet = await this.googleSheets.findOne({
102+
hash: this.createSpreadsheetHash(input),
103+
scheduled: true,
104+
});
105+
if (!sheet) return null;
106+
107+
return {
108+
spreadsheetUrl: `https://docs.google.com/spreadsheets/d/${sheet.spreadsheetId}`,
109+
spreadsheetId: sheet.spreadsheetId,
110+
};
111+
}
112+
93113
private createSpreadsheetHash(input: {
94114
guildId: string;
95115
clanTags: string[];
96-
sheetType: string;
116+
sheetType: SheetType;
97117
scheduled: boolean;
98118
}) {
99119
const { guildId, clanTags, sheetType, scheduled } = input;

0 commit comments

Comments
 (0)