github.com/schmorrison/Zoho@v1.1.4/crm/notes.go (about)

     1  package crm
     2  
     3  import (
     4  	"fmt"
     5  
     6  	zoho "github.com/schmorrison/Zoho"
     7  )
     8  
     9  // GetNotes returns a list of all notes
    10  // https://www.zoho.com/crm/help/api/v2/#notes-api
    11  func (c *API) GetNotes(params map[string]zoho.Parameter) (data NotesResponse, err error) {
    12  	endpoint := zoho.Endpoint{
    13  		Name:         "notes",
    14  		URL:          fmt.Sprintf("https://www.zohoapis.%s/crm/v2/Notes", c.ZohoTLD),
    15  		Method:       zoho.HTTPGet,
    16  		ResponseData: &NotesResponse{},
    17  		URLParameters: map[string]zoho.Parameter{
    18  			"page":     "",
    19  			"per_page": "200",
    20  		},
    21  	}
    22  
    23  	if len(params) > 0 {
    24  		for k, v := range params {
    25  			endpoint.URLParameters[k] = v
    26  		}
    27  	}
    28  
    29  	err = c.Zoho.HTTPRequest(&endpoint)
    30  	if err != nil {
    31  		return NotesResponse{}, fmt.Errorf("Failed to retrieve notes: %s", err)
    32  	}
    33  
    34  	if v, ok := endpoint.ResponseData.(*NotesResponse); ok {
    35  		return *v, nil
    36  	}
    37  
    38  	return NotesResponse{}, fmt.Errorf("Data returned was not 'NotesResponse'")
    39  }
    40  
    41  // GetNote returns the note specified by ID and module
    42  // https://www.zoho.com/crm/help/api/v2/#get-spec-notes-data
    43  func (c *API) GetNote(module Module, id string) (data NotesResponse, err error) {
    44  	endpoint := zoho.Endpoint{
    45  		Name: "notes",
    46  		URL: fmt.Sprintf(
    47  			"https://www.zohoapis.%s/crm/v2/%s/%s/Notes",
    48  			c.ZohoTLD,
    49  			module,
    50  			id,
    51  		),
    52  		Method:       zoho.HTTPGet,
    53  		ResponseData: &NotesResponse{},
    54  	}
    55  	err = c.Zoho.HTTPRequest(&endpoint)
    56  	if err != nil {
    57  		return NotesResponse{}, fmt.Errorf("Failed to retrieve notes: %s", err)
    58  	}
    59  
    60  	if v, ok := endpoint.ResponseData.(*NotesResponse); ok {
    61  		return *v, nil
    62  	}
    63  
    64  	return NotesResponse{}, fmt.Errorf("Data returned was not 'NotesResponse'")
    65  }
    66  
    67  // NotesResponse is the data returned by GetNotes and GetNote
    68  type NotesResponse struct {
    69  	Data []struct {
    70  		Owner struct {
    71  			Name string `json:"name,omitempty"`
    72  			ID   string `json:"id,omitempty"`
    73  		} `json:"Owner,omitempty"`
    74  		SeModule string `json:"$se_module,omitempty"`
    75  		Approval struct {
    76  			Delegate bool `json:"delegate,omitempty"`
    77  			Approve  bool `json:"approve,omitempty"`
    78  			Reject   bool `json:"reject,omitempty"`
    79  		} `json:"$approval,omitempty"`
    80  		ModifiedBy struct {
    81  			Name string `json:"name,omitempty"`
    82  			ID   string `json:"id,omitempty"`
    83  		} `json:"Modified_By,omitempty"`
    84  		ModifiedTime Time `json:"Modified_Time,omitempty"`
    85  		CreatedTime  Time `json:"Created_Time,omitempty"`
    86  		Followed     bool `json:"$followed,omitempty"`
    87  		ParentID     struct {
    88  			Name string `json:"name,omitempty"`
    89  			ID   string `json:"id,omitempty"`
    90  		} `json:"Parent_Id,omitempty"`
    91  		ID        string `json:"id,omitempty"`
    92  		CreatedBy struct {
    93  			Name string `json:"name,omitempty"`
    94  			ID   string `json:"id,omitempty"`
    95  		} `json:"Created_By,omitempty"`
    96  		NoteTitle   string `json:"Note_Title,omitempty"`
    97  		NoteContent string `json:"Note_Content,omitempty"`
    98  	} `json:"data,omitempty"`
    99  	Info PageInfo `json:"info,omitempty"`
   100  }
   101  
   102  // CreateNotes will create multiple notes provided in the request data
   103  // https://www.zoho.com/crm/help/api/v2/#create-notes
   104  func (c *API) CreateNotes(request CreateNoteData) (data CreateNoteResponse, err error) {
   105  	endpoint := zoho.Endpoint{
   106  		Name:         "notes",
   107  		URL:          fmt.Sprintf("https://www.zohoapis.%s/crm/v2/Notes", c.ZohoTLD),
   108  		Method:       zoho.HTTPPost,
   109  		ResponseData: &CreateNoteResponse{},
   110  		RequestBody:  request,
   111  	}
   112  
   113  	err = c.Zoho.HTTPRequest(&endpoint)
   114  	if err != nil {
   115  		return CreateNoteResponse{}, fmt.Errorf("Failed to create notes: %s", err)
   116  	}
   117  
   118  	if v, ok := endpoint.ResponseData.(*CreateNoteResponse); ok {
   119  		return *v, nil
   120  	}
   121  
   122  	return CreateNoteResponse{}, fmt.Errorf("Data returned was not 'CreateNoteResponse'")
   123  }
   124  
   125  // CreateNoteData is the data provided to create 1 or more notes
   126  type CreateNoteData struct {
   127  	Data []struct {
   128  		NoteTitle   string `json:"Note_Title,omitempty"`
   129  		NoteContent string `json:"Note_Content,omitempty"`
   130  		ParentID    string `json:"Parent_Id,omitempty"`
   131  		SeModule    string `json:"se_module,omitempty"`
   132  	} `json:"data,omitempty"`
   133  }
   134  
   135  // CreateNoteResponse is the data returned by CreateNotes
   136  type CreateNoteResponse struct {
   137  	Data []struct {
   138  		Message string `json:"message,omitempty"`
   139  		Details struct {
   140  			CreatedBy struct {
   141  				ID   string `json:"id,omitempty"`
   142  				Name string `json:"name,omitempty"`
   143  			} `json:"created_by,omitempty"`
   144  			ID         string `json:"id,omitempty"`
   145  			ModifiedBy struct {
   146  				ID   string `json:"id,omitempty"`
   147  				Name string `json:"name,omitempty"`
   148  			} `json:"modified_by,omitempty"`
   149  			ModifiedTime Time `json:"modified_time,omitempty"`
   150  			CreatedTime  Time `json:"created_time,omitempty"`
   151  		} `json:"details,omitempty"`
   152  		Status string `json:"status,omitempty"`
   153  		Code   string `json:"code,omitempty"`
   154  	} `json:"data,omitempty"`
   155  }
   156  
   157  // CreateRecordNote will create a note on the specified record of the specified module
   158  // https://www.zoho.com/crm/help/api/v2/#create-spec-notes
   159  func (c *API) CreateRecordNote(
   160  	request CreateRecordNoteData,
   161  	module Module,
   162  	recordID string,
   163  ) (data CreateRecordNoteResponse, err error) {
   164  	endpoint := zoho.Endpoint{
   165  		Name: "notes",
   166  		URL: fmt.Sprintf(
   167  			"https://www.zohoapis.%s/crm/v2/%s/%s/Notes",
   168  			c.ZohoTLD,
   169  			module,
   170  			recordID,
   171  		),
   172  		Method:       zoho.HTTPPost,
   173  		ResponseData: &CreateRecordNoteResponse{},
   174  		RequestBody:  request,
   175  	}
   176  
   177  	err = c.Zoho.HTTPRequest(&endpoint)
   178  	if err != nil {
   179  		return CreateRecordNoteResponse{}, fmt.Errorf("Failed to retrieve notes: %s", err)
   180  	}
   181  
   182  	if v, ok := endpoint.ResponseData.(*CreateRecordNoteResponse); ok {
   183  		return *v, nil
   184  	}
   185  
   186  	return CreateRecordNoteResponse{}, fmt.Errorf(
   187  		"Data returned was not 'CreateRecordNoteResponse'",
   188  	)
   189  }
   190  
   191  // CreateRecordNoteResponse is the data returned by CreateRecordNote, it is the same as the data returned by CreateNote
   192  type CreateRecordNoteResponse = CreateNoteResponse
   193  
   194  // CreateRecordNoteData is the data returned by CreateRecordNote
   195  type CreateRecordNoteData struct {
   196  	Data []struct {
   197  		NoteTitle   string `json:"Note_Title,omitempty"`
   198  		NoteContent string `json:"Note_Content,omitempty"`
   199  	} `json:"data,omitempty"`
   200  }
   201  
   202  // UpdateNote will update the note data of the specified note on the specified record of the module
   203  // https://www.zoho.com/crm/help/api/v2/#update-notes
   204  func (c *API) UpdateNote(
   205  	request UpdateNoteData,
   206  	module Module,
   207  	recordID, noteID string,
   208  ) (data UpdateNoteResponse, err error) {
   209  	endpoint := zoho.Endpoint{
   210  		Name: "notes",
   211  		URL: fmt.Sprintf(
   212  			"https://www.zohoapis.%s/crm/v2/%s/%s/Notes/%s",
   213  			c.ZohoTLD,
   214  			module,
   215  			recordID,
   216  			noteID,
   217  		),
   218  		Method:       zoho.HTTPPut,
   219  		ResponseData: &UpdateNoteResponse{},
   220  		RequestBody:  request,
   221  	}
   222  
   223  	err = c.Zoho.HTTPRequest(&endpoint)
   224  	if err != nil {
   225  		return UpdateNoteResponse{}, fmt.Errorf("Failed to update notes: %s", err)
   226  	}
   227  
   228  	if v, ok := endpoint.ResponseData.(*UpdateNoteResponse); ok {
   229  		return *v, nil
   230  	}
   231  
   232  	return UpdateNoteResponse{}, fmt.Errorf("Data returned was not 'UpdateNoteResponse'")
   233  }
   234  
   235  // UpdateNoteResponse is the data returned by UpdateNote
   236  type UpdateNoteResponse = CreateNoteResponse
   237  
   238  // UpdateNoteData is the data required by UpdateNote
   239  type UpdateNoteData = CreateRecordNoteData
   240  
   241  // DeleteNote will delete the specified note on the specified record from the module
   242  // https://www.zoho.com/crm/help/api/v2/#delete-notes
   243  func (c *API) DeleteNote(
   244  	module Module,
   245  	recordID, noteID string,
   246  ) (data DeleteNoteResponse, err error) {
   247  	endpoint := zoho.Endpoint{
   248  		Name: "notes",
   249  		URL: fmt.Sprintf(
   250  			"https://www.zohoapis.%s/crm/v2/%s/%s/Notes/%s",
   251  			c.ZohoTLD,
   252  			module,
   253  			recordID,
   254  			noteID,
   255  		),
   256  		Method:       zoho.HTTPDelete,
   257  		ResponseData: &DeleteNoteResponse{},
   258  	}
   259  
   260  	err = c.Zoho.HTTPRequest(&endpoint)
   261  	if err != nil {
   262  		return DeleteNoteResponse{}, fmt.Errorf("Failed to delete note: %s", err)
   263  	}
   264  
   265  	if v, ok := endpoint.ResponseData.(*DeleteNoteResponse); ok {
   266  		return *v, nil
   267  	}
   268  
   269  	return DeleteNoteResponse{}, fmt.Errorf("Data returned was not 'DeleteNoteResponse'")
   270  }
   271  
   272  // DeleteNotes will delete all notes specified in the IDs
   273  // https://www.zoho.com/crm/help/api/v2/#delete-bulk-notes
   274  func (c *API) DeleteNotes(IDs ...string) (data DeleteNoteResponse, err error) {
   275  	idStr := ""
   276  	for i, a := range IDs {
   277  		idStr += a
   278  		if i < len(IDs)-1 {
   279  			idStr += ","
   280  		}
   281  	}
   282  	endpoint := zoho.Endpoint{
   283  		Name:         "notes",
   284  		URL:          fmt.Sprintf("https://www.zohoapis.%s/crm/v2/Notes", c.ZohoTLD),
   285  		Method:       zoho.HTTPDelete,
   286  		ResponseData: &DeleteNoteResponse{},
   287  		URLParameters: map[string]zoho.Parameter{
   288  			"ids": "",
   289  		},
   290  	}
   291  
   292  	err = c.Zoho.HTTPRequest(&endpoint)
   293  	if err != nil {
   294  		return DeleteNoteResponse{}, fmt.Errorf("Failed to delete notes: %s", err)
   295  	}
   296  
   297  	if v, ok := endpoint.ResponseData.(*DeleteNoteResponse); ok {
   298  		return *v, nil
   299  	}
   300  
   301  	return DeleteNoteResponse{}, fmt.Errorf("Data returned was not 'DeleteNoteResponse'")
   302  }
   303  
   304  // DeleteNoteResponse is the data returned when deleting a note
   305  type DeleteNoteResponse struct {
   306  	Data []struct {
   307  		Code    string `json:"code"`
   308  		Details struct {
   309  			ID string `json:"id"`
   310  		} `json:"details"`
   311  		Message string `json:"message"`
   312  		Status  string `json:"status"`
   313  	} `json:"data"`
   314  }