Skip to content

Commit

Permalink
Track who/when data source is updated (#3435)
Browse files Browse the repository at this point in the history
* Track who/when data source is updated

* 👕

* Address feedback from review

* 📖
  • Loading branch information
flvndvd authored Jan 25, 2024
1 parent ca1aa8b commit 921f15d
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 2 deletions.
37 changes: 37 additions & 0 deletions front/lib/api/data_sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,43 @@ export async function getDataSources(
});
}

export async function updateDataSourceConnectedBy(
auth: Authenticator,
dataSource: DataSourceType
): Promise<Result<undefined, APIError>> {
const owner = auth.workspace();
const user = auth.user();
if (!owner || !user) {
return new Err({
type: "workspace_not_found",
message: "Could not find the workspace.",
});
}

if (!auth.isAdmin()) {
return new Err({
type: "workspace_auth_error",
message:
"Only users that are `admins` for the current workspace can update data sources.",
});
}

await DataSource.update(
{
editedAt: new Date(),
editedByUserId: user.id,
},
{
where: {
id: dataSource.id,
workspaceId: owner.id,
},
}
);

return new Ok(undefined);
}

export async function deleteDataSource(
auth: Authenticator,
dataSourceName: string
Expand Down
25 changes: 25 additions & 0 deletions front/lib/models/data_source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
import { DataTypes, Model } from "sequelize";

import { front_sequelize } from "@app/lib/databases";
import { User } from "@app/lib/models/user";
import { Workspace } from "@app/lib/models/workspace";

export class DataSource extends Model<
Expand All @@ -19,6 +20,10 @@ export class DataSource extends Model<
declare createdAt: CreationOptional<Date>;
declare updatedAt: CreationOptional<Date>;

// Corresponds to the ID of the last user to configure the connection.
declare editedByUserId: ForeignKey<User["id"]>;
declare editedAt: CreationOptional<Date>;

declare name: string;
declare description: string | null;
declare assistantDefaultSelected: boolean;
Expand All @@ -28,6 +33,7 @@ export class DataSource extends Model<
declare workspaceId: ForeignKey<Workspace["id"]>;

declare workspace: NonAttribute<Workspace>;
declare editedByUser: NonAttribute<User>;
}

DataSource.init(
Expand All @@ -47,6 +53,12 @@ DataSource.init(
allowNull: false,
defaultValue: DataTypes.NOW,
},
editedAt: {
type: DataTypes.DATE,
// TODO(2024-01-25 flav) Set `allowNull` to `false` once backfilled.
allowNull: true,
defaultValue: DataTypes.NOW,
},
name: {
type: DataTypes.STRING,
allowNull: false,
Expand Down Expand Up @@ -88,3 +100,16 @@ DataSource.belongsTo(Workspace, {
as: "workspace",
foreignKey: { name: "workspaceId", allowNull: false },
});

User.hasMany(DataSource, {
as: "dataSources",
// TODO(2024-01-25 flav) Set `allowNull` to `false` once backfilled.
foreignKey: { name: "workspaceId", allowNull: true },
// /!\ We don't want to delete the data source when a user gets deleted.
onDelete: "SET NULL",
});
DataSource.belongsTo(User, {
as: "editedByUser",
// TODO(2024-01-25 flav) Set `allowNull` to `false` once backfilled.
foreignKey: { name: "editedByUserId", allowNull: true },
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import * as t from "io-ts";
import * as reporter from "io-ts-reporters";
import type { NextApiRequest, NextApiResponse } from "next";

import { getDataSource } from "@app/lib/api/data_sources";
import {
getDataSource,
updateDataSourceConnectedBy,
} from "@app/lib/api/data_sources";
import { Authenticator, getSession } from "@app/lib/auth";
import logger from "@app/logger/logger";
import { apiError, withLogging } from "@app/logger/withlogging";
Expand Down Expand Up @@ -124,6 +127,9 @@ async function handler(
});
}
}

await updateDataSourceConnectedBy(auth, dataSource);

res.status(200).json(updateRes.value);
return;

Expand Down
4 changes: 3 additions & 1 deletion front/pages/api/w/[wId]/data_sources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ async function handler(
);

const owner = auth.workspace();
const user = auth.user();
const plan = auth.plan();
if (!owner || !plan || !auth.isUser()) {
if (!owner || !plan || !user || !auth.isUser()) {
return apiError(req, res, {
status_code: 404,
api_error: {
Expand Down Expand Up @@ -164,6 +165,7 @@ async function handler(
dustAPIProjectId: dustProject.value.project.project_id.toString(),
workspaceId: owner.id,
assistantDefaultSelected: req.body.assistantDefaultSelected,
editedByUserId: user.id,
});

res.status(201).json({
Expand Down
3 changes: 3 additions & 0 deletions front/pages/api/w/[wId]/data_sources/managed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ async function handler(

const owner = auth.workspace();
const plan = auth.plan();
const user = auth.user();
if (
!owner ||
!plan ||
!user ||
// No role under "builder" can create a managed data source.
// We perform a more detailed check below for each provider,
// but this is a first line of defense.
Expand Down Expand Up @@ -297,6 +299,7 @@ async function handler(
dustAPIProjectId: dustProject.value.project.project_id.toString(),
workspaceId: owner.id,
assistantDefaultSelected,
editedByUserId: user.id,
});

const connectorsAPI = new ConnectorsAPI(logger);
Expand Down

0 comments on commit 921f15d

Please sign in to comment.