Untitled
unknown
typescript
2 months ago
3.0 kB
6
Indexable
@Cron(CronExpression.EVERY_MINUTE) async makeRequest() { // Define search parameters for both locations const locations = [ { name: 'Sollentuna', id: 1000134, message: 'Found in Sollentuni' }, { name: 'Södertälje', id: 1000132, message: 'Found in Södertälje' }, { name: 'Farsta', id: 1000019, message: 'Ima termin u Södertälje' }, ]; const dateRange = { start: '2024-11-01', end: '2024-11-04' }; // Loop through each location and make the request for (const location of locations) { const data = this.buildRequestData(location.id); await this.checkAvailability( location.name, data, location.message, dateRange, ); } } // Build the request body for the specific location buildRequestData(locationId: number) { return { bookingSession: { socialSecurityNumber: '19950319-xxxx', licenceId: 5, bookingModeId: 0, ignoreDebt: false, ignoreBookingHindrance: false, examinationTypeId: 0, excludeExaminationCategories: [], rescheduleTypeId: 0, paymentIsActive: false, paymentReference: null, paymentUrl: null, searchedMonths: 0, }, occasionBundleQuery: { startDate: '1970-01-01T00:00:00.000Z', searchedMonths: 0, locationId, nearbyLocationIds: [], languageId: 0, vehicleTypeId: 4, tachographTypeId: 1, occasionChoiceId: 1, examinationTypeId: 12, }, }; } // Make the request and check for available dates async checkAvailability( locationName: string, data: any, successMessage: string, dateRange: { start: string; end: string }, ) { try { const response = await firstValueFrom( this.httpService.post(this.url, data, { headers: this.headers }), ); const availableSlots = this.filterByDateRange( response.data.data.bundles, dateRange.start, dateRange.end, ); if (availableSlots.length > 0) { this.telegramService.sendMessage(telegram_chat_id, successMessage); } else { console.log(`No available slots in ${locationName}`); } } catch (error) { this.telegramService.sendMessage(telegram_chat_id, "REQUEST DOWN! REQUEST DOWN"); console.error(`Error making request for ${locationName}:`, error); } } // Filter available dates based on the given range filterByDateRange(bundles, startDate: string, endDate: string) { const start = new Date(startDate); const end = new Date(endDate); return bundles .map((bundle) => ({ ...bundle, occasions: bundle.occasions.filter((occasion) => { const occasionDate = new Date(occasion.date); return occasionDate >= start && occasionDate <= end; }), })) .filter((bundle) => bundle.occasions.length > 0); } }
Editor is loading...
Leave a Comment