Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.7 kB
3
Indexable
Never

/**
 * Get hospital id from SI
 */
function getHospitalIdFromSI() {
  return new Promise((resolve, reject) => {
    // Make the request
    request.get(
      {
        url: config.si,
      },
      (err, res, body) => {
        if (err || res.statusCode !== 200) {
          err = err || body;
          err = new restify.InternalError(
            "getHospitalIdFromSI::request error: " + err
          );
          return reject(err);
        }

        // Parse body
        try {
          body = JSON.parse(body);
        } catch (e) {
          err = new restify.InternalError(
            "getHospitalIdFromSI::cannot parse body: " + e
          );
          return reject(err);
        }

        // send the results
        return resolve(body.items[0].hospID);
      }
    );
  });
}

/**
 * Get hospital informations from SI
 */
function getHospital() {
  return getHospitalIdFromSI().then(
    (hospID) =>
      new Promise((resolve, reject) => {
        request.get(
          {
            url: config.si + "/" + hospID,
          },
          (err, res, body) => {
            if (err || res.statusCode != 200) {
              err = err || body;
              err = new restify.InternalError(
                "getHospital::request error: " + err
              );
              return reject(err);
            }

            // Parse body
            try {
              body = JSON.parse(body);
            } catch (e) {
              err = new restify.InternalError(
                "getHospital::cannot parse body: " + e
              );
              return reject(err);
            }

            // send the results
            return resolve(body);
          }
        );
      })
  );
}