import { Attachment , AuditAttachment , Recorded , UUID } from '@mds-core/mds-types' ; import { NotFoundError , now } from '@mds-core/mds-utils' ; import schema from './schema' ; import { vals_sql , cols_sql , vals_list , logSql } from './sql-utils' ; import { getReadOnlyClient , getWriteableClient } from './client' ; export async function writeAttachment ( attachment ) { const client = await getWriteableClient ( ) ; const sql = `INSERT INTO ${ schema . TABLE . attachments } (${ cols_sql ( schema . TABLE_COLUMNS . attachments ) }) VALUES (${ vals_sql ( schema . TABLE_COLUMNS . attachments ) }) RETURNING *` ; const values = vals_list ( schema . TABLE_COLUMNS . attachments , { ... attachment , recorded : now ( ) } ) ; await logSql ( sql , values ) ; const { rows : [ recordedAttachment ] } = await client . query ( sql , values ) ; return { ... attachment , ... recordedAttachment } ; } export async function readAttachmentsForAudit ( audit_trip_id ) { const client = await getReadOnlyClient ( ) ; const sql = `SELECT * FROM ${ schema . TABLE . attachments } a JOIN ${ schema . TABLE . audit_attachments } aa
    ON a.attachment_id = aa.attachment_id where aa.audit_trip_id = '${ audit_trip_id }'` ; const { rows } = await client . query ( sql ) ; return rows ; } export async function readAuditAttachments ( attachment_id ) { const client = await getWriteableClient ( ) ; const sql = `SELECT * FROM ${ schema . TABLE . audit_attachments } WHERE attachment_id=$1` ; const res = await client . query ( sql , [ attachment_id ] ) ; return res . rows ; } export async function writeAuditAttachment ( auditAttachment ) { const client = await getWriteableClient ( ) ; const sql = `INSERT INTO ${ schema . TABLE . audit_attachments } (${ cols_sql ( schema . TABLE_COLUMNS . audit_attachments ) }) VALUES (${ vals_sql ( schema . TABLE_COLUMNS . audit_attachments ) }) RETURNING *` ; const values = vals_list ( schema . TABLE_COLUMNS . audit_attachments , { ... auditAttachment , recorded : now ( ) } ) ; await logSql ( sql , values ) ; const { rows : [ recordedAuditAttachment ] } = await client . query ( sql , values ) ; return { ... auditAttachment , ... recordedAuditAttachment } ; } export async function deleteAttachment ( attachment_id ) { const client = await getWriteableClient ( ) ; const sql = `DELETE FROM ${ schema . TABLE . attachments } WHERE attachment_id=$1 RETURNING *` ; const res = await client . query ( sql , [ attachment_id ] ) ; if ( res . rows . length > 0 ) { return { ... res . rows [ 0 ] } as Attachment ; } throw new NotFoundError ( `Attachment ${ attachment_id } not found` ) ; } export async function deleteAuditAttachment ( audit_trip_id , attachment_id ) { const client = await getWriteableClient ( ) ; const sql = `DELETE FROM ${ schema . TABLE . audit_attachments } WHERE attachment_id=$1 AND audit_trip_id=$2 RETURNING *` ; const res = await client . query ( sql , [ attachment_id , audit_trip_id ] ) ; if ( res . rows . length > 0 ) { return { ... res . rows [ 0 ] } as AuditAttachment ; } throw new NotFoundError ( `Audit attachment ${ audit_trip_id } ${ attachment_id } not found` ) ; }