Provision instance bank credentials automatically (Refs #146)
This commit is contained in:
@@ -20,6 +20,7 @@ interface Result {
|
||||
balance: number;
|
||||
}
|
||||
interface Saved {
|
||||
instance_id: string | null;
|
||||
fingerprint: string;
|
||||
state: string;
|
||||
amount: number;
|
||||
@@ -79,7 +80,8 @@ export function bankProfile(guid: string) {
|
||||
export function bankRequest(
|
||||
action: BankAction,
|
||||
value: unknown,
|
||||
authenticatedServer: string
|
||||
authenticatedServer: string,
|
||||
instanceId: string
|
||||
): { status: number; body: Result } {
|
||||
const result = (status: number, code: string, request_id = '') => ({
|
||||
status,
|
||||
@@ -121,7 +123,7 @@ export function bankRequest(
|
||||
const saved = handle
|
||||
.prepare('SELECT * FROM bank_requests WHERE server = ? AND request_id = ?')
|
||||
.get(server, request_id) as Saved | undefined;
|
||||
if (saved && saved.fingerprint !== fingerprint)
|
||||
if (saved && (saved.instance_id !== instanceId || saved.fingerprint !== fingerprint))
|
||||
return result(409, 'request_conflict', request_id);
|
||||
const balance =
|
||||
(
|
||||
@@ -145,8 +147,8 @@ export function bankRequest(
|
||||
const insert = (body: Result, matchId: number | null = null) => {
|
||||
handle
|
||||
.prepare(
|
||||
`INSERT INTO bank_requests(server,request_id,guid,fingerprint,operation,match_id,amount,state,result,created_at)
|
||||
VALUES (?,?,?,?,?,?,?,?,?,?)`
|
||||
`INSERT INTO bank_requests(server,request_id,guid,fingerprint,operation,match_id,amount,state,result,created_at,instance_id)
|
||||
VALUES (?,?,?,?,?,?,?,?,?,?,?)`
|
||||
)
|
||||
.run(
|
||||
server,
|
||||
@@ -158,7 +160,8 @@ export function bankRequest(
|
||||
body.amount,
|
||||
body.state,
|
||||
JSON.stringify(body),
|
||||
now
|
||||
now,
|
||||
instanceId
|
||||
);
|
||||
return { status: 200, body };
|
||||
};
|
||||
@@ -200,6 +203,14 @@ export function bankRequest(
|
||||
return { status: 200, body };
|
||||
}
|
||||
if (saved) return { status: 200, body: JSON.parse(saved.result) as Result };
|
||||
if (
|
||||
!handle
|
||||
.prepare(
|
||||
'SELECT 1 FROM native_instances WHERE id=? AND server=? AND retired_at IS NULL'
|
||||
)
|
||||
.get(instanceId, server)
|
||||
)
|
||||
return result(403, 'retired_instance', request_id);
|
||||
const member = handle
|
||||
.prepare(
|
||||
`SELECT m.id FROM leaderboard_matches m
|
||||
|
||||
Reference in New Issue
Block a user