|
| 1 | +/** |
| 2 | + * @author Labs64 <[email protected]> |
| 3 | + * @license Apache-2.0 |
| 4 | + * @link https://netlicensing.io |
| 5 | + * @copyright 2017 Labs64 NetLicensing |
| 6 | + */ |
| 7 | + |
| 8 | +import Service from './Service'; |
| 9 | +import Constants from '../Constants'; |
| 10 | +import CheckUtils from '../util/CheckUtils'; |
| 11 | +import FilterUtils from '../util/FilterUtils'; |
| 12 | +import itemToBundle from '../converters/itemToBundle'; |
| 13 | +import itemToLicense from '../converters/itemToLicense'; |
| 14 | +import Page from '../vo/Page'; |
| 15 | + |
| 16 | +/** |
| 17 | + * JS representation of the Bundle Service. See NetLicensingAPI for details: |
| 18 | + * https://netlicensing.io/wiki/bundle-services |
| 19 | + * |
| 20 | + * @constructor |
| 21 | + */ |
| 22 | + |
| 23 | +export default { |
| 24 | + /** |
| 25 | + * Creates new bundle with given properties.See NetLicensingAPI for details: |
| 26 | + * @see https://netlicensing.io/wiki/bundle-services#create-bundle |
| 27 | + * |
| 28 | + * determines the vendor on whose behalf the call is performed |
| 29 | + * @param context NetLicensing.Context |
| 30 | + * |
| 31 | + * non-null properties will be taken for the new object, null properties will either stay null, or will |
| 32 | + * be set to a default value, depending on property. |
| 33 | + * @param bundle NetLicensing.Bundle |
| 34 | + * |
| 35 | + * return the newly created bundle object in promise |
| 36 | + * @returns {Promise} |
| 37 | + */ |
| 38 | + |
| 39 | + async create(context, bundle) { |
| 40 | + const { data: { items: { item: items } } } = await Service |
| 41 | + .post(context, Constants.Bundle.ENDPOINT_PATH, bundle.asPropertiesMap()); |
| 42 | + |
| 43 | + const [item] = items.filter(({ type }) => type === 'Bundle'); |
| 44 | + |
| 45 | + return itemToBundle(item); |
| 46 | + }, |
| 47 | + |
| 48 | + /** |
| 49 | + * Gets bundle by its number.See NetLicensingAPI for details: |
| 50 | + * @see https://netlicensing.io/wiki/bundle-services#get-bundle |
| 51 | + * |
| 52 | + * determines the vendor on whose behalf the call is performed |
| 53 | + * @param context NetLicensing.Context |
| 54 | + * |
| 55 | + * the bundle number |
| 56 | + * @param number string |
| 57 | + * |
| 58 | + * return the bundle object in promise |
| 59 | + * @returns {Promise} |
| 60 | + */ |
| 61 | + async get(context, number) { |
| 62 | + CheckUtils.paramNotEmpty(number, Constants.NUMBER); |
| 63 | + |
| 64 | + const { data: { items: { item: items } } } = await Service |
| 65 | + .get(context, `${Constants.Bundle.ENDPOINT_PATH}/${number}`); |
| 66 | + |
| 67 | + const [item] = items.filter(({ type }) => type === 'Bundle'); |
| 68 | + |
| 69 | + return itemToBundle(item); |
| 70 | + }, |
| 71 | + |
| 72 | + /** |
| 73 | + * Returns bundle of a vendor.See NetLicensingAPI for details: |
| 74 | + * @see https://netlicensing.io/wiki/bundle-services#bundles-list |
| 75 | + * |
| 76 | + * determines the vendor on whose behalf the call is performed |
| 77 | + * @param context NetLicensing.Context |
| 78 | + * |
| 79 | + * reserved for the future use, must be omitted / set to NULL |
| 80 | + * @param filter string|null |
| 81 | + * |
| 82 | + * array of bundle entities or empty array if nothing found in promise. |
| 83 | + * @returns {Promise} |
| 84 | + */ |
| 85 | + async list(context, filter) { |
| 86 | + const queryParams = {}; |
| 87 | + |
| 88 | + if (filter) { |
| 89 | + if (!CheckUtils.isValid(filter)) throw new TypeError(`filter has bad value ${filter}`); |
| 90 | + queryParams[Constants.FILTER] = typeof filter === 'string' ? filter : FilterUtils.encode(filter); |
| 91 | + } |
| 92 | + |
| 93 | + const { data } = await Service.get(context, Constants.Bundle.ENDPOINT_PATH, queryParams); |
| 94 | + |
| 95 | + return Page( |
| 96 | + data.items.item.filter(({ type }) => type === 'Bundle').map((v) => itemToBundle(v)), |
| 97 | + data.items.pagenumber, |
| 98 | + data.items.itemsnumber, |
| 99 | + data.items.totalpages, |
| 100 | + data.items.totalitems, |
| 101 | + ); |
| 102 | + }, |
| 103 | + |
| 104 | + /** |
| 105 | + * Updates bundle properties.See NetLicensingAPI for details: |
| 106 | + * @see https://netlicensing.io/wiki/bundle-services#update-bundle |
| 107 | + * |
| 108 | + * determines the vendor on whose behalf the call is performed |
| 109 | + * @param context NetLicensing.Context |
| 110 | + * |
| 111 | + * bundle number |
| 112 | + * @param number string |
| 113 | + * |
| 114 | + * non-null properties will be updated to the provided values, null properties will stay unchanged. |
| 115 | + * @param bundle NetLicensing.Bundle |
| 116 | + * |
| 117 | + * updated bundle in promise. |
| 118 | + * @returns {Promise} |
| 119 | + */ |
| 120 | + async update(context, number, bundle) { |
| 121 | + CheckUtils.paramNotEmpty(number, Constants.NUMBER); |
| 122 | + |
| 123 | + const { data: { items: { item: items } } } = await Service |
| 124 | + .post(context, `${Constants.Bundle.ENDPOINT_PATH}/${number}`, bundle.asPropertiesMap()); |
| 125 | + |
| 126 | + const [item] = items.filter(({ type }) => type === 'Bundle'); |
| 127 | + |
| 128 | + return itemToBundle(item); |
| 129 | + }, |
| 130 | + |
| 131 | + /** |
| 132 | + * Deletes bundle.See NetLicensingAPI for details: |
| 133 | + * @see https://netlicensing.io/wiki/bundle-services#delete-bundle |
| 134 | + * |
| 135 | + * determines the vendor on whose behalf the call is performed |
| 136 | + * @param context NetLicensing.Context |
| 137 | + * |
| 138 | + * bundle number |
| 139 | + * @param number string |
| 140 | + * |
| 141 | + * if true, any entities that depend on the one being deleted will be deleted too |
| 142 | + * @param forceCascade boolean |
| 143 | + * |
| 144 | + * return boolean state of delete in promise |
| 145 | + * @returns {Promise} |
| 146 | + */ |
| 147 | + delete(context, number, forceCascade) { |
| 148 | + CheckUtils.paramNotEmpty(number, Constants.NUMBER); |
| 149 | + |
| 150 | + const queryParams = { forceCascade: Boolean(forceCascade) }; |
| 151 | + |
| 152 | + return Service.delete(context, `${Constants.Bundle.ENDPOINT_PATH}/${number}`, queryParams); |
| 153 | + }, |
| 154 | + |
| 155 | + /** |
| 156 | + * Obtain bundle.See NetLicensingAPI for details: |
| 157 | + * @see https://netlicensing.io/wiki/bundle-services#obtain-bundle |
| 158 | + * |
| 159 | + * determines the vendor on whose behalf the call is performed |
| 160 | + * @param context NetLicensing.Context |
| 161 | + * |
| 162 | + * bundle number |
| 163 | + * @param number string |
| 164 | + * |
| 165 | + * licensee number |
| 166 | + * @param bundleObtainParameters NetLicensing.BundleObtainParameters |
| 167 | + * |
| 168 | + * return array of licenses |
| 169 | + * @returns {Promise} |
| 170 | + */ |
| 171 | + async obtain(context, number, bundleObtainParameters) { |
| 172 | + CheckUtils.paramNotEmpty(number, Constants.NUMBER); |
| 173 | + CheckUtils.paramNotEmpty(bundleObtainParameters.getLicenseeNumber(), Constants.Licensee.LICENSEE_NUMBER); |
| 174 | + |
| 175 | + const { ENDPOINT_PATH, ENDPOINT_OBTAIN_PATH } = Constants.Bundle; |
| 176 | + |
| 177 | + const queryParams = { [Constants.Licensee.LICENSEE_NUMBER]: bundleObtainParameters.getLicenseeNumber() }; |
| 178 | + |
| 179 | + const { data: { items: { item: items } } } = await Service |
| 180 | + .post(context, `${ENDPOINT_PATH}/${number}/${ENDPOINT_OBTAIN_PATH}`, queryParams); |
| 181 | + |
| 182 | + return items.filter(({ type }) => type === 'License').map((i) => itemToLicense(i)); |
| 183 | + }, |
| 184 | +}; |
0 commit comments