Skip to content

Commit

Permalink
Merge pull request #16 from akbarsaputrait/develop
Browse files Browse the repository at this point in the history
v24.01.29.00
  • Loading branch information
akbarsaputrait authored Jan 29, 2024
2 parents 23d4e4a + 526d92f commit ee233bc
Show file tree
Hide file tree
Showing 20 changed files with 442 additions and 32 deletions.
31 changes: 16 additions & 15 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

export NVM_DIR="$HOME/.nvm/nvm.sh"
. "$(dirname $NVM_DIR)/nvm.sh"

export NVM_DIR="$HOME/.nvm"
a=$(nvm ls | grep 'node')
b=${a#*(-> }
v=${b%%[)| ]*}

export PATH="$NVM_DIR/versions/node/$v/bin:$PATH"

echo '🏗️👷 Styling, testing and building your project before committing'
if [[ "$OSTYPE" =~ ^msys ]]; then
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
. "$(dirname "$0")/_/husky.sh"
else
export NVM_DIR="$HOME/.nvm/nvm.sh"
. "$(dirname $NVM_DIR)/nvm.sh"

export NVM_DIR="$HOME/.nvm"
a=$(nvm ls | grep 'node')
b=${a#*(-> }
v=${b%%[)| ]*}

export PATH="$NVM_DIR/versions/node/$v/bin:$PATH"
fi

#fix & format
npx lint-staged
Expand All @@ -24,5 +26,4 @@ npm run lint ||
false;
)

echo '🎉🎉🎉🎉 Yeayy, there is no error in your code... I am committing this now. ✨🚀🏄‍♂️🍻'

echo '🎉🎉🎉🎉 Yeayy, there is no error in your code... I am committing this now. ✨🚀🏄‍♂️🍻'
10 changes: 10 additions & 0 deletions src/app.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { OwnerModule } from './app/owner/owner.module';
import { OwnerProfileModule } from './app/owner/profile/profile.module';
import { OwnerLocationModule } from './app/owner/restaurant/location/location.module';
import { OwnerRestaurantModule } from './app/owner/restaurant/restaurant.module';
import { OwnerStaffModule } from './app/owner/restaurant/staff/staff.module';
import { OwnerTableModule } from './app/owner/restaurant/table/table.module';

export const routes: Routes = [
// { path: '/auth', module: AuthModule },
Expand All @@ -22,6 +24,14 @@ export const routes: Routes = [
path: '/:restaurant_id/locations',
module: OwnerLocationModule,
},
{
path: '/:restaurant_id/tables',
module: OwnerTableModule,
},
{
path: '/:restaurant_id/staff',
module: OwnerStaffModule,
},
],
},
],
Expand Down
2 changes: 1 addition & 1 deletion src/app/owner/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class AuthController {
throw new UnauthorizedException('Password is incorrect');
}

if (user.isActive) {
if (!user.isActive) {
throw new UnauthorizedException('Your account was inactive by system');
}

Expand Down
7 changes: 4 additions & 3 deletions src/app/owner/restaurant/location/location.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Location } from '@db/entities/owner/location.entity';
import { LocationTransformer } from '@db/transformers/location.transformer';
import { PlainTransformer } from '@db/transformers/plain.transformer';
import { ValidationException } from '@lib/exceptions/validation.exception';
import { isTrue } from '@lib/helpers/utils.helper';
import { Validator } from '@lib/helpers/validator.helper';
import { Permissions } from '@lib/rbac';
import AppDataSource from '@lib/typeorm/datasource.typeorm';
Expand All @@ -30,7 +31,7 @@ export class LocationController {
@UseGuards(OwnerGuard)
@Permissions(`${PermOwner.Location}@${PermAct.R}`)
async show(@Rest() rest, @Res() response, @Param() param) {
const location = await Location.findOneBy({ restaurant_id: rest.id, id: param.location_id });
const location = await Location.findOneByOrFail({ restaurant_id: rest.id, id: param.location_id });
await response.item(location, LocationTransformer);
}

Expand All @@ -49,7 +50,7 @@ export class LocationController {

const loc = new Location();
loc.name = body.name;
loc.is_default = false;
loc.is_default = isTrue(body.is_default);
loc.restaurant_id = rest.id;
await loc.save();

Expand All @@ -75,7 +76,7 @@ export class LocationController {

const loc = await Location.findOneByOrFail({ id: param.location_id });
loc.name = body.name;
loc.is_default = body.is_default;
loc.is_default = isTrue(body.is_default);
await loc.save();

return response.item(loc, LocationTransformer);
Expand Down
4 changes: 3 additions & 1 deletion src/app/owner/restaurant/restaurant.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Module } from '@nestjs/common';
import { OwnerLocationModule } from './location/location.module';
import { RestaurantController } from './restaurant.controller';
import { OwnerStaffModule } from './staff/staff.module';
import { OwnerTableModule } from './table/table.module';

@Module({
imports: [OwnerLocationModule],
imports: [OwnerLocationModule, OwnerTableModule, OwnerStaffModule],
controllers: [RestaurantController],
providers: [],
})
Expand Down
20 changes: 20 additions & 0 deletions src/app/owner/restaurant/staff/role.controller.ts
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);
}
}
140 changes: 140 additions & 0 deletions src/app/owner/restaurant/staff/staff.controller.ts
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);
}
}
10 changes: 10 additions & 0 deletions src/app/owner/restaurant/staff/staff.module.ts
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 {}
98 changes: 98 additions & 0 deletions src/app/owner/restaurant/table/table.controller.ts
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);
}
}
Loading

0 comments on commit ee233bc

Please sign in to comment.