async function createOrUpdateDatasetView()

in api/v1/src/datasets/dataManager.js [435:503]


async function createOrUpdateDatasetView(projectId, datasetId, viewId, view, createdBy) {
    let _viewId = viewId;
    if (viewId) {
        const currentView = await getDatasetView(projectId, datasetId, viewId);
        if (currentView.success && currentView.data.rowId !== view.rowId) {
            return { success: false, code: 500, errors: ["View has been modified in another session."] };
        }
    }
    else {
        _viewId = uuidv4();
    }

    // Perform validation
    console.log('performing validation');
    let configValidator = new ConfigValidator();
    const result = await configValidator.validate(view);
    console.log(`validation response: ${JSON.stringify(result)}`);
    if (!result.isValid) {
        return { success: false, data: result, code: 400, errors: ['View validation failed'] };
    }

    const viewSql = await sqlBuilder.generateSql(view);

    const rowId = uuidv4();
    const isDeleted = false;
    const createdAt = new Date().toISOString();

    let data = {
        rowId: rowId,
        authorizedViewId: _viewId,
        name: view.name,
        description: view.description,
        datasetId: view.datasetId,
        createdBy: createdBy,
        createdAt: createdAt,
        isDeleted: isDeleted,
        viewSql: viewSql
    };

    if (view.source) {
        data.source = {};
        Object.assign(data.source, view.source);
    }
    if (view.custom) {
        data.custom = {};
        Object.assign(data.custom, view.custom);
    }
    if (view.accessControl) {
        data.accessControl = {};
        Object.assign(data.accessControl, view.accessControl);
    }
    if (view.expiration) {
        data.expiration = {};
        Object.assign(data.expiration, view.expiration);

        if (view.expiration.time) {
            data.expiration.time = new Date(view.expiration.time);
        }
        if (!Object.prototype.hasOwnProperty.call(view.expiration, 'delete')) {
            data.expiration.delete = false;
        }
    }

    console.log(data);

    const bigqueryUtil = new BigQueryUtil(projectId);
    await bigqueryUtil.insertRows(cfg.cdsDatasetId, cfg.cdsAuthorizedViewTableId, data);
    return await createView(view);
}