Files
cnpmcore/app/port/controller/ScopeController.ts
MK (fengmk2) e5162f20aa fix: improve TypeScript type definitions across codebase (#844)
This commit enhances type safety and fixes type-related issues
throughout the project including:
- Updated type definitions in entities, repositories, and models
- Improved type annotations in services and controllers
- Fixed type issues in adapters and utilities
- Enhanced test file type definitions
- Added typings/index.d.ts for global type declarations

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-10-23 00:58:59 +08:00

76 lines
2.0 KiB
TypeScript

import {
type EggContext,
Context,
HTTPBody,
HTTPController,
HTTPMethod,
HTTPMethodEnum,
HTTPParam,
Inject,
Middleware,
} from '@eggjs/tegg';
import { E400 } from 'egg-errors';
import type { Static } from '@eggjs/typebox-validate/typebox';
import { AbstractController } from './AbstractController.ts';
import { AdminAccess } from '../middleware/AdminAccess.ts';
import type { ScopeManagerService } from '../../core/service/ScopeManagerService.ts';
import type { RegistryManagerService } from '../../core/service/RegistryManagerService.ts';
import { ScopeCreateOptions } from '../typebox.ts';
@HTTPController()
export class ScopeController extends AbstractController {
@Inject()
private readonly scopeManagerService: ScopeManagerService;
@Inject()
private readonly registryManagerService: RegistryManagerService;
@HTTPMethod({
path: '/-/scope',
method: HTTPMethodEnum.POST,
})
@Middleware(AdminAccess)
async createScope(
@Context() ctx: EggContext,
@HTTPBody() scopeOptions: Static<typeof ScopeCreateOptions>
) {
const authorizedUser = await this.userRoleManager.requiredAuthorizedUser(
ctx,
'setting'
);
ctx.tValidate(ScopeCreateOptions, scopeOptions);
const { name, registryId } = scopeOptions;
const registry =
await this.registryManagerService.findByRegistryId(registryId);
if (!registry) {
throw new E400(`registry ${registryId} not found`);
}
await this.scopeManagerService.createScope({
name,
registryId,
operatorId: authorizedUser.userId,
});
return { ok: true };
}
@HTTPMethod({
path: '/-/scope/:id',
method: HTTPMethodEnum.DELETE,
})
@Middleware(AdminAccess)
async removeScope(@Context() ctx: EggContext, @HTTPParam() id: string) {
const authorizedUser = await this.userRoleManager.requiredAuthorizedUser(
ctx,
'setting'
);
await this.scopeManagerService.remove({
scopeId: id,
operatorId: authorizedUser.userId,
});
return { ok: true };
}
}