Skip to content

Commit

Permalink
feat: added places module with all features; fixed some serverless pr…
Browse files Browse the repository at this point in the history
…oblems and permissions in serverless.yml; fixed some logic in users/ module
  • Loading branch information
miltonhit committed Dec 20, 2022
1 parent 8be85c2 commit 9744037
Show file tree
Hide file tree
Showing 33 changed files with 667 additions and 27 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@ Run those commands in the terminal:
4. curl http://localhost:8000/service/status

## To deploy (aws provider)
1. Install serverless framework ([Click here] (https://www.serverless.com/framework/docs/getting-started))
1. Install [serverless framework](https://github.com/nestjs/typescript-starter)
2. run this command: serverless deploy

## Postman Documentation
[Version 0.0.1](https://documenter.getpostman.com/view/1334597/2s8YzZNe9C)
2 changes: 1 addition & 1 deletion config/env/local.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
DB_RUN_LOCAL=true
DB_RUN_LOCAL=false
DB_TABLE_SUFFIX=dev
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"prebuild": "rimraf dist",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "STAGE=local nest start --watch",
"start:debug": "STAGE=local nest start --debug --watch",
"start:prod": "STAGE=prd node dist/main",
"start": "stage=local nest start --watch",
"start:debug": "stage=local nest start --debug --watch",
"start:prod": "stage=prd node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
Expand All @@ -23,7 +23,6 @@
"deploy:prd": "nest build && serverless deploy --aws-profile hubia-deploy --stage prd"
},
"dependencies": {
"@aws/dynamodb-data-mapper": "^0.7.3",
"@aws/dynamodb-data-mapper-annotations": "^0.7.3",
"@nestjs/common": "^9.0.0",
"@nestjs/config": "^2.2.0",
Expand Down
50 changes: 49 additions & 1 deletion serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@ provider:
region: us-east-1
apiName: ${self:service}-${self:custom.stage}

iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
Resource:
- "arn:aws:dynamodb:${self:provider.region}:*:table/hub-users-${self:custom.stage}"
- "arn:aws:dynamodb:${self:provider.region}:*:table/hub-places-${self:custom.stage}"
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:Scan
Resource:
- "arn:aws:dynamodb:${self:provider.region}:*:table/hub-users-${self:custom.stage}/index/*"
- "arn:aws:dynamodb:${self:provider.region}:*:table/hub-places-${self:custom.stage}/index/*"

stackTags:
service: ${self:service}
stage: ${self:custom.stage}
Expand Down Expand Up @@ -86,4 +106,32 @@ resources:
Projection:
NonKeyAttributes:
- id
ProjectionType: INCLUDE
ProjectionType: INCLUDE

PlacesTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: hub-places-${self:custom.stage}
BillingMode: PAY_PER_REQUEST
AttributeDefinitions:
- AttributeName: id
AttributeType: S
- AttributeName: ownerId
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH
GlobalSecondaryIndexes:
- IndexName: GsiOwner
KeySchema:
- AttributeName: ownerId
KeyType: HASH
Projection:
NonKeyAttributes:
- id
ProjectionType: INCLUDE

S3:
Type: AWS::S3::Bucket
Properties:
BucketName: ${self:service}-${self:custom.stage}
6 changes: 4 additions & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AuthModule } from './auth/auth.module';
import { UsersModule } from './users/users.module';
import { PlacesModule } from './places/places.module';

const STAGE = process.env.STAGE;

@Module({
imports: [
ConfigModule.forRoot({ envFilePath: `${process.cwd()}/config/env/${process.env.STAGE}.env` }),
ConfigModule.forRoot({ envFilePath: `${process.cwd()}/config/env/${process.env.stage}.env` }),
UsersModule,
AuthModule
AuthModule,
PlacesModule
],
controllers: [AppController],
providers: [AppService]
Expand Down
2 changes: 1 addition & 1 deletion src/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class AppService {
return {
startedAt: this.startedAt,
name: "hub_delivery_api",
env: process.env.STAGE,
env: process.env.stage,
debugDb: process.env.DB_RUN_LOCAL,
suffixDb: process.env.DB_TABLE_SUFFIX
}
Expand Down
7 changes: 6 additions & 1 deletion src/commons/exceptions/error-def.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,10 @@ export enum ErrorsType {

export const ErrorsConst = {
alreadyExists: new ErrorDef({code: 1, description: "alreadyExists", type: ErrorsType.LOGIC}),
invalidInput: new ErrorDef({code: 2, description: "invalidInput", type: ErrorsType.LOGIC})
invalidInput: new ErrorDef({code: 2, description: "invalidInput", type: ErrorsType.LOGIC}),
notAllowed: new ErrorDef({code: 3, description: "notAllowed", type: ErrorsType.LOGIC}),
resourceNotFound: new ErrorDef({code: 4, description: "resourceNotFound", type: ErrorsType.LOGIC}),



}
9 changes: 8 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@ import { Server } from 'http';
import { ExpressAdapter } from '@nestjs/platform-express';
import * as awsServerlessExpress from 'aws-serverless-express';
import * as express from 'express';
import { ValidationPipe } from '@nestjs/common';
import { HttpServiceExceptionFilter } from './commons/filters/http-service-exception.filter';

let cachedServer: Server;

const bootstrapServer = async (): Promise<Server> => {
const expressApp = express();
const adapter = new ExpressAdapter(expressApp);
const app = await NestFactory.create(AppModule, adapter);
const app = await NestFactory.create(AppModule, adapter, {
logger: console
});

app.useGlobalPipes(new ValidationPipe());
app.useGlobalFilters(new HttpServiceExceptionFilter());
app.enableCors();
await app.init();
return awsServerlessExpress.createServer(expressApp);
Expand Down
22 changes: 22 additions & 0 deletions src/places/dto/address.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { IsEmail, IsNotEmpty, Length, IsNotEmptyObject, IsObject } from "class-validator";

export class AddressDto {
@IsNotEmpty()
zipcode: number;
@IsNotEmpty()
street: string;
@IsNotEmpty()
number: string;
complement: string;
@IsNotEmpty()
city: string;
@IsNotEmpty()
@Length(2, 2)
state: string;
@IsNotEmpty()
district: string;
@IsNotEmpty()
lat: number;
@IsNotEmpty()
lng: number;
}
19 changes: 19 additions & 0 deletions src/places/dto/create-place.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Address } from "../entities/address.entity";
import { Types } from "../entities/place.entity";
import { IsEmail, IsNotEmpty, Length, IsNotEmptyObject, IsObject, ValidateNested } from "class-validator";
import { Type } from 'class-transformer';
import { AddressDto } from "./address.dto";

export class CreatePlaceDto {
@IsNotEmpty()
title: string;
@IsNotEmpty()
whatsappContact: string;
imageUrl: string;

@IsNotEmptyObject()
@IsObject()
@ValidateNested()
@Type(() => AddressDto)
address: AddressDto;
}
15 changes: 15 additions & 0 deletions src/places/dto/menu-section-item.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { IsEmail, IsNotEmpty, Length, IsNotEmptyObject, IsObject } from "class-validator";
import { MultipleChoiceOptionsDto } from "./multiple-choice.dto";
import { OneChoiceOptionDto } from "./one-choice-option.dto";

export class MenuSectionItemDto {
id: string;
placeId: string;
sectionId: string;
title: string;
description: string;
value: number;
imageUrl: string;
oneChoiceOptions: OneChoiceOptionDto[];
multipleChoiceOptions: MultipleChoiceOptionsDto[];
}
7 changes: 7 additions & 0 deletions src/places/dto/menu-section.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { IsEmail, IsNotEmpty, Length, IsNotEmptyObject, IsObject } from "class-validator";

export class MenuSectionDto {
id: string;
placeId: string;
title: string;
}
9 changes: 9 additions & 0 deletions src/places/dto/multiple-choice.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { OptionDto } from "./option.dto";


export class MultipleChoiceOptionsDto {
title: string;
options: OptionDto[];
min: number;
max: number;
}
7 changes: 7 additions & 0 deletions src/places/dto/one-choice-option.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { IsEmail, IsNotEmpty, Length, IsNotEmptyObject, IsObject } from "class-validator";
import { OptionDto } from "./option.dto";

export class OneChoiceOptionDto {
title: string;
options: OptionDto[];
}
7 changes: 7 additions & 0 deletions src/places/dto/option.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { IsEmail, IsNotEmpty, Length, IsNotEmptyObject, IsObject } from "class-validator";

export class OptionDto {
title: string;
description: string;
value: number;
}
4 changes: 4 additions & 0 deletions src/places/dto/update-place.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { PartialType } from '@nestjs/mapped-types';
import { CreatePlaceDto } from './create-place.dto';

export class UpdatePlaceDto extends PartialType(CreatePlaceDto) {}
21 changes: 21 additions & 0 deletions src/places/entities/address.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export class Address {
public constructor(init?:Partial<Address>) {
Object.assign(this, init);
}

id: string;
zipcode: number;
street: string;
number: string;
complement: string;
city: string;
state: string;
district: string;
//
// GoogleMaps
completeByGeo: string;
lat: number;
lng: number;
createdAt?: Date;
updatedAt?: Date;
}
18 changes: 18 additions & 0 deletions src/places/entities/menu-section-item.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { IsEmail, IsNotEmpty, Length, IsNotEmptyObject, IsObject } from "class-validator";
import { MultipleChoiceOptions } from "./multiple-choice";
import { OneChoiceOption } from "./one-choice-option";

export class MenuSectionItem {
public constructor(init?:Partial<MenuSectionItem>) {
Object.assign(this, init);
}

id: string;
sectionId: string;
title: string;
description: string;
value: number;
imageUrl: string;
oneChoiceOptions: OneChoiceOption[];
multipleChoiceOptions: MultipleChoiceOptions[];
}
13 changes: 13 additions & 0 deletions src/places/entities/menu-section.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { IsEmail, IsNotEmpty, Length, IsNotEmptyObject, IsObject } from "class-validator";
import { MenuSectionItem } from "./menu-section-item";

export class MenuSection {
public constructor(init?:Partial<MenuSection>) {
Object.assign(this, init);
}

id: string;
placeId: string;
title: string;
itens: MenuSectionItem[];
}
9 changes: 9 additions & 0 deletions src/places/entities/multiple-choice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Option } from "./option";


export class MultipleChoiceOptions {
title: string;
options: Option[];
min: number;
max: number;
}
7 changes: 7 additions & 0 deletions src/places/entities/one-choice-option.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { IsEmail, IsNotEmpty, Length, IsNotEmptyObject, IsObject } from "class-validator";
import { Option } from "./option";

export class OneChoiceOption {
title: string;
options: Option[];
}
7 changes: 7 additions & 0 deletions src/places/entities/option.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { IsEmail, IsNotEmpty, Length, IsNotEmptyObject, IsObject } from "class-validator";

export class Option {
title: string;
description: string;
value: number;
}
Loading

0 comments on commit 9744037

Please sign in to comment.