Skip to content
110 changes: 107 additions & 3 deletions blocks/start/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,79 @@ const AEM_TEMPLATES = [
},
];

const ORG_CONFIG = `{
"data": {
"total": 1,
"limit": 1,
"offset": 0,
"data": [{}]
},
"permissions": {
"total": 2,
"limit": 2,
"offset": 0,
"data": [
{
"path": "CONFIG",
"groups": "{{EMAIL}}",
"actions": "write",
"comments": "The ability to set configurations for an org."
},
{
"path": "/ + **",
"groups": "{{EMAIL}}",
"actions": "write",
"comments": "The ability to create content."
}
]
},
":names": [
"data",
"permissions"
],
":version": 3,
":type": "multi-sheet"
}`;

async function fetchConfig(org, body) {
let opts;
if (body) opts = { method: 'POST', body };

return daFetch(`${DA_ORIGIN}/config/${org}/`, opts);
}

export async function loadConfig(org) {
const resp = await fetchConfig(org);

const result = { status: resp.status };

if (!resp.ok) {
if (resp.status === 403 && resp.status === 401) {
result.message = 'You are not authorized to change this organization.';
}
} else {
const json = await resp.json();
if (json) result.json = json;
}

return result;
}

export async function saveConfig(org, email, existingConfig) {
const defConfigStr = ORG_CONFIG.replaceAll('{{EMAIL}}', email);
const defConfig = JSON.parse(defConfigStr);

// Preserve the existing config
if (existingConfig?.data) defConfig.data = existingConfig;

const body = new FormData();
body.append('config', JSON.stringify(defConfig));

const resp = await fetchConfig(org, body);

return { status: resp.status };
}

class DaStart extends LitElement {
static properties = {
activeStep: { state: true },
Expand Down Expand Up @@ -140,9 +213,39 @@ class DaStart extends LitElement {

async submitForm(e) {
e.preventDefault();
const siteUrl = e.target.action;

// Check if user is signed in
if (!window.adobeIMS?.isSignedInUser()) {
this._errorText = 'You need to sign in first.';
return;
}

this._loading = true;
const opts = { method: 'PUT' };
const resp = await daFetch(e.target.action, opts);

const { status: orgLoadStatus } = await loadConfig(this.org);
if (orgLoadStatus === 404) {
// Check if user has an email address
const { email } = await window.adobeIMS.getProfile();
if (!email) {
this._errorText = 'Make sure your profile contains an email address.';
this._loading = false;
return;
}

const { status: orgSaveStatus } = await saveConfig(this.org, email);
if (orgSaveStatus !== 201) {
if (orgSaveStatus === 401 || orgSaveStatus === 403) {
this._errorText = 'You are not authorized to create this org. Check your permissions.';
} else {
this._errorText = 'The org could not be created. Check the console logs or contact an administrator.';
}
this._loading = false;
return;
}
}

const resp = await daFetch(siteUrl, { method: 'PUT' });
this._loading = false;
if (!resp.ok) {
if (resp.status === 401 || resp.status === 403) {
Expand All @@ -152,6 +255,7 @@ class DaStart extends LitElement {
}
return;
}

this.goToNextStep(e);
}

Expand Down Expand Up @@ -205,7 +309,7 @@ class DaStart extends LitElement {
</ul>
</div>
<div class="text-container">
<p>Don't forget the <a href="https://da.live/bot">AEM Code Bot</a>.</p>
<p>Don't forget to add the <a href="https://da.live/bot">AEM Code Sync App</a> to your repository.</p>
</div>
</div>
`;
Expand Down
Loading