@cargochain/sdk-js
Version:
The CargoChain platform allows application developers to build supply chain solutions that enable the secure distribution of cargo information among trusted partners.
346 lines (306 loc) • 10.6 kB
JavaScript
/**
* This class provides all the methods that allows to manipulate the profiles (create
* profile, add events, set alias,...).
*/
class ProfileClient {
constructor(cargoChainClient) {
this._client = cargoChainClient;
}
/**
* Create new profiles in CargoChain.
* @param {Object[]} data - The list of profiles that have to be created in CargoChain.
* @returns {Promise<Object[]>} An array of record for each profile that has been
* created.
*/
createProfiles(data) {
return this._client._post("/createProfiles", data);
}
/**
* Gets a profile by Secret ID.
* @param {string} profileSecretId - A valid Profile Secret Id.
* @returns {Promise<Object>} The profile.
*/
getProfile(profileSecretId) {
return this._client._get("/getProfile", { profileSecretId });
}
/**
* Gets a set of profiles by Secret ID.
* @param {string[]} profileSecretIds - An array of valid Profile Secret Ids.
* @returns {Promise<Object[]>} An array of profiles.
*/
getProfiles(profileSecretIds) {
return this._client._get("/getProfiles", { profileSecretIds });
}
/**
* Gets all profiles.
* @param {object} page - The page info.
* @returns {Promise<Object[]>} An array of profiles.
*/
getAllProfiles(page) {
return this._client._getPagedResponse("/getAllProfiles", page);
}
/**
* Gets a profile by Public ID.
* @param {string} profilePublicId - A valid Profile Public Id.
* @returns {Promise<Object>} The profile.
*/
getProfileByPublicId(profilePublicId) {
return this._client._get("/getProfileByPublicId", { profilePublicId });
}
/**
* Gets the public Barcode or QR Code for the given profile.
* @param {Object} data - { profileSecretId, barcodeformat }
*/
getQrCode(data) {
return this._client._get("/getQrCode", data);
}
/**
* Shares profiles with organisations or individuals.
* @param {Object[]} data
*/
share(data) {
return this._client._post("/share", data);
}
/**
* Reshares profiles with organisations or individuals.
* @param {Object[]} data
*/
reshare(data) {
return this._client._post("/reshare", data);
}
/**
* Releases profiles to organisations or individuals.
* @param {Object[]} data
*/
release(data) {
return this._client._post("/release", data);
}
/**
* Accepts profiles that have been released to your organisation.
* @param {Object[]} data
*/
accept(data) {
return this._client._post("/accept", data);
}
/**
* Rejects profiles that have been released to your organisation.
* @param {Object[]} data
*/
reject(data) {
return this._client._post("/reject", data);
}
/**
* Stuffs children profiles into a parent profile.
* @param {Object[]} data
*/
stuff(data) {
return this._client._post("/stuff", data);
}
/**
* Unstuffs children profiles from a parent profile.
* @param {Object[]} data
*/
unstuff(data) {
return this._client._post("/unstuff", data);
}
/**
* Gets the profile hierarchy info (parents and children profiles).
* @param {string} profileSecretId
*/
getHierarchyInfo(profileSecretId) {
return this._client._get("/getHierarchyInfo", { profileSecretId });
}
/**
* Link a profile to other profiles.
* @param {Object[]} data
*/
link(data) {
return this._client._post("/link", data);
}
/**
* Unlink a profile from other profiles.
* @param {Object[]} data
*/
unlink(data) {
return this._client._post("/unlink", data);
}
/**
* Gets the profiles that are linked to the specified profile.
* @param {string} profileSecretId
*/
getLinkedProfiles(profileSecretId) {
return this._client._get("/getLinkedProfiles", { profileSecretId });
}
/**
* Associates an alias (organisation scope) to profiles.
* @param {Object[]} data
*/
setAlias(aliases) {
return this._client._post("/setAlias", aliases);
}
/**
* Publishes information (events) to profiles.
* @param {Object[]} data
*/
addEvents(events) {
return this._client._post("/addEvents", events);
}
/**
* Adds hazard to a profile.
* @param {Object[]} data - An array of object: { profileSecretId, hazardousMaterialIds }
*/
addHazardousMaterial(data) {
return this._client._post("/addHazardousMaterial", data);
}
/**
* Uploads a file.
* The file upload is done in two steps. First you publish an event that contains a property (or more)
* with a File or Files datatype. The generated event will contain a fileId for each file. Then you
* have to call the 'upload' function in order to push the content of the file.
* @param {string} profileSecretId - The Profile Secret Id.
* @param {Object} file - The File object.
*/
upload(profileSecretId, file) {
return this._client._post("/upload?id=" + profileSecretId, file);
}
/**
* Gets the file URL.
* @param {string} id - The file Id.
* @param {boolean} [false] attachment
*/
getFileUrl(id, attachment) {
attachment = (attachment === true).toString();
return `${this._client.getUrl()}/file?id=${id}&attachment=${attachment}&access_token=${this._client.getAccessToken()}`;
}
/**
* Gets the file thumbnail URL (for images only).
* @param {string} id - The file Id.
*/
getThumbnailUrl(id) {
return `${this._client.getUrl()}/thumbnail?id=${id}&access_token=${this._client.getAccessToken()}`;
}
/**
* Retrieves the events of a given profile.
* @param {Object} profileSecretId
* @param {string} lastEvent
* @param {object} page - The page info.
*/
getEvents(profileSecretId, lastEvent, page) {
return this._client._getPagedResponse("/getEvents", page, { profileSecretId, lastEvent });
}
/**
* Retrieves the events of a given profile by event hash.
* @param {Object} data - { profileSecretId, hashes }
*/
getEventsByHash(data) {
return this._client._get("/getEventsByHash", data);
}
/**
* Gets the ledger data for the given profile.
* @param {string} profileSecretId
* @param {object} page - The page info.
*/
getLedger(profileSecretId, page) {
return this._client._getPagedResponse("/getLedger", page, { profileSecretId });
}
/**
* Attaches metadata to profiles. The metadata are not shared between organisations.
* Each organisation has it own set of metadata per profile.
* @param {Object[]} data - An array of metadata: { profileSecretId, key, value }
*/
setMetadata(data) {
return this._client._post("/setMetadata", data);
}
/**
* Gets the metadata attached to a profile.
* @param {Object} data - Metadata: { profileSecretId, key }
*/
getMetadata(data) {
return this._client._get("/getMetadata", data);
}
/**
* Deletes metadata.
* @param {Object} data - Metadata: { profileSecretId, key }
*/
deleteMetadata(data) {
return this._client._post("/deleteMetadata", data);
}
/**
* Searches profiles. The search is applied on the Profile Public ID,
* your alias and the Profile Type.
* @param {string} query
* @param {object} page - The page info.
*/
searchProfiles(query, page) {
return this._client._getPagedResponse("/searchProfiles", page, { query });
}
/**
* Searches profiles by metadata.
* @param {Object} data - Criteria: { key, value }
* @param {object} page - The page info.
*/
searchProfilesByMetadata(data, page) {
return this._client._getPagedResponse("/searchProfilesByMetadata", page, data);
}
/**
* Registers a hook for your organisation.
* @param {Object} data - Hook details: { organisationHookType, uri, email, emailReturnUrl }
*/
registerOrganisationHook(data) {
return this._client._post("/registerOrganisationHook", data);
}
/**
* Gets your organisation hooks.
* @param {object} page - The page info.
*/
getOrganisationHooks(page) {
return this._client._getPagedResponse("/getOrganisationHooks", page);
}
/**
* Unsubscribes a hook.
* @param {string} id - The subscription Id.
*/
deleteOrganisationHook(id) {
return this._client._post("/deleteOrganisationHook?id=" + id);
}
/**
* Registers a hook for a profile.
* @param {Object} data - Hook details: { profileSecretId, eventTypes, uri, email, emailReturnUrl }
*/
registerProfileHook(data) {
return this._client._post("/registerProfileHook", data);
}
/**
* Gets your profile hooks.
* @param {object} page - The page info.
*/
getProfileHooks(page) {
return this._client._getPagedResponse("/getProfileHooks", page);
}
/**
* Unsubscribes a hook.
* @param {string} id - The subscription Id.
*/
deleteProfileHook(id) {
return this._client._post("/deleteProfileHook?id=" + id);
}
/**
* Gets the calls history for the hook (organization or profile hook).
* @param {object} page - The page info.
* @param {string} id - The subscription Id.
*/
getHookHistory(id, page) {
return this._client._getPagedResponse("/getHookHistory", page, { id });
}
/**
* Tests a hook. By calling this endpoint, the given hook is triggered.
* If the hook is a URL, a test payload is sent to your webhook. Otherwise,
* if the hook is an email address, an generic test email is sent to this
* address email.
* @param {string} id - The subscription Id.
*/
pingHook(id) {
return this._client._post("/pingHook?id=" + id);
}
}
module.exports = ProfileClient;