import * as ZOHOCRMSDK from "@zohocrm/nodejs-sdk-5.0"; 
export default class HoursBulkCreate{
    /**
     *  Create BulkWrite Job
     * This method is used to create bulk write job with the uploaded file ID
     * @param {String} moduleAPIName The API Name of the module.
     * @param {String} fileId The ID of the uploaded file to create BulkWrite Job.
     */
    static async createBulkWriteJob(moduleAPIName, fileId){
        
        //Get instance of BulkWriteOperations Class
        let bulkWriteOperations = new ZOHOCRMSDK.BulkWrite.BulkWriteOperations();
        //Get instance of RequestWrapper Class that will contain the request body
        let requestWrapper = new ZOHOCRMSDK.BulkWrite.RequestWrapper();
        //Set the charset of the uploaded file
        requestWrapper.setCharacterEncoding("UTF-8");
        //To set the type of operation you want to perform on the bulk write job.
        requestWrapper.setOperation(new ZOHOCRMSDK.Choice("insert"));
        let resources = [];
        //Get instance of Resource Class
        let resource = new ZOHOCRMSDK.BulkWrite.Resource();
        // To set the type of module that you want to import. The value is data.
        resource.setType(new ZOHOCRMSDK.Choice("data"));
        //To set API name of the module that you select for bulk write job.
        let minifiedModuleRecord = new ZOHOCRMSDK.Modules.MinifiedModule()
        minifiedModuleRecord.setModule(moduleAPIName)
        resource.setModule(minifiedModuleRecord);
        
        //To set the fileId obtained from file upload API.
        resource.setFileId(fileId);
        //True - Ignores the empty values.The default value is false.
        resource.setIgnoreEmpty(true);
        // To set a field as a unique field or ID of a record.
        // resource.setFindBy("Email");
        let fieldMappings = [];
        //Get instance of FieldMapping Class
        let fieldMapping = new ZOHOCRMSDK.BulkWrite.FieldMapping();
        resource.setFindBy("id");
        //To set API name of the field present in Zoho module object that you want to import.
        fieldMapping.setAPIName("Projekti");
        //To set the column index of the field you want to map to the CRM field.
        fieldMapping.setIndex(0);
        fieldMappings.push(fieldMapping);
        fieldMapping = new ZOHOCRMSDK.BulkWrite.FieldMapping();
        fieldMapping.setAPIName("Teht_v");
        fieldMapping.setIndex(1);
        fieldMappings.push(fieldMapping);
        fieldMapping = new ZOHOCRMSDK.BulkWrite.FieldMapping();
        fieldMapping.setAPIName("Ty_ntekij");
        fieldMapping.setIndex(2);
        
        fieldMappings.push(fieldMapping);
        fieldMapping = new ZOHOCRMSDK.BulkWrite.FieldMapping();
        fieldMapping.setAPIName("Tunnit");
        fieldMapping.setIndex(3);
        fieldMappings.push(fieldMapping);
        fieldMapping = new ZOHOCRMSDK.BulkWrite.FieldMapping();
        fieldMapping.setAPIName("PVM");
        fieldMapping.setIndex(4);
        fieldMappings.push(fieldMapping);
        fieldMapping = new ZOHOCRMSDK.BulkWrite.FieldMapping();
        fieldMapping.setAPIName("Owner");
        fieldMapping.setIndex(5);
        fieldMappings.push(fieldMapping);
        fieldMapping = new ZOHOCRMSDK.BulkWrite.FieldMapping();
        fieldMapping.setFindBy("id")
        fieldMapping.setAPIName("Projekti_Zohossa");
        fieldMapping.setIndex(6);
        fieldMappings.push(fieldMapping);
        resource.setFieldMappings(fieldMappings);
        resources.push(resource);
        //Set the array of resources to RequestWrapper instance
        requestWrapper.setResource(resources);
        //Call createBulkWriteJob method that takes RequestWrapper instance as parameter
        let response = await bulkWriteOperations.createBulkWriteJob(requestWrapper);
        if(response != null){
            console.log(response)
            //Get the status code from response
            console.log("Status Code: " + response.getStatusCode());
            //Get object from response
            let responseObject = response.getObject();
            if(responseObject != null){
                //Check if the request is successful
                if(responseObject instanceof ZOHOCRMSDK.BulkWrite.SuccessResponse){
                    //Get the Status
                    console.log("Status: " + responseObject.getStatus().getValue());
                    //Get the Code
                    console.log("Code: " + responseObject.getCode().getValue());
                    console.log("Details");
                    let details = responseObject.getDetails();
                    //Get the details map
                    if(details != null){
                        Array.from(details.keys()).forEach(key => {
                            console.log(key + ": " + details.get(key));  
                        });
                    }
                    //Get the Message
                    console.log("Message: " + responseObject.getMessage().getValue());
                }
                //Check if the request returned an exception
                else if(responseObject instanceof ZOHOCRMSDK.BulkWrite.APIException){
                    //Get the Status
                    console.log("Status: " + responseObject.getStatus().getValue());
                    //Get the Code
                    console.log("Code: " + responseObject.getCode().getValue());
                    console.log("Details");
                    //Get the details map
                    let details = responseObject.getDetails();
                    if(details != null){
                        Array.from(details.keys()).forEach(key => {
                            console.log(key + ": " + details.get(key));  
                        });
                    }
                    //Get the Message
                    console.log("Message: " + responseObject.getMessage().getValue());
                } 
            }
        }
    }
}