Skip to content

Commit

Permalink
finished admin api
Browse files Browse the repository at this point in the history
  • Loading branch information
f-w committed Oct 1, 2023
1 parent b8b674e commit 7cf107d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 17 deletions.
8 changes: 5 additions & 3 deletions notify-bc-lb/src/controllers/administrator.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ export class AdministratorController extends BaseController {
super(appConfig, configurationRepository);
}

// start: ported
@authenticate('ipWhitelist', 'clientCertificate')
@post('/administrators', {
responses: {
Expand Down Expand Up @@ -176,9 +177,10 @@ export class AdministratorController extends BaseController {
});
return savedUser;
}
// end: ported

@authenticate('anonymous')
// start: ported
@authenticate('anonymous')
@post('/administrators/login', {
responses: {
'200': {
Expand Down Expand Up @@ -282,6 +284,7 @@ export class AdministratorController extends BaseController {
return this.administratorRepository.find(filter, undefined);
}

// start: ported
@get('/administrators/{id}', {
responses: {
'200': {
Expand Down Expand Up @@ -311,7 +314,6 @@ export class AdministratorController extends BaseController {
);
}

// start: ported
@patch('/administrators/{id}', {
responses: {
'204': {
Expand Down Expand Up @@ -368,7 +370,6 @@ export class AdministratorController extends BaseController {
undefined,
);
}
// end: ported

@del('/administrators/{id}', {
responses: {
Expand All @@ -394,4 +395,5 @@ export class AdministratorController extends BaseController {
await this.userCredentialRepository.deleteAll({userId: id}, undefined);
await this.administratorRepository.deleteById(id, undefined);
}
// end: ported
}
51 changes: 39 additions & 12 deletions src/api/administrators/administrators.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
ApiTags,
} from '@nestjs/swagger';
import { genSalt, hash } from 'bcryptjs';
import { omit } from 'lodash';
import { FilterQuery } from 'mongoose';
import { AuthnStrategy, Role } from 'src/auth/constants';
import { UserProfile } from 'src/auth/dto/user-profile.dto';
Expand Down Expand Up @@ -295,24 +296,50 @@ export class AdministratorsController {
return this.administratorsService.update(id, updateAdministratorDto, req);
}

@Get(':id')
findOne(@Param('id') id: string, @Req() req): Promise<Administrator> {
if (
req.user.authnStrategy === AuthnStrategy.AccessToken &&
req.user.securityId !== id
) {
throw new HttpException(undefined, HttpStatus.FORBIDDEN);
}
return this.administratorsService.findOne(id);
}

@Delete(':id')
async remove(@Param('id') id: string, @Req() req) {
if (
req.user.authnStrategy === AuthnStrategy.AccessToken &&
req.user.securityId !== id
) {
throw new HttpException(undefined, HttpStatus.FORBIDDEN);
}
await this.accessTokenService.removeAll({ userId: id });
await this.userCredentialService.removeAll({ userId: id });
this.administratorsService.remove(id);
}

@Post()
@Roles(Role.SuperAdmin)
create(@Body() createAdministratorDto: CreateAdministratorDto, @Req() req) {
return this.administratorsService.create(createAdministratorDto, req);
async signUp(
@Body() createAdministratorDto: CreateAdministratorDto,
@Req() req,
): Promise<Administrator> {
const savedUser = (
await this.administratorsService.create(
omit(createAdministratorDto, 'password'),
req,
)
).toJSON();
await this.createCredential(savedUser.id, req, {
password: createAdministratorDto.password,
});
return savedUser;
}

@Get()
findAll() {
return this.administratorsService.findAll();
}

@Get(':id')
findOne(@Param('id') id: string) {
return this.administratorsService.findOne(id);
}

@Delete(':id')
remove(@Param('id') id: string) {
return this.administratorsService.remove(id);
}
}
11 changes: 9 additions & 2 deletions src/api/common/base.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export class BaseService<T> {
return this.findOneAndReplace(updateDto, { _id }, req, upsert);
}

findOneAndReplace(
async findOneAndReplace(
updateDto,
filter: FilterQuery<T> | null,
req: (Request & { user?: any }) | null,
Expand All @@ -132,12 +132,19 @@ export class BaseService<T> {
updateDto.updatedBy = req.user;
updateDto.updated = new Date();
}
return this.model
const res = await this.model
.findOneAndUpdate(filter, updateDto, {
upsert,
new: true,
includeResultMetadata: true,
})
.exec();
if (upsert && !res.lastErrorObject.updatedExisting) {
await this.model.findByIdAndUpdate(res.value._id, {
createdBy: req.user,
});
}
return res.value;
}

remove(id: string) {
Expand Down

0 comments on commit 7cf107d

Please sign in to comment.