companies.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package amo
  2. import "surdeus.su/core/ss/urlenc"
  3. import "surdeus.su/core/amo/companies"
  4. import "surdeus.su/core/amo/api"
  5. import "errors"
  6. import "fmt"
  7. func (client *Client) GetCompany(
  8. companyID int,
  9. opts ...urlenc.Builder,
  10. ) (*Company, error) {
  11. deal := new(companies.Company)
  12. resource := fmt.Sprintf(
  13. "/api/v4/companies/%d?%s",
  14. companyID,
  15. urlenc.Join(opts...),
  16. )
  17. err := client.API.Get(resource, deal)
  18. if err != nil {
  19. return nil, err
  20. }
  21. return deal, nil
  22. }
  23. // Get list of leads.
  24. func (client *Client) GetCompanies(
  25. opts ...urlenc.Builder,
  26. ) ([]Company, NextFunc[[]Company], error) {
  27. res := fmt.Sprintf(
  28. "/api/v4/companies?%s",
  29. urlenc.Join(opts...).Encode(),
  30. )
  31. return client.GetCompaniesByURL(res)
  32. }
  33. func (client *Client) GetCompaniesByURL(
  34. u string,
  35. ) ([]Company, NextFunc[[]Company], error) {
  36. var fn NextFunc[[]Company]
  37. coms := companies.Companies{}
  38. err := client.API.Get(u, &coms)
  39. if err != nil {
  40. if errors.Is(err, api.ErrNoContent) {
  41. return nil, nil, nil
  42. }
  43. return nil, nil, err
  44. }
  45. //ret = append(ret, coms.Embedded.Companies...)
  46. nextHref := coms.Links.Next.Href
  47. if nextHref != "" {
  48. fn = MakeNextFunc(
  49. nextHref,
  50. client.GetCompaniesByURL,
  51. )
  52. }
  53. return coms.Embedded.Companies, fn, nil
  54. }
  55. func (client *Client) UpdateCompany(
  56. company *companies.Company,
  57. ) error {
  58. return client.updateEntity(
  59. "/api/v4/companies",
  60. company.ID,
  61. company,
  62. )
  63. }
  64. func (client *Client) UpdateCompanies(
  65. cs []companies.Company,
  66. ) ([]Company, error) {
  67. if len(cs) > MEPR {
  68. return nil, ErrTooManyEntities
  69. }
  70. //ret := []Company{}
  71. err := client.API.Patch(
  72. "/api/v4/companies",
  73. cs,
  74. nil,
  75. )
  76. if err != nil {
  77. return nil, err
  78. }
  79. return nil, nil
  80. }