Skip to content

Commit

Permalink
refactor(domain): move usecase methods to entities (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
guiseek authored Jun 8, 2023
1 parent 157b51f commit e47da20
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 51 deletions.
19 changes: 19 additions & 0 deletions libs/domain/src/lib/entities/date-rule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Frequency, Options, RRule } from 'rrule';
import { Schedule } from './schedule';

export class DateRule {
constructor(public schedule: Schedule) {}

getInterval(start: Date, end: Date) {
const { interval, byweekday } = this.schedule;

const dtstart = new Date(start);
const until = new Date(end);

return { interval, byweekday, dtstart, until };
}

getDates(options: Partial<Options>) {
return new RRule({ ...options, freq: Frequency.WEEKLY }).all();
}
}
2 changes: 2 additions & 0 deletions libs/domain/src/lib/entities/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export * from './date-rule';
export * from './schedule';
export * from './spreadsheet-file';
export * from './spreadsheet-row';
export * from './team';
export * from './time';
24 changes: 24 additions & 0 deletions libs/domain/src/lib/entities/spreadsheet-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export class SpreadsheetFile<T extends string | Blob> {
file: Blob;

constructor(content: T, type: `${string}/${string}`) {
this.file = new Blob([content], { type: `${type};charset=utf-8;` });
}

getDatePrefixFile(date: Date) {
return date.toLocaleDateString().slice(3).replace('/', '_');
}

downloadFile(download: `${string}.${string}`) {
const href = URL.createObjectURL(this.file);
this.#createEl('a', { download, href }).click();
URL.revokeObjectURL(href);
}

#createEl<K extends keyof HTMLElementTagNameMap>(
name: K,
attributes: Partial<HTMLElementTagNameMap[K]> = {}
) {
return Object.assign(document.createElement(name), attributes);
}
}
29 changes: 6 additions & 23 deletions libs/domain/src/lib/usecases/spreadsheet/build-spreadsheet.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,25 @@
import { DateRule, SpreadsheetRow } from '../../entities';
import { BuildSpreadsheetDto } from '../../dtos';
import { Frequency, RRule, Options } from 'rrule';
import { SpreadsheetRow } from '../../entities';
import { UseCase } from '../../base/use-case';
import { Schedule } from '../../entities';

export class BuildSpreadsheetUseCase
implements UseCase<BuildSpreadsheetDto, SpreadsheetRow[]>
{
execute({ schedules, dtstart, until }: BuildSpreadsheetDto) {
const spreadsheet = schedules
.map((schedule) => {
const options = this.#getRRules(schedule, dtstart, until);
const dates = this.#getDates(options);
const dateRule = new DateRule(schedule);
const interval = dateRule.getInterval(dtstart, until);
const dates = dateRule.getDates(interval);
return dates.map((date) => ({ ...schedule, date }));
})
.reduce((prev, curr) => [...prev, ...curr], [])
.map(({ team, timeStart, timeEnd, ...schedule }) => {
return {
time: `${timeStart} as ${timeEnd}`,
...schedule,
...team,
};
const time = `${timeStart} as ${timeEnd}`;
return { time, ...schedule, ...team };
})
.sort((a, b) => (a.date < b.date ? -1 : 1));

return Promise.resolve(spreadsheet);
}

#getRRules({ byweekday, interval }: Schedule, dtstart: Date, until: Date) {
return {
interval,
byweekday,
dtstart: new Date(dtstart),
until: new Date(until),
};
}

#getDates(options: Partial<Options>) {
return new RRule({ ...options, freq: Frequency.WEEKLY }).all();
}
}
32 changes: 4 additions & 28 deletions libs/domain/src/lib/usecases/spreadsheet/download-spreadsheet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SpreadsheetRow } from '../../entities/spreadsheet-row';
import { SpreadsheetRow, SpreadsheetFile } from '../../entities';
import { UseCase } from '../../base/use-case';
import { formatRow } from '../../mapper';

Expand All @@ -7,36 +7,12 @@ export class DownloadSpreadsheetUseCase
{
execute(rows: SpreadsheetRow[]) {
const parsed = rows.map(formatRow).join('\n');
const blob = this.#createFile(parsed, 'text/csv');

const prefix = this.#getDatePrefixFile(rows[0].date);
const name = `Solicitação de reserva laboratórios`;
const spreadsheet = this.#downloadFile(blob, `${prefix} - ${name}.csv`);

return Promise.resolve(spreadsheet);
}

#getDatePrefixFile(date: Date) {
return date.toLocaleDateString().slice(3).replace('/', '_');
}

#createFile<T extends string | Blob>(
content: T,
type: `${string}/${string}`
) {
return new Blob([content], { type: `${type};charset=utf-8;` });
}

#createEl<K extends keyof HTMLElementTagNameMap>(
name: K,
attributes: Partial<HTMLElementTagNameMap[K]> = {}
) {
return Object.assign(document.createElement(name), attributes);
}
const file = new SpreadsheetFile(parsed, 'text/csv');
const prefix = file.getDatePrefixFile(rows[0].date);

#downloadFile(blob: Blob, download: `${string}.${string}`) {
const href = URL.createObjectURL(blob);
this.#createEl('a', { download, href }).click();
URL.revokeObjectURL(href);
return Promise.resolve(file.downloadFile(`${prefix} - ${name}.csv`));
}
}

0 comments on commit e47da20

Please sign in to comment.