Skip to content

Commit 0d497a6

Browse files
committed
fix: namespace get by key
1 parent 4647e32 commit 0d497a6

7 files changed

Lines changed: 44 additions & 67 deletions

File tree

openapi.json

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"hash": "c16c180c2d62fd4ce0bb4fb78b3036b51724c1344dd1528a52d4624602bae82f",
2+
"hash": "e53a5702f1615db98d73ccef5f59a195fde4908d6a0a9f265ce3800fac5b5d1a",
33
"openapi": "3.0.0",
44
"paths": {
55
"/hello": {
@@ -2086,23 +2086,23 @@
20862086
]
20872087
}
20882088
},
2089-
"/namespaces/{namespaceIdOrKey}": {
2089+
"/namespaces/{key}": {
20902090
"get": {
20912091
"operationId": "getNamespace",
20922092
"parameters": [
20932093
{
2094-
"name": "namespaceIdOrKey",
2094+
"name": "key",
20952095
"required": true,
20962096
"in": "path",
2097-
"description": "Namespace id or key, if key should encodeURIComponent",
2097+
"description": "Namespace key, key should encodeURIComponent",
20982098
"schema": {
20992099
"type": "string"
21002100
}
21012101
}
21022102
],
21032103
"responses": {
21042104
"200": {
2105-
"description": "The namespace with expected id or key.",
2105+
"description": "The namespace with expected key.",
21062106
"content": {
21072107
"application/json": {
21082108
"schema": {
@@ -2117,7 +2117,7 @@
21172117
"ApiKey": []
21182118
}
21192119
],
2120-
"summary": "Find namespace by id or key",
2120+
"summary": "Find namespace by key",
21212121
"tags": [
21222122
"namespace"
21232123
]
@@ -2126,7 +2126,7 @@
21262126
"operationId": "updateNamespace",
21272127
"parameters": [
21282128
{
2129-
"name": "namespaceIdOrKey",
2129+
"name": "key",
21302130
"required": true,
21312131
"in": "path",
21322132
"schema": {
@@ -2165,14 +2165,12 @@
21652165
"tags": [
21662166
"namespace"
21672167
]
2168-
}
2169-
},
2170-
"/namespaces/{namespaceId}": {
2168+
},
21712169
"delete": {
21722170
"operationId": "deleteNamespace",
21732171
"parameters": [
21742172
{
2175-
"name": "namespaceId",
2173+
"name": "key",
21762174
"required": true,
21772175
"in": "path",
21782176
"schema": {

src/auth/auth.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export class AuthService {
6565

6666
// 递归添加命名空间及其父级的权限
6767
const addNamespacePermissions = async (nsKey: string) => {
68-
const namespace = await this.namespaceService.getByKey(nsKey);
68+
const namespace = await this.namespaceService.get(nsKey);
6969
namespace?.permissions?.forEach((permission) => permissions.add(permission));
7070
if (namespace?.ns) {
7171
await addNamespacePermissions(namespace.ns);

src/namespace/namespace.controller.ts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CacheKey, CacheTTL } from '@nestjs/cache-manager';
1+
import { CacheTTL } from '@nestjs/cache-manager';
22
import {
33
Body,
44
ConflictException,
@@ -52,7 +52,7 @@ export class NamespaceController {
5252
const { key, ns } = createDto;
5353

5454
if (key) {
55-
const namespace = await this.namespaceService.getByKey(key);
55+
const namespace = await this.namespaceService.get(key);
5656
if (namespace) {
5757
throw new ConflictException({
5858
code: ErrorCodes.NAMESPACE_ALREADY_EXISTS,
@@ -68,7 +68,7 @@ export class NamespaceController {
6868
}
6969

7070
if (ns) {
71-
const parent = await this.namespaceService.getByKey(ns);
71+
const parent = await this.namespaceService.get(ns);
7272
if (!parent) {
7373
throw new NotFoundException({
7474
code: ErrorCodes.NAMESPACE_NOT_FOUND,
@@ -114,27 +114,27 @@ export class NamespaceController {
114114
}
115115

116116
/**
117-
* Find namespace by id or key
117+
* Find namespace by key
118118
*/
119119
@ApiOperation({ operationId: 'getNamespace' })
120120
@ApiOkResponse({
121-
description: 'The namespace with expected id or key.',
121+
description: 'The namespace with expected key.',
122122
type: Namespace,
123123
})
124124
@ApiParam({
125-
name: 'namespaceIdOrKey',
125+
name: 'key',
126126
type: 'string',
127-
description: 'Namespace id or key, if key should encodeURIComponent',
127+
description: 'Namespace key, key should encodeURIComponent',
128128
})
129129
@UseInterceptors(SetCacheInterceptor)
130130
@CacheTTL(24 * 3600)
131-
@Get(':namespaceIdOrKey')
132-
async get(@Param('namespaceIdOrKey') namespaceIdOrKey: string): Promise<NamespaceDocument> {
133-
const namespace = await this.namespaceService.get(namespaceIdOrKey);
131+
@Get(':key')
132+
async get(@Param('key') key: string): Promise<NamespaceDocument> {
133+
const namespace = await this.namespaceService.get(key);
134134
if (!namespace) {
135135
throw new NotFoundException({
136136
code: ErrorCodes.NAMESPACE_NOT_FOUND,
137-
message: `Namespace ${namespaceIdOrKey} not found.`,
137+
message: `Namespace ${key} not found.`,
138138
});
139139
}
140140
return namespace;
@@ -149,17 +149,16 @@ export class NamespaceController {
149149
type: Namespace,
150150
})
151151
@UseInterceptors(UnsetCacheInterceptor)
152-
@CacheKey('/namespaces/:id,/namespaces/:key')
153-
@Patch(':namespaceIdOrKey')
152+
@Patch(':key')
154153
async update(
155-
@Param('namespaceIdOrKey') namespaceIdOrKey: string,
154+
@Param('key') key: string,
156155
@Body() updateDto: UpdateNamespaceDto
157156
): Promise<NamespaceDocument> {
158-
const namespace = await this.namespaceService.update(namespaceIdOrKey, updateDto);
157+
const namespace = await this.namespaceService.update(key, updateDto);
159158
if (!namespace) {
160159
throw new NotFoundException({
161160
code: ErrorCodes.NAMESPACE_NOT_FOUND,
162-
message: `Namespace ${namespaceIdOrKey} not found.`,
161+
message: `Namespace ${key} not found.`,
163162
});
164163
}
165164
return namespace;
@@ -172,8 +171,8 @@ export class NamespaceController {
172171
@ApiNoContentResponse({ description: 'No content.' })
173172
@HttpCode(HttpStatus.NO_CONTENT)
174173
@UseInterceptors(UnsetCacheInterceptor)
175-
@Delete(':namespaceId')
176-
async delete(@Param('namespaceId') namespaceId: string): Promise<void> {
177-
await this.namespaceService.delete(namespaceId);
174+
@Delete(':key')
175+
async delete(@Param('key') key: string): Promise<void> {
176+
await this.namespaceService.delete(key);
178177
}
179178
}

src/namespace/namespace.service.spec.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,6 @@ describe('NamespaceService', () => {
6666
});
6767

6868
describe('getNamespace', () => {
69-
it('should get a namespace by id', async () => {
70-
const toBeCreated = mockNamespace('n-1');
71-
const namespace = await service.create(toBeCreated);
72-
expect(namespace).toBeDefined();
73-
74-
const foundByNs = await service.get(namespace.id);
75-
expect(foundByNs).toBeDefined();
76-
expect(foundByNs).toMatchObject(toBeCreated);
77-
});
78-
7969
it('should get a namespace by key', async () => {
8070
const namespace1 = await service.create(mockNamespace('nn-1'));
8171
const namespace2 = await service.create(mockNamespace('nn-2'));
@@ -98,7 +88,7 @@ describe('NamespaceService', () => {
9888
const namespace = await service.create(toBeCreated);
9989
expect(namespace).toBeDefined();
10090

101-
await service.delete(namespace.id);
91+
await service.delete(namespace.key);
10292
const found = await service.get(namespace.key);
10393
expect(found).toEqual(null);
10494
});
@@ -128,7 +118,7 @@ describe('NamespaceService', () => {
128118
const toBeCreated = mockNamespace('n-1');
129119
const namespace = await service.create(toBeCreated);
130120
const updateDoc = { name: 'updated name' };
131-
const updated = await service.update(namespace.id, updateDoc);
121+
const updated = await service.update(namespace.key, updateDoc);
132122
expect(updated).toBeDefined();
133123
expect(updated).toMatchObject(updateDoc);
134124
});

src/namespace/namespace.service.ts

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Injectable } from '@nestjs/common';
22
import { InjectModel } from '@nestjs/mongoose';
33
import { DeleteResult } from 'mongodb';
4-
import { isObjectIdOrHexString, Model } from 'mongoose';
4+
import { Model } from 'mongoose';
55

66
import { buildMongooseQuery } from 'src/mongo';
77

@@ -33,30 +33,20 @@ export class NamespaceService {
3333
return this.namespaceModel.find(filter).sort(sort).skip(offset).limit(limit).exec();
3434
}
3535

36-
get(idOrKey: string): Promise<NamespaceDocument> {
37-
if (isObjectIdOrHexString(idOrKey)) {
38-
return this.namespaceModel.findById(idOrKey).exec();
39-
}
40-
return this.getByKey(idOrKey);
36+
update(key: string, updateDto: UpdateNamespaceDto): Promise<NamespaceDocument> {
37+
return this.namespaceModel.findOneAndUpdate({ key }, updateDto, { new: true }).exec();
4138
}
4239

43-
update(idOrKey: string, updateDto: UpdateNamespaceDto): Promise<NamespaceDocument> {
44-
if (isObjectIdOrHexString(idOrKey)) {
45-
return this.namespaceModel.findByIdAndUpdate(idOrKey, updateDto, { new: true }).exec();
46-
}
47-
return this.namespaceModel.findOneAndUpdate({ key: idOrKey }, updateDto, { new: true }).exec();
48-
}
49-
50-
upsertByKey(key: string, dto: UpsertNamespaceDto) {
40+
upsertByKey(key: string, dto: UpsertNamespaceDto): Promise<NamespaceDocument> {
5141
const filter = { key };
5242
return this.namespaceModel.findOneAndUpdate(filter, dto, { upsert: true, new: true }).exec();
5343
}
5444

55-
delete(id: string) {
56-
return this.namespaceModel.findByIdAndDelete(id).exec();
45+
delete(key: string): Promise<NamespaceDocument> {
46+
return this.namespaceModel.findOneAndDelete({ key }).exec();
5747
}
5848

59-
getByKey(key: string): Promise<NamespaceDocument | null> {
49+
get(key: string): Promise<NamespaceDocument | null> {
6050
return this.namespaceModel.findOne({ key }).exec();
6151
}
6252

src/user/user.controller.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export class UserController {
128128

129129
// 查询用户的 ns 是否存在
130130
if (ns) {
131-
const namespace = await this.namespaceService.getByKey(ns);
131+
const namespace = await this.namespaceService.get(ns);
132132
if (!namespace) {
133133
throw new NotFoundException({
134134
code: ErrorCodes.NAMESPACE_NOT_FOUND,
@@ -217,7 +217,7 @@ export class UserController {
217217
): Promise<UserDocument> {
218218
const { username, email, phone, employeeId, ns } = updateDto;
219219
if (ns) {
220-
const namespace = await this.namespaceService.getByKey(ns);
220+
const namespace = await this.namespaceService.get(ns);
221221
if (!namespace) {
222222
throw new NotFoundException({
223223
code: ErrorCodes.NAMESPACE_NOT_FOUND,
@@ -318,7 +318,7 @@ export class UserController {
318318
): Promise<UserDocument> {
319319
const { username, email, phone, ns, employeeId: toBeUpdatedEmployeeId } = dto;
320320
if (ns) {
321-
const namespace = await this.namespaceService.getByKey(ns);
321+
const namespace = await this.namespaceService.get(ns);
322322
if (!namespace) {
323323
throw new NotFoundException({
324324
code: ErrorCodes.NAMESPACE_NOT_FOUND,

test/namespace.e2e-spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ describe('Namespace crud (e2e)', () => {
120120
});
121121

122122
const resp1 = await request(app.getHttpServer())
123-
.get(`/namespaces/${encodeURIComponent(ns1.id)}`)
123+
.get(`/namespaces/${encodeURIComponent(ns1.key)}`)
124124
.set('Content-Type', 'application/json')
125125
.set('x-api-key', auth.apiKey)
126126
.set('Accept', 'application/json')
@@ -148,7 +148,7 @@ describe('Namespace crud (e2e)', () => {
148148
const nameToBeUpdated = 'test name';
149149

150150
const resp = await request(app.getHttpServer())
151-
.patch(`/namespaces/${ns.id}`)
151+
.patch(`/namespaces/${ns.key}`)
152152
.send({
153153
name: nameToBeUpdated,
154154
})
@@ -169,11 +169,11 @@ describe('Namespace crud (e2e)', () => {
169169
});
170170

171171
await request(app.getHttpServer())
172-
.delete(`/namespaces/${ns.id}`)
172+
.delete(`/namespaces/${ns.key}`)
173173
.set('Content-Type', 'application/json')
174174
.set('x-api-key', auth.apiKey)
175175
.set('Accept', 'application/json')
176176
.expect(204);
177-
expect(await namespaceService.get(ns.id)).toBeNull();
177+
expect(await namespaceService.get(ns.key)).toBeNull();
178178
});
179179
});

0 commit comments

Comments
 (0)