-
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 #16 from akbarsaputrait/develop
v24.01.29.00
- Loading branch information
Showing
20 changed files
with
442 additions
and
32 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
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 { Restaurant } from '@db/entities/owner/restaurant.entity'; | ||
import { Table, TableStatus } from '@db/entities/owner/table.entity'; | ||
import { PlainTransformer } from '@db/transformers/plain.transformer'; | ||
import { TableTransformer } from '@db/transformers/table.transformer'; | ||
import { ValidationException } from '@lib/exceptions/validation.exception'; | ||
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 TableController { | ||
@Get() | ||
@UseGuards(OwnerGuard) | ||
@Permissions(`${PermOwner.Table}@${PermAct.R}`) | ||
async index(@Rest() rest, @Res() response, @Quero() quero) { | ||
const tables = AppDataSource.createQueryBuilder(Table, 't1'); | ||
tables.where({ restaurant_id: rest.id }); | ||
|
||
if (quero.location_id) { | ||
tables.andWhere({ location_id: quero.location_id }); | ||
} | ||
|
||
const data = await tables.search().sort().getPaged(); | ||
|
||
await response.paginate(data, PlainTransformer); | ||
} | ||
|
||
@Get('/:table_id') | ||
@UseGuards(OwnerGuard) | ||
@Permissions(`${PermOwner.Table}@${PermAct.R}`) | ||
async show(@Rest() rest, @Res() response, @Param() param) { | ||
const table = await Table.findOneByOrFail({ restaurant_id: rest.id, id: param.table_id }); | ||
await response.item(table, TableTransformer); | ||
} | ||
|
||
@Post() | ||
@UseGuards(OwnerGuard) | ||
@Permissions(`${PermOwner.Table}@${PermAct.C}`) | ||
async create(@Rest() rest: Restaurant, @Body() body, @Res() response) { | ||
console.log(Object.values(TableStatus).join(',')); | ||
const rules = { | ||
number: 'required|unique|safe_text', | ||
location_id: 'required', | ||
status: `required|in:${Object.values(TableStatus).join(',')}`, | ||
}; | ||
const validation = Validator.init(body, rules); | ||
if (validation.fails()) { | ||
throw new ValidationException(validation); | ||
} | ||
|
||
const loc = await Location.findOneByOrFail({ id: body.location_id }); | ||
|
||
const table = new Table(); | ||
table.number = body.number; | ||
table.status = body.status; | ||
table.location_id = loc.id; | ||
table.restaurant_id = rest.id; | ||
await table.save(); | ||
|
||
return response.item(table, TableTransformer); | ||
} | ||
|
||
@Put('/:table_id') | ||
@UseGuards(OwnerGuard) | ||
@Permissions(`${PermOwner.Location}@${PermAct.C}`) | ||
async update(@Rest() rest: Restaurant, @Body() body, @Res() response, @Param() param) { | ||
const rules = { | ||
number: 'required|unique|safe_text', | ||
location_id: 'required', | ||
status: `required|in:${Object.values(TableStatus).join(',')}`, | ||
}; | ||
const validation = Validator.init(body, rules); | ||
if (validation.fails()) { | ||
throw new ValidationException(validation); | ||
} | ||
|
||
if (!param.table_id) { | ||
throw new BadRequestException(); | ||
} | ||
|
||
const table = await Table.findOneByOrFail({ id: param.table_id }); | ||
table.number = body.number; | ||
table.status = body.status; | ||
table.location_id = body.location_id; | ||
table.restaurant_id = rest.id; | ||
await table.save(); | ||
|
||
return response.item(table, TableTransformer); | ||
} | ||
} |
Oops, something went wrong.