import { marshall , unmarshall } from "../deps.ts" ; import { DYNAMO_CLIENT } from "./clients.ts" ; import { DYNAMO_ALIAS_TABLE } from "./constants.ts" ; export type Tags = Record < string , string > ; export interface Alias { alias : string ; owner : number ; tags : Tags ; } export type Requests = Alias [ ] ; export async function getRequests ( ) { const { Item : item } = await DYNAMO_CLIENT . getItem ( { TableName : DYNAMO_ALIAS_TABLE , Key : marshall ( { alias : "$requests" } ) } ) ; if ( item === undefined ) { await DYNAMO_CLIENT . putItem ( { TableName : DYNAMO_ALIAS_TABLE , Item : marshall ( { alias : "$requests" , requests : [ ] } ) } ) ; return [ ] ; } return unmarshall ( item ) . requests ; } export async function pushRequest ( alias ) { const requests = await getRequests ( ) ; requests . push ( alias ) ; return await DYNAMO_CLIENT . putItem ( { TableName : DYNAMO_ALIAS_TABLE , Item : marshall ( { alias : "$requests" , requests } ) } ) ; } export async function approveRequest ( owner , alias ) { const requests = await getRequests ( ) ; const found = requests . splice ( requests . findIndex ( ( request ) => request . owner === owner && request . alias === alias ) , 1 ) [ 0 ] ; if ( found ) { await putAlias ( found ) ; } else { return ; } return await DYNAMO_CLIENT . putItem ( { TableName : DYNAMO_ALIAS_TABLE , Item : marshall ( { alias : "$requests" , requests } ) } ) ; } export async function denyRequest ( owner , alias ) { const requests = await getRequests ( ) ; requests . splice ( requests . findIndex ( ( request ) => request . owner === owner && request . alias === alias ) , 1 ) ; return await DYNAMO_CLIENT . putItem ( { TableName : DYNAMO_ALIAS_TABLE , Item : marshall ( { alias : "$requests" , requests } ) } ) ; } export async function getAlias ( alias ) { const { Item : item } = await DYNAMO_CLIENT . getItem ( { TableName : DYNAMO_ALIAS_TABLE , Key : marshall ( { alias } ) } ) ; if ( item ) { return unmarshall ( item ) ; } } export async function putAlias ( alias ) { return await DYNAMO_CLIENT . putItem ( { TableName : DYNAMO_ALIAS_TABLE , Item : marshall ( alias ) } ) ; } export async function getIdFromAlias ( alias , tag ) { const item = await getAlias ( alias ) ; if ( item === undefined ) { return undefined ; } return item . tags [ tag ] ; } export async function listAliases ( id ) { const { Items : items } = await DYNAMO_CLIENT . scan ( { TableName : DYNAMO_ALIAS_TABLE , FilterExpression : "#owner = :owner" , ExpressionAttributeValues : { ":owner" : { "N" : id . toString ( ) } } , ExpressionAttributeNames : { "#owner" : "owner" } } ) ; if ( items ) { return items . map ( unmarshall ) ; } else { return [ ] ; } }