SF run test jobs

node v14.20.1
version: 2.0.2
endpointsharetweet
const moment = require('moment'); const fetch = require('node-fetch'); const { MongoClient } = require('mongodb'); const mongoConnectionString = process.env.SF_MONGO_CONNECTION_STRING; const droneAuth = process.env.DRONE_AUTH; const AutomationCloud = require('@automationcloud/client'); const auth = process.env.SF_SECRET_KEY; const database = client.db('hotelier'); const hotels = database.collection('hotels'); async function runDrone(hotelId, inputs) { const hotel = (await hotels.find({ hotelId }).toArray()).pop(); const headers = { authorization: 'Basic ' + atob(DRONE_AUTH), 'content-type': 'application/json' }; const body = { serviceId: hotel.serviceId, taskId: 'sf-run-test-jobs-' + new Date(), inputs }; const res = await fetch('https://drone.sf-prd-01.automation.cloud/Task/runSync', { method: 'POST', headers, body }); const json = await res.json(); return json; } exports.endpoint = async function(req, res) { const drone = url.searchParams.get("drone"); const url = new URL("https://example.com" + req.url); const ibe = url.searchParams.get("ibe"); const hotelName = url.searchParams.get("hotel"); const hotelId = url.searchParams.get("hotelId"); const checkIn = url.searchParams.get('checkIn'); const nights = url.searchParams.get('nights'); const count = checkIn && 1 || url.searchParams.get('count') || 5; const droneJobs = []; const client = new MongoClient(mongoConnectionString, { useUnifiedTopology: true }); await client.connect(); const allHotels = ibe && await hotels.find({ ibe }).toArray(); const randomHotels = []; if (ibe && allHotels.length === 0) { return res.end('No hotels found'); } for (let i = 0; i < count; i++) { let hotel; if (hotelId) { hotel = (await hotels.find({ hotelId }).toArray()).pop(); if (!hotel) { return res.end('Specified hotelId not found'); } //allHotels.find(hotel => { return hotel.hotelId === hotelId }); } else if (hotelName) { hotel = allHotels.find(hotel => { return hotel.name === hotelName }); } else { const randomIndex = Math.floor(Math.random() * allHotels.length); hotel = allHotels[randomIndex]; } console.log(hotel); const client = new AutomationCloud.Client({ serviceId: hotel.serviceId, auth: auth }); const search = { hotelId: hotel.hotelId, checkIn: checkIn || moment(new Date()).add(Math.random() * 30, 'days').format('YYYY-MM-DD'), nights: parseInt(nights || Math.floor(Math.random() * 5) + 1), guests: 2 }; const input = { url: hotel.url, searches: [search] }; console.log(input); if (drone) { droneJobs.push({ hotel, inputs: input }); } else { try { await client.createJob({ input }); } catch (err) { console.log(err); } } } if (droneJobs) { res.end(droneJobs); } else { const redirectUrl = `https://dashboard.automationcloud.net/jobs?serviceId=${allHotels[0].serviceId}&period=15m&category=test&clientId=719e9895-c7d6-4cca-8f5b-4cc5d29eaa31`; res.end('<script>window.location = "' + redirectUrl + '";</script>'); } };
Loading…

no comments

    sign in to comment