-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from akbarsaputrait/6-api-owner---manage-resta…
…urant-staff Owner - Restaurant manage staff
- Loading branch information
Showing
9 changed files
with
266 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { OwnerAuthGuard } from '@core/guards/auth.guard'; | ||
import { OwnerGuard } from '@core/guards/owner.guard'; | ||
import { PermAct, PermOwner } from '@core/services/role.service'; | ||
import { StaffRole } from '@db/entities/staff/role.entity'; | ||
import { RawTransformer } from '@db/transformers/raw.transformer'; | ||
import { Permissions } from '@lib/rbac'; | ||
import AppDataSource from '@lib/typeorm/datasource.typeorm'; | ||
import { Controller, Get, Res, UseGuards } from '@nestjs/common'; | ||
|
||
@Controller('roles') | ||
@UseGuards(OwnerAuthGuard()) | ||
export class RoleController { | ||
@Get() | ||
@UseGuards(OwnerGuard) | ||
@Permissions(`${PermOwner.Role}@${PermAct.R}`) | ||
async index(@Res() response) { | ||
const roles = await AppDataSource.createQueryBuilder(StaffRole, 't1').search().sort().getPaged(); | ||
await response.paginate(roles, RawTransformer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import { Quero } from '@core/decorators/quero.decorator'; | ||
import { Rest } from '@core/decorators/restaurant.decorator'; | ||
import { OwnerAuthGuard } from '@core/guards/auth.guard'; | ||
import { OwnerGuard } from '@core/guards/owner.guard'; | ||
import { PermAct, PermOwner } from '@core/services/role.service'; | ||
import { Location } from '@db/entities/owner/location.entity'; | ||
import { StaffRole } from '@db/entities/staff/role.entity'; | ||
import { StaffUser, StaffUserStatus } from '@db/entities/staff/user.entity'; | ||
import { StaffTransformer } from '@db/transformers/staff.transformer'; | ||
import { ValidationException } from '@lib/exceptions/validation.exception'; | ||
import { hash } from '@lib/helpers/encrypt.helper'; | ||
import { randomChar } from '@lib/helpers/utils.helper'; | ||
import { Validator } from '@lib/helpers/validator.helper'; | ||
import { Permissions } from '@lib/rbac'; | ||
import AppDataSource from '@lib/typeorm/datasource.typeorm'; | ||
import { BadRequestException, Body, Controller, Get, Param, Post, Put, Res, UseGuards } from '@nestjs/common'; | ||
|
||
@Controller() | ||
@UseGuards(OwnerAuthGuard()) | ||
export class StaffController { | ||
@Get() | ||
@UseGuards(OwnerGuard) | ||
@Permissions(`${PermOwner.Staff}@${PermAct.R}`) | ||
async index(@Rest() rest, @Res() response, @Quero() quero) { | ||
const query = AppDataSource.createQueryBuilder(StaffUser, 't1').search().sort(); | ||
query.where({ restaurant_id: rest.id }); | ||
|
||
if (quero.location_id) { | ||
query.andWhere({ location_id: quero.location_id }); | ||
} | ||
|
||
const staffs = await query.getPaged(); | ||
await response.paginate(staffs, StaffTransformer); | ||
} | ||
|
||
@Get('/:id') | ||
@UseGuards(OwnerGuard) | ||
@Permissions(`${PermOwner.Staff}@${PermAct.R}`) | ||
async show(@Rest() rest, @Res() response, @Param() param) { | ||
const staff = await StaffUser.findOrFailByRestaurant(param.id, rest); | ||
await response.item(staff, StaffTransformer); | ||
} | ||
|
||
@Post() | ||
@Permissions(`${PermOwner.Staff}@${PermAct.C}`) | ||
async store(@Body() body, @Res() response, @Res() rest) { | ||
const rules = { | ||
name: 'required|safe_text', | ||
email: 'required|email', | ||
phone: 'required|phone', | ||
role_id: 'required|uid', | ||
location_id: 'uid', | ||
}; | ||
const validation = Validator.init(body, rules); | ||
if (validation.fails()) { | ||
throw new ValidationException(validation); | ||
} | ||
|
||
if (!rest) { | ||
throw new BadRequestException('You must be assigned to a restaurant'); | ||
} | ||
|
||
const emailExists = await StaffUser.exists({ where: { email: body.email } }); | ||
if (emailExists) { | ||
throw new BadRequestException('Email has already been used'); | ||
} | ||
|
||
const phoneExists = await StaffUser.exists({ where: { phone: body.phone } }); | ||
if (phoneExists) { | ||
throw new BadRequestException('Phone has already been used'); | ||
} | ||
|
||
const role = await StaffRole.findOneByOrFail({ id: body.role_id }); | ||
|
||
let location: Location = null; | ||
if (body.location_id) { | ||
location = await Location.findOneByOrFail({ id: body.location_id }); | ||
} | ||
|
||
const plainPass = randomChar(8); | ||
const staff = new StaffUser(); | ||
staff.name = body.name; | ||
staff.email = String(body.email).toLowerCase(); | ||
staff.phone = body.phone; | ||
staff.password = await hash(plainPass); | ||
staff.status = StaffUserStatus.Active; | ||
staff.role_slug = role.slug; | ||
staff.location_id = location ? location.id : null; | ||
staff.restaurant_id = rest.id; | ||
await staff.save(); | ||
|
||
// this.mail | ||
// .sendMail({ | ||
// to: staff.email, | ||
// subject: 'Your staff account!', | ||
// template: 'staff-register', | ||
// context: { | ||
// name: staff.name, | ||
// password: plainPass, | ||
// }, | ||
// }) | ||
// .then(() => null) | ||
// .catch((error) => Logger.getInstance().notify(error)); | ||
|
||
await response.item(staff, StaffTransformer); | ||
} | ||
|
||
@Put('/:id') | ||
@Permissions(`${PermOwner.Staff}@${PermAct.U}`) | ||
async update(@Param() param, @Body() body, @Res() response) { | ||
const rules = { | ||
name: 'required|safe_text', | ||
phone: 'required|phone', | ||
status: `required|in:${Object.values(StaffUserStatus).join(',')}`, | ||
role_id: 'required|uid', | ||
location_id: 'uid', | ||
}; | ||
const validation = Validator.init(body, rules); | ||
if (validation.fails()) { | ||
throw new ValidationException(validation); | ||
} | ||
|
||
const role = await StaffRole.findOneByOrFail({ id: body.role_id }); | ||
let location: Location = null; | ||
if (body.location_id) { | ||
location = await Location.findOneByOrFail({ id: body.location_id }); | ||
} | ||
|
||
const staff = await StaffUser.findOneByOrFail({ id: param.id }); | ||
|
||
staff.name = body.name; | ||
staff.phone = body.phone; | ||
staff.status = body.status; | ||
staff.role_slug = role.slug; | ||
staff.location_id = location ? location.id : null; | ||
await staff.save(); | ||
|
||
await response.item(staff, StaffTransformer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { RoleController } from './role.controller'; | ||
import { StaffController } from './staff.controller'; | ||
|
||
@Module({ | ||
imports: [], | ||
controllers: [StaffController, RoleController], | ||
providers: [], | ||
}) | ||
export class OwnerStaffModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { Media } from '@db/entities/core/media.entity'; | ||
import { StaffUser } from '@db/entities/staff/user.entity'; | ||
import { encrypt } from '@lib/helpers/encrypt.helper'; | ||
import { RequestHelper } from '@lib/helpers/request.helper'; | ||
import { TransformerAbstract } from '@lib/transformer/abstract.transformer'; | ||
import { LocationTransformer } from './location.transformer'; | ||
|
||
export class StaffTransformer extends TransformerAbstract { | ||
get availableInclude() { | ||
return ['location', 'role']; | ||
} | ||
|
||
get defaultInclude() { | ||
return []; | ||
} | ||
|
||
transform(entity: StaffUser) { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const { media, ...rest } = entity.toJSON(); | ||
|
||
return { | ||
...rest, | ||
avatar: Media.getImage(entity.media), | ||
}; | ||
} | ||
|
||
async transformWithPermission(entity: StaffUser) { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const { media, ...rest } = entity.toJSON(); | ||
|
||
const grants = RequestHelper.getPermissionGrants(); | ||
const role = await entity.role; | ||
const location = await entity.location; | ||
return { | ||
...rest, | ||
role: { | ||
id: role.id, | ||
name: role.slug, | ||
permissions: encrypt(grants[role.slug]), | ||
}, | ||
location: location | ||
? { | ||
id: location.id, | ||
name: location.name, | ||
} | ||
: null, | ||
avatar: Media.getImage(entity.media), | ||
}; | ||
} | ||
|
||
async includeLocation(entity: StaffUser) { | ||
const location = await entity.location; | ||
if (!location) { | ||
return this.null(); | ||
} | ||
|
||
return this.item(location, LocationTransformer); | ||
} | ||
|
||
async includeRole(entity: StaffUser) { | ||
const role = await entity.role; | ||
if (!role) { | ||
return this.null(); | ||
} | ||
|
||
return { id: role.id, name: role.slug }; | ||
} | ||
} |