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 fde9590
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 20 deletions.
5 changes: 1 addition & 4 deletions notify-bc-lb/src/controllers/administrator.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// file ported
import {authenticate} from '@loopback/authentication';
import {ApplicationConfig, CoreBindings, inject, service} from '@loopback/core';
import {
Expand Down Expand Up @@ -178,7 +179,6 @@ export class AdministratorController extends BaseController {
}

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

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

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

@del('/administrators/{id}', {
responses: {
Expand Down
70 changes: 56 additions & 14 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 @@ -70,7 +71,7 @@ export class AdministratorsController {
})
count(@Req() req, @JsonQuery('where') where?: FilterQuery<Administrator>) {
if (req?.user?.authnStrategy === AuthnStrategy.AccessToken) {
where = { and: [where ?? {}, { id: req.user.securityId }] };
where = { $and: [where ?? {}, { id: req.user.securityId }] };
}
return this.administratorsService.count(where);
}
Expand Down Expand Up @@ -295,24 +296,65 @@ export class AdministratorsController {
return this.administratorsService.update(id, updateAdministratorDto, req);
}

@Post()
@Roles(Role.SuperAdmin)
create(@Body() createAdministratorDto: CreateAdministratorDto, @Req() req) {
return this.administratorsService.create(createAdministratorDto, 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);
}

@Get()
findAll() {
return this.administratorsService.findAll();
@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);
}

@Get(':id')
findOne(@Param('id') id: string) {
return this.administratorsService.findOne(id);
@Post()
@Roles(Role.SuperAdmin)
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;
}

@Delete(':id')
remove(@Param('id') id: string) {
return this.administratorsService.remove(id);
@Get()
@ApiFilterJsonQuery()
@ApiOkResponse({
description: 'Array of Administrator model instances',
type: [Administrator],
})
findAll(
@JsonQuery('filter')
filter: FilterDto<Administrator>,
@Req() req,
) {
if (req.user.authnStrategy === AuthnStrategy.AccessToken) {
filter = filter ?? {};
filter.where = {
$and: [filter.where ?? {}, { id: req.user.securityId }],
};
}
return this.administratorsService.findAll(filter);
}
}
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 fde9590

Please sign in to comment.