diff --git a/packages/server/appPackages/master/appDefinition.json b/packages/server/appPackages/master/appDefinition.json index e9399f759a..d5d0748c8c 100644 --- a/packages/server/appPackages/master/appDefinition.json +++ b/packages/server/appPackages/master/appDefinition.json @@ -502,6 +502,12 @@ "behaviourSource": "main", "behaviourName": "createNewUser", "initialOptions": {} + }, + "set_default_version": { + "name": "set_default_version", + "behaviourSource": "main", + "behaviourName": "setDefaultVersion", + "initialOptions": {} } }, "triggers": [ diff --git a/packages/server/appPackages/master/main.js b/packages/server/appPackages/master/main.js index ac0ff8a736..62403902f6 100644 --- a/packages/server/appPackages/master/main.js +++ b/packages/server/appPackages/master/main.js @@ -86,6 +86,22 @@ module.exports = (context) => { authUser.accessLevels = [instance.version.defaultAccessLevel]; await instanceApis.authApi.createUser(authUser); + }, + + setDefaultVersion : async ({apis, version}) => { + const appKey = $(version.key, [ + splitKey, + take(2), + joinKey + ]); + + const application = await apis.recordApi.load(appKey); + + if(application.defaultVersion.key) return; + + application.defaultVersion = version; + + await apis.recordApi.save(application); } }); diff --git a/packages/server/appPackages/testApp/appDefinition.json b/packages/server/appPackages/testApp/appDefinition.json index a303f152d4..58f80233fb 100644 --- a/packages/server/appPackages/testApp/appDefinition.json +++ b/packages/server/appPackages/testApp/appDefinition.json @@ -1 +1,86 @@ -{"hierarchy":{"name":"root","type":"root","children":[{"name":"customer","type":"record","fields":[{"name":"name","type":"string","typeOptions":{"maxLength":1000,"values":null,"allowDeclaredValuesOnly":false},"label":"name","getInitialValue":"default","getUndefinedValue":"default"}],"children":[{"name":"invoiceyooo","type":"record","fields":[{"name":"amount","type":"number","typeOptions":{"minValue":99999999999,"maxValue":99999999999,"decimalPlaces":2},"label":"amount","getInitialValue":"default","getUndefinedValue":"default"}],"children":[],"validationRules":[],"nodeId":2,"indexes":[],"allidsShardFactor":1,"collectionName":"invoices","isSingle":false}],"validationRules":[],"nodeId":1,"indexes":[],"allidsShardFactor":64,"collectionName":"customers","isSingle":false}],"pathMaps":[],"indexes":[],"nodeId":0},"triggers":[{"actionName":"output_to_file","eventName":"authApi:createUser:onComplete","optionsCreator":"return { filename:'tempaccess' + context.user.name, content:context.result.tempCode };","condition":"!context.password"},{"actionName":"output_to_file","eventName":"authApi:createTemporaryAccess:onComplete","optionsCreator":"return { filename:'tempaccess' + context.userName, content:context.result };","condition":""}],"actions":{"output_to_file":[{"name":"output_to_file","behaviourSource":"main","behaviourName":"outputToFile","initialOptions":{}}]}} \ No newline at end of file +{ + "hierarchy": { + "name": "root", + "type": "root", + "children": [ + { + "name": "customer", + "type": "record", + "fields": [ + { + "name": "name", + "type": "string", + "typeOptions": { + "maxLength": 1000, + "values": null, + "allowDeclaredValuesOnly": false + }, + "label": "name", + "getInitialValue": "default", + "getUndefinedValue": "default" + } + ], + "children": [ + { + "name": "invoiceyooo", + "type": "record", + "fields": [ + { + "name": "amount", + "type": "number", + "typeOptions": { + "minValue": 99999999999, + "maxValue": 99999999999, + "decimalPlaces": 2 + }, + "label": "amount", + "getInitialValue": "default", + "getUndefinedValue": "default" + } + ], + "children": [], + "validationRules": [], + "nodeId": 2, + "indexes": [], + "allidsShardFactor": 1, + "collectionName": "invoices", + "isSingle": false + } + ], + "validationRules": [], + "nodeId": 1, + "indexes": [], + "allidsShardFactor": 64, + "collectionName": "customers", + "isSingle": false + } + ], + "pathMaps": [], + "indexes": [], + "nodeId": 0 + }, + "triggers": [ + { + "actionName": "output_to_file", + "eventName": "authApi:createUser:onComplete", + "optionsCreator": "return { filename:'tempaccess' + context.user.name, content:context.result.tempCode };", + "condition": "!context.password" + }, + { + "actionName": "output_to_file", + "eventName": "authApi:createTemporaryAccess:onComplete", + "optionsCreator": "return { filename:'tempaccess' + context.userName, content:context.result };", + "condition": "" + } + ], + "actions": { + "output_to_file": + { + "name": "output_to_file", + "behaviourSource": "main", + "behaviourName": "outputToFile", + "initialOptions": {} + } + + } +} \ No newline at end of file diff --git a/packages/server/appPackages/testApp/dist/package.tar.gz b/packages/server/appPackages/testApp/dist/package.tar.gz index 8cd74900b6..1115b0e5a8 100644 Binary files a/packages/server/appPackages/testApp/dist/package.tar.gz and b/packages/server/appPackages/testApp/dist/package.tar.gz differ diff --git a/packages/server/appPackages/testApp/plugins.js b/packages/server/appPackages/testApp/plugins.js index 838a10a7ff..9ba245a89f 100644 --- a/packages/server/appPackages/testApp/plugins.js +++ b/packages/server/appPackages/testApp/plugins.js @@ -2,8 +2,13 @@ const fs = require("fs"); module.exports = (config) => ({ main: { - outputToFile : ({filename, content}) => { - fs.writeFile(`./tests/.data/${filename}`, content, {encoding:"utf8"}); + outputToFile : async ({filename, content}) => { + await new Promise((resolve,reject) => { + fs.writeFile(`./tests/.data/${filename}`, content, {encoding:"utf8"}, err => { + if(err) reject(); + else resolve(err); + }); + }); } } }) \ No newline at end of file diff --git a/packages/server/package.json b/packages/server/package.json index 834c3f0779..0ca5ad75c2 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -17,8 +17,8 @@ "budibase-core": "file:../core/dist", "koa": "^2.7.0", "koa-body": "^4.1.0", - "koa-session": "^5.12.0", "koa-send": "^5.0.0", + "koa-session": "^5.12.0", "koa-static": "^5.0.0", "ncp": "^2.0.0", "rimraf": "^2.6.3", diff --git a/packages/server/tests/createNewApp.js b/packages/server/tests/createNewApp.js index ec76affe0b..82e659070c 100644 --- a/packages/server/tests/createNewApp.js +++ b/packages/server/tests/createNewApp.js @@ -1,7 +1,8 @@ const statusCodes = require("../utilities/statusCodes"); const constructHierarchy = require("../utilities/constructHierarchy"); const { readFile } = require("../utilities/fsawait"); -const {getRecordApi, getAuthApi} = require("budibase-core"); +const { hierarchy } = require("budibase-core"); +const { take } = require("lodash/fp"); const masterAppDefinition = constructHierarchy( require("../appPackages/master/appDefinition.json")); const {getApisWithFullAccess} = require("../utilities/budibaseApi"); @@ -57,6 +58,15 @@ module.exports = (app) => { .set("cookie", app.credentials.masterOwner.cookie) .expect(statusCodes.OK); + const savedAppResponse = await app.get(`/_master/api/record/${newAppKey}`) + .set("cookie", app.credentials.masterOwner.cookie); + + const savedApp = savedAppResponse.body; + savedApp.defaultVersion = version1 + await app.post(`/_master/api/record/${newAppKey}`, savedApp) + .set("cookie", app.credentials.masterOwner.cookie) + .expect(statusCodes.OK); + app.apps.testApp1.version1 = version1; }); diff --git a/packages/server/utilities/masterAppInternal.js b/packages/server/utilities/masterAppInternal.js index c77d632444..d19954039a 100644 --- a/packages/server/utilities/masterAppInternal.js +++ b/packages/server/utilities/masterAppInternal.js @@ -6,7 +6,7 @@ const { const getDatastore = require("./datastore"); const getDatabaseManager = require("./databaseManager"); const {$, splitKey} = require("budibase-core").common; -const { keyBy, last } = require("lodash/fp"); +const { keyBy, last, filter } = require("lodash/fp"); const { masterAppPackage, applictionVersionPackage, @@ -30,10 +30,13 @@ module.exports = async (context) => { const bb = await getMasterApisWithFullAccess(context); let applications; - const loadApplications = async () => - applications = $(await bb.indexApi.listItems("/all_applications"), [ + const loadApplications = async () => { + const apps = await bb.indexApi.listItems("/all_applications"); + applications = $(apps, [ + filter(a => !!a.defaultVersion.key), keyBy("name") ]); + } await loadApplications(); const getInstanceDatastore = (instanceDatastoreConfig) =>