edge-core-js
Version:
Edge account & wallet management library
38 lines (31 loc) • 1.07 kB
JavaScript
import {
asLoginPayload,
wasChangeVouchersPayload
} from '../../types/server-cleaners'
import { applyLoginPayload, makeAuthJson } from './login'
import { loginFetch } from './login-fetch'
import { getStashById } from './login-selectors'
import { saveStash } from './login-stash'
/**
* Approves or rejects vouchers on the server.
*/
export async function changeVoucherStatus(
ai,
login,
vouchers
) {
const { stashTree } = getStashById(ai, login.loginId)
const { deviceDescription } = ai.props.state.login
const request = makeAuthJson(stashTree, login)
if (deviceDescription != null) request.deviceDescription = deviceDescription
request.data = wasChangeVouchersPayload(vouchers)
// We would normally use `applyKit` instead of a direct fetch,
// but we need the server to tell us what changed, not the diff:
const reply = await loginFetch(ai, 'POST', '/v2/login/vouchers', request)
const newStashTree = applyLoginPayload(
stashTree,
login.loginKey,
asLoginPayload(reply)
)
return await saveStash(ai, newStashTree)
}