API apiKey list return accessType and orgName
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m4s
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m4s
This commit is contained in:
parent
95aad0b9fb
commit
ccfb2754fd
1 changed files with 147 additions and 2 deletions
|
|
@ -20,6 +20,12 @@ import { In } from "typeorm";
|
||||||
import { RequestWithUser } from "../middlewares/user";
|
import { RequestWithUser } from "../middlewares/user";
|
||||||
import { ApiName } from "../entities/ApiName";
|
import { ApiName } from "../entities/ApiName";
|
||||||
import { ApiHistory } from "../entities/ApiHistory";
|
import { ApiHistory } from "../entities/ApiHistory";
|
||||||
|
import { OrgRoot } from "../entities/OrgRoot";
|
||||||
|
import { OrgChild1 } from "../entities/OrgChild1";
|
||||||
|
import { OrgChild2 } from "../entities/OrgChild2";
|
||||||
|
import { OrgChild3 } from "../entities/OrgChild3";
|
||||||
|
import { OrgChild4 } from "../entities/OrgChild4";
|
||||||
|
import { OrgRevision } from "../entities/OrgRevision";
|
||||||
|
|
||||||
const jwt = require("jsonwebtoken");
|
const jwt = require("jsonwebtoken");
|
||||||
@Route("api/v1/org/apiKey")
|
@Route("api/v1/org/apiKey")
|
||||||
|
|
@ -33,6 +39,12 @@ export class ApiKeyController extends Controller {
|
||||||
private apiKeyRepository = AppDataSource.getRepository(ApiKey);
|
private apiKeyRepository = AppDataSource.getRepository(ApiKey);
|
||||||
private apiNameRepository = AppDataSource.getRepository(ApiName);
|
private apiNameRepository = AppDataSource.getRepository(ApiName);
|
||||||
private apiHistoryRepository = AppDataSource.getRepository(ApiHistory);
|
private apiHistoryRepository = AppDataSource.getRepository(ApiHistory);
|
||||||
|
private orgRootRepository = AppDataSource.getRepository(OrgRoot);
|
||||||
|
private orgChild1Repository = AppDataSource.getRepository(OrgChild1);
|
||||||
|
private orgChild2Repository = AppDataSource.getRepository(OrgChild2);
|
||||||
|
private orgChild3Repository = AppDataSource.getRepository(OrgChild3);
|
||||||
|
private orgChild4Repository = AppDataSource.getRepository(OrgChild4);
|
||||||
|
private orgRevisionRepository = AppDataSource.getRepository(OrgRevision);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* API ตรวจสอบและถอดรหัส JWT token
|
* API ตรวจสอบและถอดรหัส JWT token
|
||||||
|
|
@ -151,6 +163,9 @@ export class ApiKeyController extends Controller {
|
||||||
relations: ["apiNames", "apiHistorys"],
|
relations: ["apiNames", "apiHistorys"],
|
||||||
order: { createdAt: "DESC", apiNames: { createdAt: "DESC" } },
|
order: { createdAt: "DESC", apiNames: { createdAt: "DESC" } },
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const orgNames = await this.buildOrgNameBatch(apiKey);
|
||||||
|
|
||||||
const data = apiKey.map((_data) => ({
|
const data = apiKey.map((_data) => ({
|
||||||
id: _data.id,
|
id: _data.id,
|
||||||
createdAt: _data.createdAt,
|
createdAt: _data.createdAt,
|
||||||
|
|
@ -163,6 +178,7 @@ export class ApiKeyController extends Controller {
|
||||||
dnaChild2Id: _data.dnaChild2Id,
|
dnaChild2Id: _data.dnaChild2Id,
|
||||||
dnaChild3Id: _data.dnaChild3Id,
|
dnaChild3Id: _data.dnaChild3Id,
|
||||||
dnaChild4Id: _data.dnaChild4Id,
|
dnaChild4Id: _data.dnaChild4Id,
|
||||||
|
orgName: orgNames.get(_data.id),
|
||||||
apiNames: _data.apiNames.map((x) => ({
|
apiNames: _data.apiNames.map((x) => ({
|
||||||
id: x.id,
|
id: x.id,
|
||||||
name: x.name,
|
name: x.name,
|
||||||
|
|
@ -174,10 +190,139 @@ export class ApiKeyController extends Controller {
|
||||||
return new HttpSuccess(data);
|
return new HttpSuccess(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async buildOrgNameBatch(apiKeys: ApiKey[]): Promise<Map<string, string | null>> {
|
||||||
|
const currentRevision = await this.orgRevisionRepository.findOne({
|
||||||
|
where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!currentRevision) {
|
||||||
|
return new Map(apiKeys.map((k) => [k.id, null]));
|
||||||
|
}
|
||||||
|
|
||||||
|
const currentRevisionId = currentRevision.id;
|
||||||
|
|
||||||
|
const rootIds = [...new Set(apiKeys.map((k) => k.dnaRootId).filter(Boolean))];
|
||||||
|
const child1Ids = [...new Set(apiKeys.map((k) => k.dnaChild1Id).filter(Boolean))];
|
||||||
|
const child2Ids = [...new Set(apiKeys.map((k) => k.dnaChild2Id).filter(Boolean))];
|
||||||
|
const child3Ids = [...new Set(apiKeys.map((k) => k.dnaChild3Id).filter(Boolean))];
|
||||||
|
const child4Ids = [...new Set(apiKeys.map((k) => k.dnaChild4Id).filter(Boolean))];
|
||||||
|
|
||||||
|
const [roots, child1s, child2s, child3s, child4s] = await Promise.all([
|
||||||
|
rootIds.length > 0
|
||||||
|
? this.orgRootRepository.find({
|
||||||
|
where: [
|
||||||
|
{ id: In(rootIds), orgRevisionId: currentRevisionId },
|
||||||
|
{ ancestorDNA: In(rootIds), orgRevisionId: currentRevisionId },
|
||||||
|
],
|
||||||
|
select: ["id", "ancestorDNA", "orgRootName"],
|
||||||
|
})
|
||||||
|
: [],
|
||||||
|
child1Ids.length > 0
|
||||||
|
? this.orgChild1Repository.find({
|
||||||
|
where: [
|
||||||
|
{ id: In(child1Ids), orgRevisionId: currentRevisionId },
|
||||||
|
{ ancestorDNA: In(child1Ids), orgRevisionId: currentRevisionId },
|
||||||
|
],
|
||||||
|
select: ["id", "ancestorDNA", "orgChild1Name"],
|
||||||
|
})
|
||||||
|
: [],
|
||||||
|
child2Ids.length > 0
|
||||||
|
? this.orgChild2Repository.find({
|
||||||
|
where: [
|
||||||
|
{ id: In(child2Ids), orgRevisionId: currentRevisionId },
|
||||||
|
{ ancestorDNA: In(child2Ids), orgRevisionId: currentRevisionId },
|
||||||
|
],
|
||||||
|
select: ["id", "ancestorDNA", "orgChild2Name"],
|
||||||
|
})
|
||||||
|
: [],
|
||||||
|
child3Ids.length > 0
|
||||||
|
? this.orgChild3Repository.find({
|
||||||
|
where: [
|
||||||
|
{ id: In(child3Ids), orgRevisionId: currentRevisionId },
|
||||||
|
{ ancestorDNA: In(child3Ids), orgRevisionId: currentRevisionId },
|
||||||
|
],
|
||||||
|
select: ["id", "ancestorDNA", "orgChild3Name"],
|
||||||
|
})
|
||||||
|
: [],
|
||||||
|
child4Ids.length > 0
|
||||||
|
? this.orgChild4Repository.find({
|
||||||
|
where: [
|
||||||
|
{ id: In(child4Ids), orgRevisionId: currentRevisionId },
|
||||||
|
{ ancestorDNA: In(child4Ids), orgRevisionId: currentRevisionId },
|
||||||
|
],
|
||||||
|
select: ["id", "ancestorDNA", "orgChild4Name"],
|
||||||
|
})
|
||||||
|
: [],
|
||||||
|
]);
|
||||||
|
|
||||||
|
const rootMap = new Map(
|
||||||
|
roots.map((r) => [r.id, { name: r.orgRootName, ancestorDNA: r.ancestorDNA }]),
|
||||||
|
);
|
||||||
|
const child1Map = new Map(
|
||||||
|
child1s.map((c) => [c.id, { name: c.orgChild1Name, ancestorDNA: c.ancestorDNA }]),
|
||||||
|
);
|
||||||
|
const child2Map = new Map(
|
||||||
|
child2s.map((c) => [c.id, { name: c.orgChild2Name, ancestorDNA: c.ancestorDNA }]),
|
||||||
|
);
|
||||||
|
const child3Map = new Map(
|
||||||
|
child3s.map((c) => [c.id, { name: c.orgChild3Name, ancestorDNA: c.ancestorDNA }]),
|
||||||
|
);
|
||||||
|
const child4Map = new Map(
|
||||||
|
child4s.map((c) => [c.id, { name: c.orgChild4Name, ancestorDNA: c.ancestorDNA }]),
|
||||||
|
);
|
||||||
|
|
||||||
|
const result = new Map<string, string | null>();
|
||||||
|
for (const apiKey of apiKeys) {
|
||||||
|
if (apiKey.accessType === "ALL") {
|
||||||
|
result.set(apiKey.id, null);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const parts: string[] = [];
|
||||||
|
|
||||||
|
const getOrgName = (
|
||||||
|
dnaId: string,
|
||||||
|
orgMap: Map<string, { name: string; ancestorDNA: string }>,
|
||||||
|
): string | null => {
|
||||||
|
const byId = orgMap.get(dnaId);
|
||||||
|
if (byId) return byId.name;
|
||||||
|
for (const [, value] of orgMap) {
|
||||||
|
if (value.ancestorDNA === dnaId) return value.name;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
|
if (apiKey.dnaChild4Id) {
|
||||||
|
const name = getOrgName(apiKey.dnaChild4Id, child4Map);
|
||||||
|
if (name) parts.push(name);
|
||||||
|
}
|
||||||
|
if (apiKey.dnaChild3Id) {
|
||||||
|
const name = getOrgName(apiKey.dnaChild3Id, child3Map);
|
||||||
|
if (name) parts.push(name);
|
||||||
|
}
|
||||||
|
if (apiKey.dnaChild2Id) {
|
||||||
|
const name = getOrgName(apiKey.dnaChild2Id, child2Map);
|
||||||
|
if (name) parts.push(name);
|
||||||
|
}
|
||||||
|
if (apiKey.dnaChild1Id) {
|
||||||
|
const name = getOrgName(apiKey.dnaChild1Id, child1Map);
|
||||||
|
if (name) parts.push(name);
|
||||||
|
}
|
||||||
|
if (apiKey.dnaRootId) {
|
||||||
|
const name = getOrgName(apiKey.dnaRootId, rootMap);
|
||||||
|
if (name) parts.push(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
result.set(apiKey.id, parts.length > 0 ? parts.join(" ") : null);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* API รายการ Api Key
|
* API รายการ Api Name
|
||||||
*
|
*
|
||||||
* @summary รายการ Api Key (ADMIN)
|
* @summary รายการ Api Name (ADMIN)
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@Get("name")
|
@Get("name")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue