-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fa50004
commit c529f7e
Showing
4 changed files
with
132 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,55 @@ | ||
import { | ||
authRequest, | ||
standbyRequest, | ||
infoRequest | ||
} from '@corcc/nvr'; | ||
import { LightResponse } from '@corcc/nvr/lib/util/type'; | ||
|
||
import { confirmRequest, errorRequest, failureRequest, progressRequest, successRequest } from '@corcc/nvr'; | ||
import { config } from 'dotenv'; | ||
import { randomNumber } from './util/Random'; | ||
import { getVaccinesFromResponseBody } from './util/Response'; | ||
import { vaccineQuantity } from './info/Request'; | ||
config(); | ||
async function followRedirect ({ | ||
res | ||
|
||
export async function reservationSubmit ({ | ||
org, | ||
key, | ||
vaccines | ||
}: { | ||
res: any | ||
}): Promise<LightResponse | any> { | ||
if (res.responseCode == 200) { | ||
const { location } = res; | ||
if (!location) { | ||
let result = await res; | ||
result = getVaccinesFromResponseBody(res); | ||
return result; | ||
} | ||
} | ||
if (res.responseCode == 302) { | ||
const u: any = new URL('https://example.com'); | ||
u.params = { '': '' }; | ||
let _res: LightResponse = { | ||
responseCode: 0, | ||
headers: {}, | ||
body: '', | ||
location: u | ||
}; | ||
const { key } = res.location.params; | ||
if (res.location.pathname.indexOf('auth') > -1) { | ||
_res = await authRequest({ | ||
key | ||
}); | ||
} | ||
if (res.location.pathname.indexOf('info') > -1) { | ||
_res = await infoRequest({ | ||
key | ||
}); | ||
} | ||
if (_res.responseCode == 0) { | ||
throw new Error(); | ||
} | ||
return await followRedirect({ | ||
res: _res | ||
org: string, | ||
key: string, | ||
vaccines: any | ||
}): Promise<any> { | ||
config(); | ||
const name = process.env.VACCINE_NAME; | ||
let { cd }: any = vaccines.filter((vaccine: any) => (vaccine.name.indexOf(name) == 0)); | ||
cd = cd ? cd[0] : cd; | ||
const progressResult = await progressRequest({ | ||
key, | ||
cd | ||
}); | ||
const progressResponseCode = progressResult.responseCode; | ||
const confirmResult = await (async function (r: number) { | ||
switch ((r / 100)) { | ||
case 2: return await confirmRequest({ | ||
key, | ||
cd | ||
}); | ||
} | ||
} | ||
|
||
async function vaccineQuantity () { | ||
const standByResponse: LightResponse = await standbyRequest({ | ||
orgCd: process.env.orgCd, | ||
sid: process.env.sid | ||
default: throw new Error(); | ||
} | ||
})(Number(progressResponseCode)); | ||
const { code } = JSON.parse(confirmResult.body); | ||
switch (code) { | ||
case 'success': return successRequest({ | ||
key, | ||
cd | ||
}); | ||
let result = await followRedirect({ | ||
res: standByResponse | ||
case 'failure': return failureRequest({ | ||
key, | ||
cd, | ||
code | ||
}); | ||
case 'undefined': return errorRequest({ | ||
key, | ||
cd | ||
}); | ||
const filterAvailable:Function = function (r:any):any { | ||
const _r = r; | ||
_r.vaccines = _r.vaccines.filter((_: any) => (!_.disabled)); | ||
return _r; | ||
}; | ||
result = filterAvailable(result); | ||
let infoResponse: any; | ||
while (!result.vaccines.length) { | ||
const twoSec: number = 2000; | ||
const randomTwoSec:number = twoSec + randomNumber(randomNumber(0x7FF)); | ||
await new Promise(resolve => setTimeout(resolve, randomTwoSec)); | ||
infoResponse = await infoRequest({ | ||
key: result.key | ||
}); | ||
result = getVaccinesFromResponseBody(infoResponse); | ||
result = filterAvailable(result); | ||
} | ||
return result; | ||
} | ||
|
||
(async function () { | ||
await vaccineQuantity(); | ||
const vaccinesQuantities = await vaccineQuantity(); | ||
const reservationResult = await reservationSubmit(vaccinesQuantities); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { | ||
authRequest, | ||
standbyRequest, | ||
infoRequest | ||
} from '@corcc/nvr'; | ||
import { LightResponse } from '@corcc/nvr/lib/util/type'; | ||
import { config } from 'dotenv'; | ||
import { randomNumber } from '../util/Random'; | ||
import { getVaccinesFromResponseBody } from '../util/Response'; | ||
|
||
async function followRedirect ({ | ||
res | ||
}: { | ||
res: any | ||
}): Promise<LightResponse | any> { | ||
if (res.responseCode == 200) { | ||
const { location } = res; | ||
if (!location) { | ||
let result = await res; | ||
result = getVaccinesFromResponseBody(res); | ||
return result; | ||
} | ||
} | ||
if (res.responseCode == 302) { | ||
const u: any = new URL('https://example.com'); | ||
u.params = { '': '' }; | ||
let _res: LightResponse = { | ||
responseCode: 0, | ||
headers: {}, | ||
body: '', | ||
location: u | ||
}; | ||
const { key } = res.location.params; | ||
if (res.location.pathname.indexOf('auth') > -1) { | ||
_res = await authRequest({ | ||
key | ||
}); | ||
} | ||
if (res.location.pathname.indexOf('info') > -1) { | ||
_res = await infoRequest({ | ||
key | ||
}); | ||
} | ||
if (_res.responseCode == 0) { | ||
throw new Error(); | ||
} | ||
return await followRedirect({ | ||
res: _res | ||
}); | ||
} | ||
} | ||
|
||
export async function vaccineQuantity () { | ||
const standByResponse: LightResponse = await standbyRequest({ | ||
orgCd: process.env.orgCd, | ||
sid: process.env.sid | ||
}); | ||
let result = await followRedirect({ | ||
res: standByResponse | ||
}); | ||
const filterAvailable:Function = function (r:any):any { | ||
const _r = r; | ||
_r.vaccines = _r.vaccines.filter((_: any) => (!_.disabled)); | ||
return _r; | ||
}; | ||
result = filterAvailable(result); | ||
let infoResponse: any; | ||
while (!result.vaccines.length) { | ||
const twoSec: number = 2000; | ||
const randomTwoSec:number = twoSec + randomNumber(randomNumber(0x7FF)); | ||
await new Promise(resolve => setTimeout(resolve, randomTwoSec)); | ||
infoResponse = await infoRequest({ | ||
key: result.key | ||
}); | ||
result = getVaccinesFromResponseBody(infoResponse); | ||
result = filterAvailable(result); | ||
} | ||
return result; | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters