11import cloneDeep from 'lodash/cloneDeep'
22import error from '../core/contentstackError'
3- import { create , deleteEntity , fetch , fetchAll , update } from '../entity'
3+ import { create , deleteEntity , fetch , update } from '../entity'
44import { Authorization } from './authorization'
55import { Hosting } from './hosting'
6- import { Installation } from './installation'
6+ import { Installation , InstallationCollection } from './installation'
77import { Oauth } from './oauth'
8+ import ContentstackCollection from '../contentstackCollection'
89
910export function App ( http , data ) {
1011 http . defaults . versioningStrategy = undefined
@@ -26,25 +27,25 @@ export function App (http, data) {
2627 this . urlPath = `/manifests/${ this . uid } `
2728
2829 /**
29- * @description The update manifest call is used to update the app details such as name, description, icon, and so on.
30- * @memberof App
31- * @func update
32- * @returns {Promise<App> }
33- *
34- * @example
35- * import * as contentstack from '@contentstack/management'
36- * const client = contentstack.client({ authtoken: 'TOKEN'})
37- * const updateApp = {
38- * name: 'APP_NAME',
39- * description: 'APP_DESCRIPTION',
40- * target_type: 'stack'/'organization',
41- * }
42- * const app = client.organization('organization_uid').app('manifest_uid')
43- * app = Object.assign(app, updateApp)
44- * app.update()
45- * .then((app) => console.log(app))
46- *
47- */
30+ * @description The update manifest call is used to update the app details such as name, description, icon, and so on.
31+ * @memberof App
32+ * @func update
33+ * @returns {Promise<App> }
34+ *
35+ * @example
36+ * import * as contentstack from '@contentstack/management'
37+ * const client = contentstack.client({ authtoken: 'TOKEN'})
38+ * const updateApp = {
39+ * name: 'APP_NAME',
40+ * description: 'APP_DESCRIPTION',
41+ * target_type: 'stack'/'organization',
42+ * }
43+ * const app = client.organization('organization_uid').app('manifest_uid')
44+ * app = Object.assign(app, updateApp)
45+ * app.update()
46+ * .then((app) => console.log(app))
47+ *
48+ */
4849 this . update = update ( http , undefined , this . params )
4950
5051 /**
@@ -142,26 +143,34 @@ export function App (http, data) {
142143 }
143144
144145 /**
145- * @description The Installation will allow you to fetch, update and delete of the app installation.
146+ * @description The upgrade call is used to upgrade the installation of an app
146147 * @memberof App
147- * @func installation
148- * @param {String } uid Installation uid
149- * @returns Installation
150- *
151- * @example
152- * import * as contentstack from '@contentstack/management'
153- * const client = contentstack.client({ authtoken: 'TOKEN'})
154- * client.organization('organization_uid').app('manifest_uid').installation().findAll()
155- * .then((installations) => console.log(installations))
148+ * @func upgrade
149+ * @param {String } param.targetType - The target on which app needs to be installed, stack or ogranization.
150+ * @param {String } param.targetUid - The uid of the target, on which the app will be installed
151+ * @returns Promise<Installation>
156152 *
157153 * @example
158154 * import * as contentstack from '@contentstack/management'
159155 * const client = contentstack.client({ authtoken: 'TOKEN'})
160- * client.organization('organization_uid').app('manifest_uid').installation('installation_uid').fetch( )
156+ * client.organization('organization_uid').app('manifest_uid').install({ targetUid: 'STACK_API_KEY', targetType: 'stack' } )
161157 * .then((installation) => console.log(installation))
162158 */
163- this . installation = ( uid = null ) => {
164- return new Installation ( http , uid ? { data : { uid } } : { app_uid : this . uid } , this . params )
159+ this . upgrade = async ( { targetUid, targetType } ) => {
160+ try {
161+ const headers = {
162+ headers : { ...cloneDeep ( this . params ) }
163+ } || { }
164+
165+ const response = await http . put ( `${ this . urlPath } /reinstall` , { target_type : targetType , target_uid : targetUid } , headers )
166+ if ( response . data ) {
167+ return new Installation ( http , response . data , this . params ) || { }
168+ } else {
169+ throw error ( response )
170+ }
171+ } catch ( err ) {
172+ throw error ( err )
173+ }
165174 }
166175 /**
167176 * @description The GET app requests of an app call is used to retrieve all requests of an app.
@@ -266,9 +275,20 @@ export function App (http, data) {
266275 * .then((collection) => console.log(collection))
267276 *
268277 */
269- this . listInstallations = ( ) => {
270- this . urlPath = `manifests/${ data . app_uid } /installations`
271- return fetchAll ( http , InstallationCollection , this . params )
278+ this . listInstallations = async ( ) => {
279+ try {
280+ const headers = {
281+ headers : { ...cloneDeep ( this . params ) , ...cloneDeep ( this . headers ) }
282+ }
283+ const response = await http . get ( `manifests/${ this . uid } /installations` , headers )
284+ if ( response . data ) {
285+ return new ContentstackCollection ( response , http , this . stackHeaders , InstallationCollection )
286+ } else {
287+ throw error ( response )
288+ }
289+ } catch ( err ) {
290+ throw error ( err )
291+ }
272292 }
273293 } else {
274294 /**
@@ -301,69 +321,7 @@ export function App (http, data) {
301321 *
302322 */
303323 this . create = create ( { http, params : this . params } )
304-
305- /**
306- * @description The get all manifest call is used to fetch all the apps in your Contentstack organization.
307- * @memberof App
308- * @func findAll
309- * @returns {Promise<ContentstackCollection<App>> }
310- *
311- * @example
312- * import * as contentstack from '@contentstack/management'
313- * const client = contentstack.client({ authtoken: 'TOKEN'})
314- *
315- * client.organization('organization_uid').app().findAll()
316- * .then((collection) => console.log(collection))
317- *
318- */
319- this . findAll = fetchAll ( http , AppCollection , this . params )
320-
321- /**
322- * @description To get the apps list of authorized apps for the particular organization
323- * @memberof Organization
324- * @func authorizedApps
325- * @param {number } skip - Offset for skipping content in the response.
326- * @param {number } limit - Limit on api response to provide content in list.
327- * @example
328- * import * as contentstack from '@contentstack/management'
329- * const client = contentstack.client()
330- *
331- * client.organization('organization_uid').authorizedApps({ skip: 10 })
332- * .then((roles) => console.log(roles))
333- *
334- */
335- this . findAllAuthorized = async ( param = { } ) => {
336- const headers = {
337- headers : { ...cloneDeep ( this . params ) }
338- }
339-
340- headers . params = { ...param }
341- try {
342- const response = await http . get ( `/authorized-apps` , headers )
343- if ( response . data ) {
344- return response . data
345- } else {
346- return error ( response )
347- }
348- } catch ( err ) {
349- return error ( err )
350- }
351- }
352324 }
353325 }
354326 return this
355327}
356-
357- export function AppCollection ( http , data ) {
358- const obj = cloneDeep ( data . data ) || [ ]
359- return obj . map ( ( appData ) => {
360- return new App ( http , { data : appData } )
361- } )
362- }
363-
364- function InstallationCollection ( http , data ) {
365- const obj = cloneDeep ( data . data ) || [ ]
366- return obj . map ( ( installationData ) => {
367- return new Installation ( http , { data : installationData } )
368- } )
369- }
0 commit comments