In 15 Minutes from 0 to App

< 15 Minutes from 0 to App

Learn how to build a complete employee asset management app from scratch in under 15 minutes using the AVERIO Cloud Platform

Goals

Employees have different assets provided by the company including computers, phones, tools, software licenses and other equipment.

To simplify the management of these employee assets, a simple custom cloud application is created in this tutorial.

Data Model

The following entities (models) are managed by this application:

AssetClass:

  • holds a type of asset (e.g. „computer“)

Asset:

  • identifies a specific asset item (e.g. „Max’s notebook“)
  • belongs to one AssetClass (relation: assetOfClass)
  • belongs on one User (relation: assetOwningUser)

User Roles

Users

  • can only see their own assets (read only)

AssetManager

  • can see all assets of all users
  • can manage assets

Admin

  • can set users as AssetManager

Creating a Cloud App

To create a cloud app follow these steps:

    1. create a new package com.example.assetmgr
    2. create a module folder „App“ (this name can be arbitrarily chosen)
    3. inside the /App module directory, create the module.json file
{
	//type of the module
	"type": "App",

	//the author of this module
	"author": "AVERIO",

	//the official copyright notice
	"copyright": "(c) 2020 - wwww.averiocloud.com - all rights reserved",

	//human-friendly title for this module
	"title": "Employee Asset Management App",

	//human-friendly description of this module's purpose
	"description": "App Description",

	//object containing {: | }
	"requiresModules": {}

}
    1. each Cloud App is required to have an App.js file. Let’s create this now:
 
defineApp('AssetMgr','App',{
});
  1. create subfolders for Actions and Models:
    • /App/Actions → all Actions of this App go here
    • /App/Database → will contain all Models and Relations of this App

(the folder structure and file names within a module can be freely choosen, but we recommend this convention)

Creating the Data Model

  1. create the AssetClass Model in AssetClass.js
var modelName = 'AssetClass';
AVERIO.MainDatabase.defineModel(modelName, {
})
.defineField(modelName,'name',{
	type: 'ShortText'
})
.defineField(modelName,'description',{
	type: 'ShortText'
})
.defineField(modelName,'date',{
	type: 'datetime'
})
  1. create the Asset Model in Asset.js
 
var modelName = 'Asset';
AVERIO.MainDatabase.defineModel(modelName, {
})
.defineField(modelName,'name',{
	type: 'ShortText'
})
.defineField(modelName,'type',{
	type: 'ShortText'
})
.defineField(modelName,'date',{
	type: 'datetime'
})
  1. create the file_Relations_.js which will contain all relations defined by this Cloud App. Note that the file name is just a recommendation. It will ensure that this file is always sorted to be easily located.
  1. define the two required relations:
 AVERIO.MainDatabase
.defineBelongsTo('assetOfClass','Asset','AssetClass',{})
.defineBelongsTo('assetOfUser','Asset','User',{})

Creating the Actions for AssetClasses

  1. create the file ListAssetClasses.js and add the following code to list all AssetClass records in a searchable table layout:
App.defineAction('ListAseetClasses','List',{
    modelName: 'AssetClass'
})
  1. create the file AddAssetClass.js for creating new AssetClass records:
 
App.defineAction('AddAssetClass','Add',{
	modelName: 'AssetClass'
})
  1. create the file EditAssetClass.js to modify existing AssetClass records:
App.defineAction('EditAssetClass','Edit',{
	modelName: 'AssetClass'
})
  1. create the file DeleteAssetClass.js to remove AssetClass records:
App.defineAction('DeleteAssetClass','Delete',{
	modelName: 'AssetClass'
})
  1. now it’s time to fire up this Cloud App for the first time

Creating the Actions for Assets

  1. create the file ListAssets.js:
App.defineAction('ListAssets','List',{
    modelName: 'Asset'
})
  1. create the file AddAsset.js:
App.defineAction('AddAsset','Add',{
	modelName: 'Asset'
})
  1. create the file ViewAsset.js:
App.defineAction('ViewAsset','View',{
	modelName: 'Asset'
})
  1. create the file EditAsset.js:
App.defineAction('EditAsset','Edit',{
	modelName: 'Asset'
})
  1. create the file DeleteAsset.js:
App.defineAction('DeleteAsset','Delete',{
	modelName: 'Asset'
})
  1. run the Cloud App again to see the newly added asset management features in action

Access Control

  1. add a new file Database/UserExt.js to extend the User model, which is predefined in the AVERIO Cloud Platform. Add a new Boolean field „isAssetManager“
AVERIO.MainDatabase.defineField('User','isAssetManager',{
    type: 'boolean'
})
  1. define this authorizeWrite callback to the field definition of „isAssetManager“ to make the field only editable by admins. Everyone else can still see this filed as readonly.
FieldAuthorizeWrite: async function(sessionData)
    {
        return (sessionData && sessionData.User && sessionData.User.isAdmin) ? true : false
    }
  1. add the following ActionAuthorize override method for all AssetClass actions to make them only accessible to asset managers.
 
ActionAuthorize: async function(sessionData)
    {
        return (sessionData && sessionData.User && (sessionData.User.isAdmin || sessionData.User.isActionManager)) ? true : false
    }
  1. Asset actions will be accessible to everyone, but should restrict the Assets shown for regular users. Therefore, we create authorizeRead and authorizeWrite callbacks on the Asset model. These will restrict the access to the Asset model system-wide for all Actions. Only records which fulfill the conditions below will be readable / writable.
ActionAuthorize: async function(sessionData)
    {
        return (sessionData && sessionData.User && (sessionData.User.isAdmin || sessionData.User.isActionManager)) ? true : false
    }
  1. now, start the cloud app to see the final result of this tutorial.

Request Access Now



Copyright 2020 AVERIO Connect GmbH.