import { ASSET_TYPE , NODE_TYPES } from 'common/enums' ; import { AssetNotFoundError , EntityNotFoundError , UserInputError , } from 'common/errors' ; import { fromGlobalId , toGlobalId } from 'common/utils' ; import { MutationToPutAnnouncementResolver } from 'definitions' ; const resolver = async ( root , { input : { id , title , content , cover , link , type , visible , order } } , { dataSources : { atomService , systemService } , viewer } ) => { const table = 'announcement' ; const toAnnouncementId = ( dbId ) => toGlobalId ( { type : NODE_TYPES . Announcement , id : dbId } ) ; const toCoverURL = async ( coverId ) => coverId ? systemService . findAssetUrl ( coverId ) : null ; let coverDbId ; if ( cover ) { const asset = await systemService . findAssetByUUID ( cover ) ; if ( ! asset || asset . type !== ASSET_TYPE . announcementCover || asset . authorId !== viewer . id ) { throw new AssetNotFoundError ( 'annuncement cover asset does not exists' ) ; } coverDbId = asset . id ; } if ( id ) { const { id : dbId } = fromGlobalId ( id ) ; const item = await atomService . findUnique ( { table , where : { id : dbId } , } ) ; if ( ! item ) { throw new EntityNotFoundError ( `target ${ dbId } not found` ) ; } const updatedItem = await atomService . update ( { table , where : { id : dbId } , data : { ... ( title ? { title } : { } ) , ... ( coverDbId ? { cover : coverDbId } : { } ) , ... ( content ? { content } : { } ) , ... ( link ? { link } : { } ) , ... ( type ? { type } : { } ) , ... ( typeof visible === 'boolean' ? { visible } : { } ) , } , } ) ; const updatedAnnouncement = { ... updatedItem , id : toAnnouncementId ( updatedItem . id ) , cover : toCoverURL ( updatedItem . cover ) , } ; return updatedAnnouncement ; } if ( ! type ) { throw new UserInputError ( 'required parameters missing: type' ) ; } const newItem = await atomService . create ( { table , data : { cover : coverDbId , link , type , order : order || 0 } , } ) ; const newAnnouncement = { ... newItem , id : toAnnouncementId ( newItem . id ) , cover : toCoverURL ( newItem . cover ) , } ; return newAnnouncement ; } ; export default resolver ;