{
  "info": {
    "name": "b2g Zoho push by company PK",
    "description": "Push one b2g company to Zoho CRM by its membercompmst_pk. The server builds the whole payload from the database, so you only need the PK.\n\nAUTH: a static secret in the header X-Zoho-Sync-Secret. No JWT, no console command. The value is params['zohoSyncSecret'] in b2g_usermgmt/config/params-{env}.php on the server you are calling. Set it in the collection variable `syncSecret`.\n\nHOW TO USE\n1. Set `baseUrl` to the b2g_usermgmt base of the environment (see the variable description).\n2. Set `syncSecret` to that environment's zohoSyncSecret.\n3. Open \"1. Push one company\", put the PK in the `companypk` query param, press Send.\n4. \"status\": true means it worked. The DB is updated automatically: one row in zohoapimain_tbl (zam_status 1) and the Account/Contact ids in zohoapidtls_tbl.\n\nBULK: put the PKs in a CSV with a column named companypk, select \"1. Push one company\", Run collection with that CSV and a 2000 ms delay. Every run is a real CRM write - do not re-run blindly.",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  },
  "variable": [
    {
      "key": "baseUrl",
      "value": "http://localhost:9002",
      "type": "string",
      "description": "b2g_usermgmt base URL, no trailing slash.\nLocal Docker direct: http://localhost:9002\nLocal via Kong:      http://localhost:8000/usermg/web\nDev server:          http://172.16.16.23:86/b2g/b2g_usermgmt/web\nDemo / Prod (AWS):   https://<host>/usermg/web  (the Kong route for usermgmt)"
    },
    {
      "key": "syncSecret",
      "value": "ab0b3f95cc3234223174463e0c21c606e46823bf389a5995",
      "type": "string",
      "description": "params['zohoSyncSecret'] of the environment you call. Treat it like a password - it can create and change CRM records."
    },
    {
      "key": "companypk",
      "value": "",
      "type": "string",
      "description": "membercompmst_pk of the company to push. Filled from the CSV in a collection run."
    }
  ],
  "item": [
    {
      "name": "1. Push one company",
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "const body = pm.response.json();",
              "pm.test('HTTP 200', () => pm.response.to.have.status(200));",
              "pm.test('status true', () => pm.expect(body.status).to.eql(true));",
              "if (body.data) {",
              "  console.log('companypk', pm.variables.get('companypk'), 'accountId', body.data.accountId, 'existing', body.data.existing, 'matchedBy', body.data.matchedBy, 'portalError', body.data.portalError);",
              "  pm.test('no portalError', () => pm.expect(body.data.portalError).to.be.null);",
              "}"
            ]
          }
        }
      ],
      "request": {
        "method": "GET",
        "header": [
          {
            "key": "X-Zoho-Sync-Secret",
            "value": "{{syncSecret}}"
          }
        ],
        "url": {
          "raw": "{{baseUrl}}/apium/zohosync/push?companypk={{companypk}}",
          "host": ["{{baseUrl}}"],
          "path": ["apium", "zohosync", "push"],
          "query": [
            {
              "key": "companypk",
              "value": "{{companypk}}",
              "description": "membercompmst_pk"
            },
            {
              "key": "force",
              "value": "1",
              "disabled": true,
              "description": "Enable only to resend identical data pushed less than 5 minutes ago."
            }
          ]
        },
        "description": "READING THE RESPONSE\n\"status\": true -> synced. \"message\" has the Zoho Account id.\n  data.existing  true = updated the Account already in Zoho, false = created a new one.\n  data.matchedBy \"recordId\" = matched the id stored from a previous sync (exact).\n                 \"crNumber\" = matched on CR number only. For a JSRS/NIBE company spot-check that Account in Zoho - CR numbers are not unique in this org.\n  data.portalError filled = Account synced but the Portal Registration record did not. Report the text to a developer.\n\nERRORS\n401 -> wrong or missing X-Zoho-Sync-Secret, or zohoSyncSecret is empty on that server.\n409 -> Zoho sync is disabled in that environment's params (zohoCRM.enabled / zohoapiUrl).\n422 -> companypk missing or not a number.\n502 -> the push failed; \"message\" says why (company not found, admin user not found, or the Zoho error). \"data\" has the zohoapi envelope when there is one.\n\nWHAT GETS UPDATED IN THE DB (automatic, nothing to do by hand)\n  zohoapimain_tbl  - one row per attempt: zam_createdby = companypk, zam_status 1 success / 2 failure, zam_data = payload, zam_api_res = Zoho response.\n  zohoapidtls_tbl  - one row per company: zad_recordID = Account id, zad_contactid = Contact id, zad_status.\n\nVERIFY\nSELECT zohoapimain_pk, zam_status, zam_createdon FROM zohoapimain_tbl WHERE zam_createdby = <pk> ORDER BY zohoapimain_pk DESC LIMIT 3;\nSELECT zad_recordID, zad_contactid, zad_status, zad_updatedon FROM zohoapidtls_tbl WHERE zad_membercompanymst_fk = <pk>;"
      }
    },
    {
      "name": "2. Check connection (optional, no write)",
      "request": {
        "method": "GET",
        "header": [
          {
            "key": "X-Zoho-Sync-Secret",
            "value": "{{syncSecret}}"
          }
        ],
        "url": {
          "raw": "{{baseUrl}}/apium/zohosync/push?companypk=0",
          "host": ["{{baseUrl}}"],
          "path": ["apium", "zohosync", "push"],
          "query": [{ "key": "companypk", "value": "0" }]
        },
        "description": "Sends an invalid PK on purpose. Nothing is pushed.\n\n422 \"companypk is required\" -> the URL and the secret are both correct; you can start pushing.\n401 -> the secret is wrong for this environment.\n404 -> the baseUrl is wrong."
      }
    }
  ]
}
