Skip to content

Commit dd7086c

Browse files
committed
fix merge confliicts
2 parents b4481df + 13a5a15 commit dd7086c

File tree

4 files changed

+113
-35
lines changed

4 files changed

+113
-35
lines changed

src/modules/common/models/collection.model.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,3 +1158,12 @@ export class CollectionDto {
11581158
@IsOptional()
11591159
activeSync?: boolean;
11601160
}
1161+
1162+
export interface RequestBodyDto {
1163+
raw?: string;
1164+
urlencoded?: { key: string; value: string; checked: boolean }[];
1165+
formdata?: {
1166+
text?: { key: string; value: string; checked: boolean }[];
1167+
file?: any[];
1168+
};
1169+
}

src/modules/workspace/controllers/collection.controller.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,7 +1604,7 @@ export class collectionController {
16041604
/**
16051605
* Endpoint to Generate the Variables of whole Collection using AI.
16061606
*/
1607-
@Post("generate-variables/:collectionId")
1607+
@Post(":collectionId/generate-variables")
16081608
@ApiOperation({
16091609
summary: "Generate Variables",
16101610
description:
@@ -1640,7 +1640,7 @@ export class collectionController {
16401640
* @param KeyValuePairs[] The collectionId.
16411641
* @returns The response object with status and data.
16421642
*/
1643-
@Post("generate-variables/insert")
1643+
@Post(":collectionId/insert-variables")
16441644
@ApiOperation({
16451645
summary: "Insert Generated Variables into Collection",
16461646
description:
@@ -1656,21 +1656,23 @@ export class collectionController {
16561656
description: "Failed to add a Generate Variables.",
16571657
})
16581658
async addGeneratedVariables(
1659+
@Param("collectionId") collectionId: string,
16591660
@Body() content: CollectionGeneratedVariableDto,
16601661
@Res() res: FastifyReply,
16611662
@Req() request: ExtendedFastifyRequest,
16621663
) {
16631664
const user = request.user;
1664-
const response = await this.collectionService.insertGeneratedVariables(
1665-
content.collectionId,
1665+
await this.collectionService.insertGeneratedVariables(
1666+
collectionId,
16661667
content.generatedeVariables,
16671668
content.workspaceId,
16681669
user,
16691670
);
1671+
const collection = await this.collectionService.getCollection(collectionId);
16701672
const responseData = new ApiResponseService(
16711673
"Generated Variables Inserted Successfully",
16721674
HttpStatusCode.OK,
1673-
response,
1675+
collection,
16741676
);
16751677
return res.status(responseData.httpStatusCode).send(responseData);
16761678
}

src/modules/workspace/schedulers/ai-consumption.scheduler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export class AiConsumptionScheduler {
3131
return { start, end };
3232
}
3333

34-
@Cron(CronExpression.EVERY_5_MINUTES)
34+
@Cron(CronExpression.EVERY_WEEK)
3535
async handleConsumption() {
3636
// This method will handle the AI consumption logic.
3737
// It can be used to schedule tasks related to AI consumption.

src/modules/workspace/services/collection.service.ts

Lines changed: 96 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import {
5050
import { DecodedUserObject } from "@src/types/fastify";
5151
import { EncryptionService } from "@src/modules/common/services/encryption.service";
5252
import { VariableDto } from "@src/modules/common/models/environment.model";
53+
import { RequestBodyDto } from "@src/modules/common/models/collection.model";
5354

5455
@Injectable()
5556
export class CollectionService {
@@ -1142,35 +1143,88 @@ export class CollectionService {
11421143
generatedVariables: VariableDto[],
11431144
requestItem: any,
11441145
): any {
1145-
// Recursive function to deeply replace matches
1146-
const replaceValues = (obj: any, path: string = ""): any => {
1147-
if (Array.isArray(obj)) {
1148-
return obj.map((item, index) =>
1149-
replaceValues(item, `${path}[${index}]`),
1150-
);
1151-
} else if (obj && typeof obj === "object") {
1152-
const newObj: any = {};
1153-
for (const [key, value] of Object.entries(obj)) {
1154-
newObj[key] = replaceValues(value, `${path}.${key}`);
1155-
}
1156-
return newObj;
1157-
} else if (typeof obj === "string") {
1158-
for (const variable of generatedVariables) {
1159-
if (obj === variable.value) {
1160-
return `{{${variable.key}}}`;
1146+
// Helper: replace only outside {{ }} blocks
1147+
const replaceOutsideBraces = (text: string): string => {
1148+
return text.replace(
1149+
/(\{\{.*?\}\})|([^{}]+)/g,
1150+
(match, insideBraces, outside) => {
1151+
if (insideBraces) return insideBraces;
1152+
let updated = outside;
1153+
for (const variable of generatedVariables) {
1154+
if (updated === variable.value) {
1155+
updated = `{{${variable.key}}}`;
1156+
} else if (updated.includes(variable.value)) {
1157+
updated = updated.replace(
1158+
new RegExp(variable.value, "g"),
1159+
`{{${variable.key}}}`,
1160+
);
1161+
}
1162+
}
1163+
return updated;
1164+
},
1165+
);
1166+
};
1167+
1168+
// Special updater for array of key-value objects
1169+
const updateKeyValueArray = (arr: any[]) => {
1170+
return arr.map((entry) => ({
1171+
...entry,
1172+
key:
1173+
typeof entry.key === "string"
1174+
? replaceOutsideBraces(entry.key)
1175+
: entry.key,
1176+
value:
1177+
typeof entry.value === "string"
1178+
? replaceOutsideBraces(entry.value)
1179+
: entry.value,
1180+
}));
1181+
};
1182+
1183+
// Main recursive update
1184+
const replaceValues = (obj: any): any => {
1185+
if (!obj || typeof obj !== "object") {
1186+
return typeof obj === "string" ? replaceOutsideBraces(obj) : obj;
1187+
}
1188+
const newObj: any = Array.isArray(obj) ? [] : {};
1189+
for (const [key, value] of Object.entries(obj)) {
1190+
if (key === "url" && typeof value === "string") {
1191+
newObj[key] = replaceOutsideBraces(value);
1192+
} else if (key === "headers" && Array.isArray(value)) {
1193+
newObj[key] = updateKeyValueArray(value);
1194+
} else if (key === "queryParams" && Array.isArray(value)) {
1195+
newObj[key] = updateKeyValueArray(value);
1196+
} else if (
1197+
key === "body" &&
1198+
typeof value === "object" &&
1199+
value !== null
1200+
) {
1201+
const updatedBody = { ...(value as RequestBodyDto) };
1202+
if (typeof updatedBody.raw === "string") {
1203+
updatedBody.raw = replaceOutsideBraces(updatedBody.raw);
11611204
}
1162-
if (obj.includes(variable.value)) {
1163-
obj = obj.replace(
1164-
new RegExp(variable.value, "g"),
1165-
`{{${variable.key}}}`,
1205+
if (Array.isArray(updatedBody.urlencoded)) {
1206+
updatedBody.urlencoded = updateKeyValueArray(
1207+
updatedBody.urlencoded,
11661208
);
11671209
}
1210+
if (
1211+
updatedBody.formdata &&
1212+
typeof updatedBody.formdata === "object"
1213+
) {
1214+
if (Array.isArray(updatedBody.formdata.text)) {
1215+
updatedBody.formdata.text = updateKeyValueArray(
1216+
updatedBody.formdata.text,
1217+
);
1218+
}
1219+
}
1220+
newObj[key] = updatedBody;
1221+
} else {
1222+
newObj[key] = replaceValues(value);
11681223
}
1169-
return obj;
11701224
}
1171-
return obj;
1225+
return newObj;
11721226
};
1173-
return replaceValues(requestItem, "root");
1227+
return replaceValues(requestItem);
11741228
}
11751229

11761230
public async insertGeneratedVariables(
@@ -1190,17 +1244,30 @@ export class CollectionService {
11901244
}
11911245
const traverseAndUpdate = (items: any[]) => {
11921246
for (const item of items) {
1193-
if (
1194-
item.type === ItemTypeEnum.REQUEST ||
1195-
item.type === ItemTypeEnum.GRAPHQL ||
1196-
item.type === ItemTypeEnum.SOCKETIO ||
1197-
item.type === ItemTypeEnum.WEBSOCKET
1198-
) {
1247+
if (item.type === ItemTypeEnum.REQUEST) {
11991248
item.request = this.updatedRequestInCollection(
12001249
generatedPairs,
12011250
item.request,
12021251
);
12031252
}
1253+
if (item.type === ItemTypeEnum.SOCKETIO) {
1254+
item.socketio = this.updatedRequestInCollection(
1255+
generatedPairs,
1256+
item.socketio,
1257+
);
1258+
}
1259+
if (item.type === ItemTypeEnum.WEBSOCKET) {
1260+
item.websocket = this.updatedRequestInCollection(
1261+
generatedPairs,
1262+
item.websocket,
1263+
);
1264+
}
1265+
if (item.type === ItemTypeEnum.GRAPHQL) {
1266+
item.graphql = this.updatedRequestInCollection(
1267+
generatedPairs,
1268+
item.graphql,
1269+
);
1270+
}
12041271
// If folder or item has nested items
12051272
if (Array.isArray(item.items) && item.items.length > 0) {
12061273
traverseAndUpdate(item.items);

0 commit comments

Comments
 (0)