Skip to content

Commit

Permalink
Merge branch 'staging'
Browse files Browse the repository at this point in the history
  • Loading branch information
fagundesjg committed May 16, 2024
2 parents 38f817e + 0321963 commit 3de7c29
Show file tree
Hide file tree
Showing 14 changed files with 273 additions and 1 deletion.
34 changes: 34 additions & 0 deletions .github/workflows/dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Deploy Backend in Development Enviroment

on:
push:
branches: [develop]

jobs:
build:
runs-on: dev

steps:
- uses: actions/checkout@v4

- name: Create .env file
run: |
touch .env
echo TZ=${{ secrets.TZ }} >> .env
echo DB_HOST=${{ secrets.DB_HOST }} >> .env
echo DB_PORT=${{ secrets.DB_PORT }} >> .env
echo DB_USER=${{ secrets.DB_USER }} >> .env
echo DB_PASSWORD=${{ secrets.DB_PASSWORD }} >> .env
echo DB_DATABASE_NAME=${{ secrets.DEV_DB_DATABASE_NAME }} >> .env
echo DATABASE_URL=postgresql://${{ secrets.DB_USER }}:${{ secrets.DB_PASSWORD }}@${{ secrets.DB_HOST }}:${{ secrets.DB_PORT }}/${{ secrets.DEV_DB_DATABASE_NAME }}?schema=public >> .env
echo SECRET_KEY=${{ secrets.SECRET_KEY }} >> .env
echo HOST=${{ secrets.HOST }} >> .env
echo PORT=${{ secrets.PORT }} >> .env
echo SERVER_USER_PASSWORD=${{ secrets.SERVER_USER_PASSWORD }} >> .env
cat .env
- name: Remove old docker image
run: echo ${{ secrets.SERVER_USER_PASSWORD }} | sudo -S docker compose down --rmi all

- name: Create new docker image
run: echo ${{ secrets.SERVER_USER_PASSWORD }} | sudo -S docker compose up -d --force-recreate
34 changes: 34 additions & 0 deletions .github/workflows/staging.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Deploy Backend in Development Enviroment

on:
push:
branches: [staging]

jobs:
build:
runs-on: stg

steps:
- uses: actions/checkout@v4

- name: Create .env file
run: |
touch .env
echo TZ=${{ secrets.TZ }} >> .env
echo DB_HOST=${{ secrets.DB_HOST }} >> .env
echo DB_PORT=${{ secrets.DB_PORT }} >> .env
echo DB_USER=${{ secrets.DB_USER }} >> .env
echo DB_PASSWORD=${{ secrets.DB_PASSWORD }} >> .env
echo DB_DATABASE_NAME=${{ secrets.STG_DB_DATABASE_NAME }} >> .env
echo DATABASE_URL=postgresql://${{ secrets.DB_USER }}:${{ secrets.DB_PASSWORD }}@${{ secrets.DB_HOST }}:${{ secrets.DB_PORT }}/${{ secrets.STG_DB_DATABASE_NAME }}?schema=public >> .env
echo SECRET_KEY=${{ secrets.SECRET_KEY }} >> .env
echo HOST=${{ secrets.HOST }} >> .env
echo PORT=${{ secrets.PORT }} >> .env
echo SERVER_USER_PASSWORD=${{ secrets.SERVER_USER_PASSWORD }} >> .env
cat .env
- name: Remove old docker image
run: echo ${{ secrets.SERVER_USER_PASSWORD }} | sudo -S docker compose down --rmi all

- name: Create new docker image
run: echo ${{ secrets.SERVER_USER_PASSWORD }} | sudo -S docker compose up -d --force-recreate
14 changes: 14 additions & 0 deletions prisma/migrations/20240516212629_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- CreateTable
CREATE TABLE "supporters" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"image_url" TEXT NOT NULL,
"link" TEXT NOT NULL,
"created_at" VARCHAR(32) NOT NULL,
"updated_at" VARCHAR(32),

CONSTRAINT "supporters_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "supporters_name_key" ON "supporters"("name");
11 changes: 11 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,14 @@ model Partners {
@@map("partners")
}

model Supporters {
id String @id @default(uuid())
name String @unique
imageUrl String @map("image_url")
link String
createdAt String @map("created_at") @db.VarChar(32)
updatedAt String? @map("updated_at") @db.VarChar(32)
@@map("supporters")
}
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { SupplyCategoriesModule } from './supply-categories/supply-categories.mo
import { ShelterManagersModule } from './shelter-managers/shelter-managers.module';
import { ShelterSupplyModule } from './shelter-supply/shelter-supply.module';
import { PartnersModule } from './partners/partners.module';
import { SupportersModule } from './supporters/supporters.module';

@Module({
imports: [
Expand All @@ -24,6 +25,7 @@ import { PartnersModule } from './partners/partners.module';
ShelterManagersModule,
ShelterSupplyModule,
PartnersModule,
SupportersModule,
],
controllers: [],
providers: [
Expand Down
23 changes: 22 additions & 1 deletion src/partners/partners.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { Controller, Get, HttpException, Logger } from '@nestjs/common';
import {
Body,
Controller,
Get,
HttpException,
Logger,
Post,
UseGuards,
} from '@nestjs/common';
import { PartnersService } from './partners.service';
import { ServerResponse } from '../utils';
import { AdminGuard } from '@/guards/admin.guard';

@Controller('partners')
export class PartnersController {
Expand All @@ -18,4 +27,16 @@ export class PartnersController {
throw new HttpException(err?.code ?? err?.name ?? `${err}`, 400);
}
}

@Post('')
@UseGuards(AdminGuard)
async store(@Body() body) {
try {
await this.partnersService.store(body);
return new ServerResponse(200, 'Successfully created partner');
} catch (err: any) {
this.logger.error(`Failed to create partner: ${err}`);
throw new HttpException(err?.code ?? err?.name ?? `${err}`, 400);
}
}
}
9 changes: 9 additions & 0 deletions src/partners/partners.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Injectable } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';

import { PrismaService } from '../prisma/prisma.service';
import { z } from 'zod';
import { CreatePartnerSchema } from './types';

@ApiTags('Parceiros')
@Injectable()
Expand All @@ -11,4 +13,11 @@ export class PartnersService {
async index() {
return await this.prismaService.partners.findMany({});
}

async store(body: z.infer<typeof CreatePartnerSchema>) {
const payload = CreatePartnerSchema.parse(body);
await this.prismaService.partners.create({
data: { ...payload, createdAt: new Date().toISOString() },
});
}
}
17 changes: 17 additions & 0 deletions src/partners/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import z from 'zod';

const PartnerSchema = z.object({
id: z.string(),
name: z.string(),
link: z.string(),
createdAt: z.string(),
updatedAt: z.string().nullable().optional(),
});

const CreatePartnerSchema = PartnerSchema.omit({
id: true,
createdAt: true,
updatedAt: true,
});

export { PartnerSchema, CreatePartnerSchema };
18 changes: 18 additions & 0 deletions src/supporters/supporters.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { SupportersController } from './supporters.controller';

describe('SupportersController', () => {
let controller: SupportersController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [SupportersController],
}).compile();

controller = module.get<SupportersController>(SupportersController);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
43 changes: 43 additions & 0 deletions src/supporters/supporters.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {
Body,
Controller,
Get,
HttpException,
Logger,
Post,
UseGuards,
} from '@nestjs/common';

import { SupportersService } from './supporters.service';
import { ServerResponse } from '../utils';
import { AdminGuard } from '@/guards/admin.guard';

@Controller('supporters')
export class SupportersController {
private logger = new Logger(SupportersController.name);

constructor(private readonly supportersService: SupportersService) {}

@Get('')
async index() {
try {
const data = await this.supportersService.index();
return new ServerResponse(200, 'Successfully get supporters', data);
} catch (err: any) {
this.logger.error(`Failed to get supporters: ${err}`);
throw new HttpException(err?.code ?? err?.name ?? `${err}`, 400);
}
}

@Post('')
@UseGuards(AdminGuard)
async store(@Body() body) {
try {
await this.supportersService.store(body);
return new ServerResponse(200, 'Successfully created supporter');
} catch (err: any) {
this.logger.error(`Failed to create supporter: ${err}`);
throw new HttpException(err?.code ?? err?.name ?? `${err}`, 400);
}
}
}
12 changes: 12 additions & 0 deletions src/supporters/supporters.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Module } from '@nestjs/common';

import { SupportersService } from './supporters.service';
import { SupportersController } from './supporters.controller';
import { PrismaModule } from '../prisma/prisma.module';

@Module({
imports: [PrismaModule],
providers: [SupportersService],
controllers: [SupportersController],
})
export class SupportersModule {}
18 changes: 18 additions & 0 deletions src/supporters/supporters.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { SupportersService } from './supporters.service';

describe('SupportersService', () => {
let service: SupportersService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [SupportersService],
}).compile();

service = module.get<SupportersService>(SupportersService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
21 changes: 21 additions & 0 deletions src/supporters/supporters.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { z } from 'zod';
import { Injectable } from '@nestjs/common';

import { PrismaService } from '../prisma/prisma.service';
import { CreateSupporterSchema } from './types';

@Injectable()
export class SupportersService {
constructor(private readonly prismaService: PrismaService) {}

async index() {
return await this.prismaService.supporters.findMany({});
}

async store(body: z.infer<typeof CreateSupporterSchema>) {
const payload = CreateSupporterSchema.parse(body);
await this.prismaService.supporters.create({
data: { ...payload, createdAt: new Date().toISOString() },
});
}
}
18 changes: 18 additions & 0 deletions src/supporters/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import z from 'zod';

const SupporterSchema = z.object({
id: z.string(),
name: z.string(),
imageUrl: z.string(),
link: z.string(),
createdAt: z.string(),
updatedAt: z.string().nullable().optional(),
});

const CreateSupporterSchema = SupporterSchema.omit({
id: true,
createdAt: true,
updatedAt: true,
});

export { SupporterSchema, CreateSupporterSchema };

0 comments on commit 3de7c29

Please sign in to comment.