Skip to content

Commit 5e446fe

Browse files
refactor: extract magic var
1 parent 205abf9 commit 5e446fe

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

src/domain/usecases/HandleNewReservationUseCase.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { reservationRepository } from '../../infrastructure/ReservationRepositor
22
import { NotFoundError } from '../NotFoundError.js';
33
import { Reservation } from '../Reservation.js';
44

5+
const CODE_REGEXP = /Voici votre code de réservation UCPA : (?<code>\d+)/;
6+
57
export class HandleNewReservationUseCase {
68
constructor({ imapClient, searchQuery }) {
79
this.imapClient = imapClient;
@@ -20,7 +22,7 @@ export class HandleNewReservationUseCase {
2022
}
2123

2224
_getUCPAReservationCode(message) {
23-
const match = message.html.match(/Voici votre code de réservation UCPA : (?<code>\d+)/);
25+
const match = message.html.match(CODE_REGEXP);
2426
if (!match) {
2527
return null;
2628
}

src/domain/usecases/HandleScheduledReservationUseCase.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { NotFoundError } from '../NotFoundError.js';
22
import { Reservation } from '../Reservation.js';
33

4+
const RESERVATION_ACCEPTED_MESSAGE_CONTENT = 'MERCI POUR VOTRE RESERVATION !';
5+
const EXTRACT_INFORMATION_REGEXP = /Terrain (?<court>\d+) (?<activity>\w+)\s\w+ le (?<date>\d{2}-\d{2}-\d{4}) à (?<hour>\d{2}:\d{2})/;
6+
const EXTRACT_CODE_REGEXP = /<p>(?<code>\d+)<\/p>/;
7+
48
export class HandleScheduledReservationUseCase {
59
constructor({ imapClient, searchQuery, reservationRepository }) {
610
this.imapClient = imapClient;
@@ -11,7 +15,7 @@ export class HandleScheduledReservationUseCase {
1115
async execute() {
1216
const messages = await this.imapClient.fetch(this.searchQuery);
1317
for (const message of messages) {
14-
const isScheduledReservationMessage = message.html.includes('MERCI POUR VOTRE RESERVATION !');
18+
const isScheduledReservationMessage = message.html.includes(RESERVATION_ACCEPTED_MESSAGE_CONTENT);
1519
if (!isScheduledReservationMessage) {
1620
continue;
1721
}
@@ -24,12 +28,12 @@ export class HandleScheduledReservationUseCase {
2428
}
2529

2630
_getInformation(message) {
27-
const match = message.html.match(/Terrain (?<court>\d+) (?<activity>\w+)\s\w+ le (?<date>\d{2}-\d{2}-\d{4}) à (?<hour>\d{2}:\d{2})/);
31+
const match = message.html.match(EXTRACT_INFORMATION_REGEXP);
2832
if (!match) {
2933
return null;
3034
}
3135

32-
const matchCode = message.html.match(/<p>(?<code>\d+)<\/p>/);
36+
const matchCode = message.html.match(EXTRACT_CODE_REGEXP);
3337
if (!matchCode) {
3438
return null;
3539
}

src/infrastructure/TimeSlotDatasource.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import { TimeSlot } from '../domain/TimeSlot.js';
22
import { httpClient } from './HttpClient.js';
33

4+
const USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:127.0) Gecko/20100101 Firefox/127.0';
5+
46
export class TimeSlotDatasource {
57
async getAllAvailable(areaId) {
68
const requests = [
79
getDatePlusDays(0),
810
getDatePlusDays(7),
911
getDatePlusDays(14),
1012
].map(async (date) => {
11-
return httpClient.get(`https://www.ucpa.com/sport-station/api/areas-offers/weekly/alpha_hp?=&reservationPeriod=1&espace=${areaId}&time=${date}&__amp_source_origin=https://www.ucpa.com`, { 'User-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:127.0) Gecko/20100101 Firefox/127.0' });
13+
return httpClient.get(`https://www.ucpa.com/sport-station/api/areas-offers/weekly/alpha_hp?=&reservationPeriod=1&espace=${areaId}&time=${date}&__amp_source_origin=https://www.ucpa.com`, { 'User-agent': USER_AGENT });
1214
});
1315

1416
let availableTimeSlots = [];

0 commit comments

Comments
 (0)