import { v4 as uuid } from 'uuid' ; import { MemoryDB } from '../db' ; import { Book , Loan , LoanInput , castLoan , User } from '@lib/entities' ; export async function takeLoan ( db , input ) { const loan = castLoan ( { ... input , returned : false , id : uuid ( ) } ) ; db . loans . add ( loan ) ; return loan ; } export async function endLoan ( db , input ) { for ( const loan of db . loans ) { if ( loan . id === input . id ) { db . loans . delete ( loan ) ; db . loans . add ( { ... loan , returned : true } ) ; return loan ; } } throw new Error ( 'No loan found in set' ) ; } export async function getUserLoans ( db , input ) { const loans = Array . from ( db . loans ) ; return loans . filter ( loan => loan . userId === input . id && loan . returned === false ) ; } export async function getLoan ( db , input ) { for ( const loan of db . loans ) { if ( loan . bookId === input . id && loan . returned == false ) { return loan ; } } return null ; }