Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

Refactor Inbox action sentence

天舟ad3798dabfbc
diff --git a/frontend/src/miragejs/routes/environment.js b/frontend/src/miragejs/routes/environment.js
index d0eb5a74b0..baac0121dc 100644
--- a/frontend/src/miragejs/routes/environment.js
+++ b/frontend/src/miragejs/routes/environment.js
@@ -1,6 +1,7 @@
import { Response } from "miragejs";
import { postMessageToOwnerAndDBA } from "../utils";
import { WORKSPACE_ID, OWNER_ID } from "./index";
+import { EnvironmentBuiltinFieldId } from "../../types";
export default function configureEnvironment(route) {
route.get("/environment", function (schema, request) {
@@ -81,10 +82,6 @@ export default function configureEnvironment(route) {
route.patch("/environment/:environmentId", function (schema, request) {
const attrs = this.normalizedRequestAttrs("environment-patch");
- return schema.environments.find(request.params.environmentId).update(attrs);
- });
-
- route.delete("/environment/:environmentId", function (schema, request) {
const environment = schema.environments.find(request.params.environmentId);
if (!environment) {
@@ -98,20 +95,35 @@ export default function configureEnvironment(route) {
);
}
- const instanceCount = schema.instances.where({
- environmentId: request.params.environmentId,
- }).models.length;
- if (instanceCount > 0) {
- return new Response(
- 400,
- {},
- {
- errors: `Found ${instanceCount} instance(s) in environment ${environment.name}. Environment can only be deleted if no instance resides under it.`,
- }
- );
+ const changeList = [];
+ let type = "bb.msg.environment.update";
+
+ if (attrs.rowStatus && attrs.rowStatus != environment.rowStatus) {
+ if (attrs.rowStatus == "ARCHIVED") {
+ type = "bb.msg.environment.archive";
+ } else if (attrs.rowStatus == "NORMAL") {
+ type = "bb.msg.environment.restore";
+ } else if (attrs.rowStatus == "PENDING_DELETE") {
+ type = "bb.msg.environment.delete";
+ }
+ changeList.push({
+ fieldId: EnvironmentBuiltinFieldId.ROW_STATUS,
+ oldValue: environment.rowStatus,
+ newValue: attrs.rowStatus,
+ });
}
- environment.destroy();
+ if (attrs.name && attrs.name != environment.name) {
+ changeList.push({
+ fieldId: EnvironmentBuiltinFieldId.NAME,
+ oldValue: environment.name,
+ newValue: attrs.name,
+ });
+ }
+
+ const updatedEnvironment = schema.environments
+ .find(request.params.environmentId)
+ .update(attrs);
// NOTE, in actual implementation, we need to fetch the user from the auth context.
const callerId = OWNER_ID;
@@ -120,14 +132,47 @@ export default function configureEnvironment(route) {
containerId: environment.id,
createdTs: ts,
lastUpdatedTs: ts,
- type: "bb.msg.environment.delete",
+ type,
status: "DELIVERED",
creatorId: callerId,
workspaceId: WORKSPACE_ID,
payload: {
environmentName: environment.name,
+ changeList,
},
};
postMessageToOwnerAndDBA(schema, callerId, messageTemplate);
+
+ return updatedEnvironment;
+ });
+
+ route.delete("/environment/:environmentId", function (schema, request) {
+ const environment = schema.environments.find(request.params.environmentId);
+
+ if (!environment) {
+ return new Response(
+ 404,
+ {},
+ {
+ errors:
+ "Environment id " + request.params.environmentId + " not found",
+ }
+ );
+ }
+
+ const instanceCount = schema.instances.where({
+ environmentId: request.params.environmentId,
+ }).models.length;
+ if (instanceCount > 0) {
+ return new Response(
+ 400,
+ {},
+ {
+ errors: `Found ${instanceCount} instance(s) in environment ${environment.name}. Environment can only be deleted if no instance resides under it.`,
+ }
+ );
+ }
+
+ environment.destroy();
});
}
diff --git a/frontend/src/plugins/task/template.ts b/frontend/src/plugins/task/template.ts
index e2c602aeb8..15660baf3a 100644
--- a/frontend/src/plugins/task/template.ts
+++ b/frontend/src/plugins/task/template.ts
@@ -1,4 +1,4 @@
-import { TaskField, TaskFieldId, TaskTemplate, UNKNOWN_FIELD } from "../types";
+import { TaskField, FieldId, TaskTemplate, UNKNOWN_FIELD } from "../types";
import { TaskType } from "../../types";
import DefaultTemplate from "./DefaultTemplate";
import DatabaseCreateTemplate from "./DatabaseCreateTemplate";
@@ -22,7 +22,7 @@ export function templateForType(type: TaskType): TaskTemplate | undefined {
export function fieldFromId(
template: TaskTemplate,
- fieldId: TaskFieldId
+ fieldId: FieldId
): TaskField {
if (template.fieldList) {
return (
diff --git a/frontend/src/plugins/types.ts b/frontend/src/plugins/types.ts
index a79dd8e504..8b32d0be05 100644
--- a/frontend/src/plugins/types.ts
+++ b/frontend/src/plugins/types.ts
@@ -10,7 +10,7 @@ import {
// Task
// It has to be string type because the id for stage field contain multiple parts.
-export type TaskFieldId = string;
+export type FieldId = string;
export enum TaskBuiltinFieldId {
NAME = "1",
@@ -59,7 +59,7 @@ export type TaskField = {
// Used as the key to store the data. This must NOT be changed after
// in use, otherwise, it will cause data loss/corruption. Its design
// is very similar to the message field id in Protocol Buffers.
- id: TaskFieldId;
+ id: FieldId;
// Used as the key to generate UI artifacts (e.g. query parameter).
// Though changing it won't have catastrophic consequence like changing
// id, we strongly recommend NOT to change it as well, otherwise, previous
diff --git a/frontend/src/types/bytebase.ts b/frontend/src/types/bytebase.ts
index c175ddc243..ae4127e935 100644
--- a/frontend/src/types/bytebase.ts
+++ b/frontend/src/types/bytebase.ts
@@ -1,6 +1,6 @@
import { ResourceObject } from "./jsonapi";
import { BBNotificationStyle } from "../bbkit/types";
-import { TaskFieldId } from "../plugins";
+import { FieldId } from "../plugins";
export const UNKNOWN_ID = "-1";
@@ -483,7 +483,7 @@ export type ActionType = TaskActionType;
export type ActionTaskFieldUpdatePayload = {
changeList: {
- fieldId: TaskFieldId;
+ fieldId: FieldId;
oldValue?: string;
newValue?: string;
}[];
@@ -519,7 +519,10 @@ export type ActivityPatch = {
// Message
export type EnvironmentMessageType =
| "bb.msg.environment.create"
- | "bb.msg.environment.delete";
+ | "bb.msg.environment.update"
+ | "bb.msg.environment.delete"
+ | "bb.msg.environment.archive"
+ | "bb.msg.environment.restore";
export type InstanceMessageType =
| "bb.msg.instance.create"
@@ -535,8 +538,22 @@ export type MessageType =
| InstanceMessageType
| TaskMessageType;
+export type EnvironmentUpdateMessagePayload = {
+ environmentName: string;
+};
+
+export enum EnvironmentBuiltinFieldId {
+ ROW_STATUS = "1",
+ NAME = "2",
+}
+
export type EnvironmentMessagePayload = {
environmentName: string;
+ changeList: {
+ fieldId: EnvironmentBuiltinFieldId;
+ oldValue?: any;
+ newValue?: any;
+ }[];
};
export type InstanceMessagePaylaod = {
@@ -562,6 +579,7 @@ export type TaskCommentMessagePayload = {
export type MessagePayload =
| EnvironmentMessagePayload
+ | EnvironmentUpdateMessagePayload
| InstanceMessagePaylaod
| TaskAssignMessagePayload
| TaskUpdateStatusMessagePayload
diff --git a/frontend/src/views/Inbox.vue b/frontend/src/views/Inbox.vue
index f56fdb66d8..2ac37a2b90 100644
--- a/frontend/src/views/Inbox.vue
+++ b/frontend/src/views/Inbox.vue
@@ -45,8 +45,15 @@
class="font-medium text-main hover:underline"
>{{ message.creator.name }}</router-link
>
- <template v-if="message.type == 'bb.msg.environment.create'">
- <span class="ml-1"> created environment</span>
+ <span class="ml-1"> {{ actionSentence(message) }}</span>
+ <template
+ v-if="
+ message.type == 'bb.msg.environment.create' ||
+ message.type == 'bb.msg.environment.update' ||
+ message.type == 'bb.msg.environment.archive' ||
+ message.type == 'bb.msg.environment.restore'
+ "
+ >
<router-link
:to="`/environment#${message.containerId}`"
class="normal-link ml-1"
@@ -57,13 +64,11 @@
<template
v-else-if="message.type == 'bb.msg.environment.delete'"
>
- <span class="ml-1"> deleted environment</span>
<span class="font-medium text-main ml-1">
{{ message.payload.environmentName }}
</span>
</template>
<template v-else-if="message.type == 'bb.msg.instance.create'">
- <span class="ml-1"> created instance</span>
<router-link
:to="`/instance/${message.containerId}`"
class="normal-link ml-1"
@@ -72,62 +77,11 @@
</router-link>
</template>
<template v-else-if="message.type == 'bb.msg.instance.delete'">
- <span class="ml-1"> deleted instance</span>
<span class="font-medium text-main ml-1">
{{ message.payload.instanceName }}
</span>
</template>
<template v-else-if="message.type == 'bb.msg.task.assign'">
- <span class="text-sm ml-1">
- <template
- v-if="
- message.payload.oldAssigneeId == '-1' &&
- message.payload.newAssigneeId != '-1'
- "
- >
- assigned
- {{
- currentUser.id == message.payload.newAssigneeId
- ? "you"
- : principalFromId(message.payload.newAssigneeId).name
- }}
- task
- </template>
- <template
- v-else-if="
- message.payload.oldAssigneeId != '-1' &&
- message.payload.newAssigneeId != '-1'
- "
- >
- re-assigned from
- {{
- currentUser.id == message.payload.oldAssigneeId
- ? "you"
- : principalFromId(message.payload.oldAssigneeId).name
- }}
- to
- {{
- currentUser.id == message.payload.newAssigneeId
- ? "you"
- : principalFromId(message.payload.newAssigneeId).name
- }}
- task
- </template>
- <template
- v-else-if="
- message.payload.oldAssigneeId != '-1' &&
- message.payload.newAssigneeId == '-1'
- "
- >
- un-assigned
- {{
- currentUser.id == message.payload.oldAssigneeId
- ? "you"
- : principalFromId(message.payload.oldAssigneeId).name
- }}
- task
- </template>
- </span>
<router-link
:to="`/task/${message.containerId}`"
class="normal-link ml-1"
@@ -138,10 +92,6 @@
<template
v-else-if="message.type == 'bb.msg.task.updatestatus'"
>
- <span class="ml-1">
- changed task status from {{ message.payload.oldStatus }} to
- {{ message.payload.newStatus }}</span
- >
<router-link
:to="`/task/${message.containerId}`"
class="normal-link ml-1"
@@ -150,7 +100,6 @@
</router-link>
</template>
<template v-else-if="message.type == 'bb.msg.task.comment'">
- <span class="ml-1"> commented task</span>
<router-link
:to="`/task/${message.containerId}#activity${message.payload.commentId}`"
class="normal-link ml-1"
@@ -183,11 +132,14 @@ import { computed, onMounted, reactive, watchEffect } from "vue";
import { useStore } from "vuex";
import {
Message,
+ MessagePayload,
Principal,
PrincipalId,
TaskAssignMessagePayload,
TaskUpdateStatusMessagePayload,
+ UNKNOWN_ID,
} from "../types";
+import { assign } from "lodash";
interface LocalState {
showAll: boolean;
@@ -247,6 +199,69 @@ export default {
return store.getters["principal/principalById"](principalId);
};
+ const actionSentence = (message: Message): string => {
+ switch (message.type) {
+ case "bb.msg.environment.create":
+ return "created environment";
+ case "bb.msg.environment.update":
+ return "updated environment";
+ case "bb.msg.environment.delete":
+ return "deleted environment";
+ case "bb.msg.environment.archive":
+ return "archived environment";
+ case "bb.msg.environment.restore":
+ return "restored environment";
+ case "bb.msg.instance.create":
+ return "created instance";
+ case "bb.msg.instance.delete":
+ return "deleted instance";
+ case "bb.msg.task.assign": {
+ const payload = message.payload as TaskAssignMessagePayload;
+ if (
+ payload.oldAssigneeId == UNKNOWN_ID &&
+ payload.newAssigneeId != UNKNOWN_ID
+ ) {
+ return "assigned " + currentUser.value.id == payload.newAssigneeId
+ ? "you"
+ : principalFromId(payload.newAssigneeId).name + " task";
+ } else if (
+ payload.oldAssigneeId != UNKNOWN_ID &&
+ payload.newAssigneeId == UNKNOWN_ID
+ ) {
+ return "un-assigned " + currentUser.value.id ==
+ payload.oldAssigneeId
+ ? "you"
+ : principalFromId(payload.oldAssigneeId).name + " task";
+ } else if (
+ payload.oldAssigneeId != UNKNOWN_ID &&
+ payload.newAssigneeId != UNKNOWN_ID
+ ) {
+ return "re-assigned from " + currentUser.value.id ==
+ payload.oldAssigneeId
+ ? "you"
+ : principalFromId(payload.oldAssigneeId).name +
+ " to " +
+ currentUser.value.id ==
+ payload.newAssigneeId
+ ? "you"
+ : principalFromId(payload.newAssigneeId).name + " task";
+ }
+ return "assigned task";
+ }
+ case "bb.msg.task.updatestatus": {
+ const payload = message.payload as TaskUpdateStatusMessagePayload;
+ return (
+ "changed task status from " +
+ payload.oldStatus +
+ " to " +
+ payload.newStatus
+ );
+ }
+ case "bb.msg.task.comment":
+ return "commented task";
+ }
+ };
+
const clickMessage = (message: Message) => {
if (message.status == "DELIVERED") {
state.whitelist.push(message);
@@ -279,6 +294,7 @@ export default {
currentUser,
principalFromId,
effectiveMessageList,
+ actionSentence,
clickMessage,
showAll,
markAllAsRead,