fix(orchestration): Throw if not exists using filters (#9275)

This commit is contained in:
Adrien de Peretti
2024-09-24 17:12:04 +02:00
committed by GitHub
parent b4b1a48987
commit 802f204d31
4 changed files with 92 additions and 18 deletions

View File

@@ -357,7 +357,6 @@ describe("RemoteJoiner", () => {
expect(serviceMock.userService).toHaveBeenCalledTimes(1)
expect(serviceMock.userService).toHaveBeenCalledWith({
args: [],
fields: ["id", "name", "email"],
options: { id: ["1"] },
})
@@ -379,7 +378,6 @@ describe("RemoteJoiner", () => {
expect(serviceMock.userService).toHaveBeenCalledTimes(1)
expect(serviceMock.userService).toHaveBeenCalledWith({
args: [],
fields: ["id"],
options: { id: ["1"] },
})
@@ -432,7 +430,6 @@ describe("RemoteJoiner", () => {
expect(serviceMock.userService).toHaveBeenCalledTimes(1)
expect(serviceMock.userService).toHaveBeenCalledWith({
args: [],
fields: ["username", "email"],
options: { id: ["1"] },
})
@@ -464,7 +461,6 @@ describe("RemoteJoiner", () => {
expect(serviceMock.userService).toHaveBeenCalledTimes(1)
expect(serviceMock.userService).toHaveBeenCalledWith({
args: [],
fields: ["username", "email", "products"],
expands: {
products: {

View File

@@ -1189,34 +1189,32 @@ export class RemoteJoiner {
throw new Error(`Service "${queryObj.service}" was not found.`)
}
let pkName = serviceConfig.primaryKeys[0]
const primaryKeyArg = queryObj.args?.find((arg) => {
const inc = serviceConfig.primaryKeys.includes(arg.name)
if (inc) {
pkName = arg.name
}
return inc
const { primaryKeyArg, otherArgs, pkName } = gerPrimaryKeysAndOtherFilters({
serviceConfig,
queryObj,
})
const otherArgs = queryObj.args?.filter(
(arg) => !serviceConfig.primaryKeys.includes(arg.name)
)
const implodeMapping: InternalImplodeMapping[] = []
const parsedExpands = this.parseExpands({
const parseExpandsConfig: Parameters<typeof this.parseExpands>[0] = {
initialService: {
property: "",
parent: "",
serviceConfig,
entity: serviceConfig.entity,
fields: queryObj.fields,
args: otherArgs,
},
query: queryObj,
serviceConfig,
expands: queryObj.expands!,
implodeMapping,
options,
})
}
if (otherArgs) {
parseExpandsConfig.initialService.args = otherArgs
}
const parsedExpands = this.parseExpands(parseExpandsConfig)
const root = parsedExpands.get(BASE_PATH)!
@@ -1239,3 +1237,49 @@ export class RemoteJoiner {
return response.data
}
}
function gerPrimaryKeysAndOtherFilters({ serviceConfig, queryObj }): {
primaryKeyArg: { name: string; value: any } | undefined
otherArgs: { name: string; value: any }[] | undefined
pkName: string
} {
let pkName = serviceConfig.primaryKeys[0]
let primaryKeyArg = queryObj.args?.find((arg) => {
const include = serviceConfig.primaryKeys.includes(arg.name)
if (include) {
pkName = arg.name
}
return include
})
let otherArgs = queryObj.args?.filter(
(arg) => !serviceConfig.primaryKeys.includes(arg.name)
)
const filters =
queryObj.args?.find((arg) => arg.name === "filters")?.value ?? {}
if (!primaryKeyArg) {
const primaryKeyFilter = Object.keys(filters).find((key) => {
return serviceConfig.primaryKeys.includes(key)
})
if (primaryKeyFilter) {
pkName = primaryKeyFilter
primaryKeyArg = {
name: primaryKeyFilter,
value: filters[primaryKeyFilter],
}
delete filters[primaryKeyFilter]
}
}
otherArgs = otherArgs?.length ? otherArgs : undefined
return {
primaryKeyArg,
otherArgs,
pkName,
}
}