import { sp , EmailProperties , Items , Item , Web } from "@pnp/sp" ; import { HttpClient , AadHttpClient , IHttpClientOptions , HttpClientResponse } from '@microsoft/sp-http' ; import { Guid } from "@microsoft/sp-core-library" ; import { Dialog } from '@microsoft/sp-dialog' ; class TableColumn { public replacementType : "PlainText" | "Image" ; public value : string ; } ; class TableRow { public columns : TableColumn [ ] ; public constructor ( ) { this . columns = [ ] ; } } ; class TableParameter { public token : string ; public rows : TableRow [ ] ; public constructor ( token ) { this . rows = [ ] ; this . token = token ; } } ; class ReplacementParameters { public tableParameters : TableParameter [ ] ; public plainTextParameters : { replacementType : "PlainText" | "Image" ; token : string ; value : string ; } [ ] ; public constructor ( ) { this . plainTextParameters = [ ] ; this . tableParameters = [ ] ; } } export default class DocumentGenerator { param aadHttpClient param web param listId param itemId param azureFunctionUrl param templateServerRelativeUrl param destinationFolderServerRelativeURL param temporaryFolderServerRelativeURL param webServerRelativeURL param saveAsFormat public static async generateDocument ( aadHttpClient , web , listId , itemId , azureFunctionUrl , templateServerRelativeUrl , destinationFolderServerRelativeUrl , temporaryFolderServerRelativeUrl , webServerRelativeUrl , saveAsFormat ) { var rp = await this . getData ( web , listId , itemId , new ReplacementParameters ( ) ) ; var ifr = await web . lists . getById ( listId . toString ( ) ) . items . getById ( itemId ) . get ( ) ; var newFileName = ifr [ "Title" ] . replace ( /\//g , "-" ) . replace ( ":" , "-" ) ; const body = JSON . stringify ( { 'plainTextParameters' : rp . plainTextParameters , "temporaryFolderServerRelativeUrl" : temporaryFolderServerRelativeUrl , "webServerRelativeUrl" : webServerRelativeUrl , 'templateServerRelativeUrl' : templateServerRelativeUrl , 'destinationFolderServerRelativeUrl' : destinationFolderServerRelativeUrl , 'fileName' : newFileName , "saveAsFormat" : saveAsFormat } ) ; const requestHeaders = new Headers ( ) ; requestHeaders . append ( 'Content-type' , 'application/json' ) ; return aadHttpClient . fetch ( azureFunctionUrl , AadHttpClient . configurations . v1 , { method : "POST" , body : body , headers : requestHeaders , } ) . then ( ( response ) => { if ( response . status === 200 ) { return response . json ( ) . then ( ( responseObject ) => { debugger ; return responseObject . url ; } ) ; } else { console . log ( `Error http Reponse follows` ) ; console . log ( response ) ; Dialog . alert ( `Error-- Code: ${ response . status } Message:${ response . statusText }` ) ; return null ; } } ) . catch ( ( err ) => { Dialog . alert ( `an error occurred on the back-end server` ) ; debugger ; return null ; } ) ; } public static async PreviewPDF ( aadHttpClient , web , listId , itemId , azureFunctionUrl , templateServerRelativeUrl , destinationFolderServerRelativeUrl , temporaryFolderServerRelativeUrl , webServerRelativeUrl , saveAsFormat ) { var rp = await this . getData ( web , listId , itemId , new ReplacementParameters ( ) ) ; var ifr = await web . lists . getById ( listId . toString ( ) ) . items . getById ( itemId ) . get ( ) ; var newFileName = ifr [ "Title" ] . replace ( /\//g , "-" ) . replace ( ":" , "-" ) ; var testTableData = [ { "Id" : "XXX" , "desc" : "desk" , "qty" : "1" } , { "Id" : "YYY" , "desc" : "chair" , "qty" : "3" } ] ; var tbl1 = new TableParameter ( "table1" ) ; for ( var rc of testTableData ) { var tr = new TableRow ( ) ; tr . columns . push ( { replacementType : "PlainText" , value : rc . Id } ) ; tr . columns . push ( { replacementType : "PlainText" , value : rc . desc } ) ; tr . columns . push ( { replacementType : "PlainText" , value : rc . qty } ) ; tbl1 . rows . push ( tr ) ; } const body = JSON . stringify ( { 'plainTextParameters' : rp . plainTextParameters , 'tableParameters' : [ tbl1 ] , "temporaryFolderServerRelativeUrl" : temporaryFolderServerRelativeUrl , "webServerRelativeUrl" : webServerRelativeUrl , 'templateServerRelativeUrl' : templateServerRelativeUrl , 'fileName' : newFileName } ) ; const requestHeaders = new Headers ( ) ; requestHeaders . append ( 'Content-type' , 'application/json' ) ; console . log ( `About to make REST API request to function with URL ${ azureFunctionUrl } ` ) ; return aadHttpClient . fetch ( azureFunctionUrl , AadHttpClient . configurations . v1 , { method : "POST" , body : body , headers : requestHeaders , } ) . then ( ( response ) => { if ( response . status === 200 ) { return response . json ( ) . then ( ( responseObject ) => { debugger ; return responseObject . url ; } ) ; } else { console . log ( `http Reponse follows` ) ; console . log ( response ) ; Dialog . alert ( `Error-- Code: ${ response . status } Message:${ response . statusText }` ) ; return null ; } } ) . catch ( ( err ) => { Dialog . alert ( `an error occurred on the back-end server ${ err }` ) ; debugger ; return null ; } ) ; } public static async getData ( web , listId , itemId , replacementParameters ) { var ifr = await web . lists . getById ( listId . toString ( ) ) . items . getById ( itemId ) . expand ( 'Author' ) . select ( 'Title,Created,IsComplete,Author/Title' ) . get ( ) ; replacementParameters . plainTextParameters . push ( { "replacementType" : "PlainText" , "token" : "ptTitle" , "value" : ifr . Title } ) ; replacementParameters . plainTextParameters . push ( { "replacementType" : "PlainText" , "token" : "ptCreated" , "value" : ifr . Created } ) ; replacementParameters . plainTextParameters . push ( { "replacementType" : "PlainText" , "token" : "ptAuthor" , "value" : ifr . Author . Title } ) ; var ifrAttachementFiles = await web . lists . getById ( listId . toString ( ) ) . items . getById ( itemId ) . attachmentFiles . get ( ) ; if ( ifrAttachementFiles . length > 0 ) { replacementParameters . plainTextParameters . push ( { "replacementType" : "Image" , "token" : "pic1" , "value" : ifrAttachementFiles [ 0 ] . ServerRelativeUrl } ) ; } replacementParameters . plainTextParameters . push ( { "replacementType" : "PlainText" , "token" : "ptIsComplete" , "value" : ifr [ "IsComplete" ] === "Yes" ? "☒" : "☐" } ) ; return replacementParameters ; } }