Skip to content

Commit

Permalink
Patch undefined req.secrets
Browse files Browse the repository at this point in the history
  • Loading branch information
dangtony98 committed Feb 17, 2023
1 parent dab677b commit dbcd2b0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
36 changes: 23 additions & 13 deletions backend/src/controllers/v2/secretsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,6 @@ export const batchSecrets = async (req: Request, res: Response) => {
requests: BatchSecretRequest[];
}= req.body;

// construct object containing all secrets
const listedSecretsObj: {
[key: string]: {
version: number;
type: string;
}
} = req.secrets.reduce((obj: any, secret: ISecret) => ({
...obj,
[secret._id.toString()]: secret
}), {});

const createSecrets: BatchSecret[] = [];
const updateSecrets: BatchSecret[] = [];
const deleteSecrets: Types.ObjectId[] = [];
Expand Down Expand Up @@ -123,7 +112,20 @@ export const batchSecrets = async (req: Request, res: Response) => {

// handle update secrets
let updatedSecrets: ISecret[] = [];
if (updateSecrets.length > 0) {
if (updateSecrets.length > 0 && req.secrets) {
// construct object containing all secrets
let listedSecretsObj: {
[key: string]: {
version: number;
type: string;
}
} = {};

listedSecretsObj = req.secrets.reduce((obj: any, secret: ISecret) => ({
...obj,
[secret._id.toString()]: secret
}), {});

const updateOperations = updateSecrets.map((u) => ({
updateOne: {
filter: { _id: new Types.ObjectId(u._id) },
Expand Down Expand Up @@ -168,6 +170,14 @@ export const batchSecrets = async (req: Request, res: Response) => {
}
});

const updateAction = await EELogService.createAction({
name: ACTION_UPDATE_SECRETS,
userId: req.user._id,
workspaceId: new Types.ObjectId(workspaceId),
secretIds: updatedSecrets.map((u) => u._id)
}) as IAction;
actions.push(updateAction);

if (postHogClient) {
postHogClient.capture({
event: 'secrets modified',
Expand Down Expand Up @@ -218,7 +228,7 @@ export const batchSecrets = async (req: Request, res: Response) => {
}
}

if (actions.length > 1) {
if (actions.length > 0) {
// (EE) create (audit) log
await EELogService.createLog({
userId: req.user._id.toString(),
Expand Down
1 change: 0 additions & 1 deletion backend/src/routes/v2/secrets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
SECRET_PERSONAL,
SECRET_SHARED
} from '../../variables';

import {
BatchSecretRequest
} from '../../types/secret';
Expand Down

0 comments on commit dbcd2b0

Please sign in to comment.