"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.run = void 0;
const { DocumentTypes } = require("@devlego/backend-core/db");
const application_1 = require("../../api/controllers/application");
/**
 * Date:
 * January 2022
 *
 * Description:
 * Add the url to the app metadata if it doesn't exist
 */
const run = (appDb) => __awaiter(void 0, void 0, void 0, function* () {
    let metadata;
    try {
        metadata = yield appDb.get(DocumentTypes.APP_METADATA);
    }
    catch (e) {
        // sometimes the metadata document doesn't exist
        // exit early instead of failing the migration
        console.error("Error retrieving app metadata. Skipping", e);
        return;
    }
    if (!metadata.url) {
        const context = {
            request: {
                body: {
                    name: metadata.name,
                },
            },
        };
        metadata.url = (0, application_1.getAppUrl)(context);
        console.log(`Adding url to app: ${metadata.url}`);
        yield appDb.put(metadata);
    }
});
exports.run = run;
