Patch undefined req.secrets

This commit is contained in:
Tuan Dang
2023-02-17 11:56:06 +07:00
parent dab677b360
commit dbcd2b0988
2 changed files with 23 additions and 14 deletions

View File

@ -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[] = [];
@ -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) },
@ -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',
@ -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(),

View File

@ -15,7 +15,6 @@ import {
SECRET_PERSONAL,
SECRET_SHARED
} from '../../variables';
import {
BatchSecretRequest
} from '../../types/secret';