-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: gh actions, category supploes and fix shelter service methods
- Loading branch information
1 parent
067fa6b
commit d95d816
Showing
19 changed files
with
301 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
TZ=America/Sao_Paulo | ||
|
||
DB_HOST= | ||
DB_PORT= | ||
DB_USER= | ||
DB_PASSWORD= | ||
DB_DATABASE_NAME= | ||
DATABASE_URL="postgresql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_DATABASE_NAME}?schema=public&sslmode=require" | ||
SECRET_KEY= | ||
|
||
HOST=::0.0.0.0 | ||
PORT=4000 |
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,36 @@ | ||
name: Deploy Backend | ||
|
||
on: | ||
push: | ||
branches: [main] | ||
pull_request: | ||
branches: [main] | ||
|
||
jobs: | ||
build: | ||
runs-on: self-hosted | ||
|
||
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.DB_DATABASE_NAME }} >> .env | ||
echo DATABASE_URL=postgresql://${{ secrets.DB_USER }}:${{ secrets.DB_PASSWORD }}@${{ secrets.DB_HOST }}:${{ secrets.DB_PORT }}/${{ secrets.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 |
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,8 @@ | ||
FROM node:18.18 as node | ||
|
||
WORKDIR /usr/app | ||
|
||
COPY package.json package-lock.json ./ | ||
|
||
RUN npm install | ||
COPY . . |
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,14 @@ | ||
services: | ||
sos-rs-api: | ||
container_name: sos-rs-api | ||
build: | ||
context: . | ||
dockerfile: Dockerfile | ||
restart: always | ||
tty: true | ||
ports: | ||
- '4000:4000' | ||
command: > | ||
sh -c "npx prisma generate && | ||
npx prisma migrate deploy && | ||
npm run build && npm run start:prod" |
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,12 @@ | ||
/* | ||
Warnings: | ||
- A unique constraint covering the columns `[name]` on the table `shelters` will be added. If there are existing duplicate values, this will fail. | ||
- Added the required column `name` to the `shelters` table without a default value. This is not possible if the table is not empty. | ||
*/ | ||
-- AlterTable | ||
ALTER TABLE "shelters" ADD COLUMN "name" TEXT NOT NULL; | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "shelters_name_key" ON "shelters"("name"); |
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
18 changes: 18 additions & 0 deletions
18
src/supply-categories/supply-categories.controller.spec.ts
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,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { SupplyCategoriesController } from './supply-categories.controller'; | ||
|
||
describe('SupplyCategoriesController', () => { | ||
let controller: SupplyCategoriesController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [SupplyCategoriesController], | ||
}).compile(); | ||
|
||
controller = module.get<SupplyCategoriesController>(SupplyCategoriesController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
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,73 @@ | ||
import { | ||
Body, | ||
Controller, | ||
Get, | ||
HttpException, | ||
Logger, | ||
Param, | ||
Post, | ||
Put, | ||
UseGuards, | ||
} from '@nestjs/common'; | ||
import { ApiTags } from '@nestjs/swagger'; | ||
|
||
import { SupplyCategoriesService } from './supply-categories.service'; | ||
import { ServerResponse } from '../utils'; | ||
import { StaffGuard } from '@/guards/staff.guard'; | ||
|
||
@ApiTags('Categoria de Suprimentos') | ||
@Controller('supply-categories') | ||
export class SupplyCategoriesController { | ||
private logger = new Logger(SupplyCategoriesController.name); | ||
|
||
constructor( | ||
private readonly supplyCategoryServices: SupplyCategoriesService, | ||
) {} | ||
|
||
@Get('') | ||
async index() { | ||
try { | ||
const data = await this.supplyCategoryServices.index(); | ||
return new ServerResponse( | ||
200, | ||
'Successfully get supply categories', | ||
data, | ||
); | ||
} catch (err: any) { | ||
this.logger.error(`Failed to get supply categories: ${err}`); | ||
throw new HttpException(err?.code ?? err?.name ?? `${err}`, 400); | ||
} | ||
} | ||
|
||
@Post('') | ||
@UseGuards(StaffGuard) | ||
async store(@Body() body) { | ||
try { | ||
const data = await this.supplyCategoryServices.store(body); | ||
return new ServerResponse( | ||
200, | ||
'Successfully created supply category', | ||
data, | ||
); | ||
} catch (err: any) { | ||
this.logger.error(`Failed to create supply category: ${err}`); | ||
throw new HttpException(err?.code ?? err?.name ?? `${err}`, 400); | ||
} | ||
} | ||
|
||
@Put(':id') | ||
@UseGuards(StaffGuard) | ||
async update(@Param('id') id: string, @Body() body) { | ||
try { | ||
const data = await this.supplyCategoryServices.update(id, body); | ||
return new ServerResponse( | ||
200, | ||
'Successfully updated supply category', | ||
data, | ||
); | ||
} catch (err: any) { | ||
this.logger.error(`Failed to update supply category: ${err}`); | ||
throw new HttpException(err?.code ?? err?.name ?? `${err}`, 400); | ||
} | ||
} | ||
} |
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,12 @@ | ||
import { Module } from '@nestjs/common'; | ||
|
||
import { SupplyCategoriesService } from './supply-categories.service'; | ||
import { SupplyCategoriesController } from './supply-categories.controller'; | ||
import { PrismaModule } from '../prisma/prisma.module'; | ||
|
||
@Module({ | ||
imports: [PrismaModule], | ||
providers: [SupplyCategoriesService], | ||
controllers: [SupplyCategoriesController], | ||
}) | ||
export class SupplyCategoriesModule {} |
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,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { SupplyCategoriesService } from './supply-categories.service'; | ||
|
||
describe('SupplyCategoriesService', () => { | ||
let service: SupplyCategoriesService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [SupplyCategoriesService], | ||
}).compile(); | ||
|
||
service = module.get<SupplyCategoriesService>(SupplyCategoriesService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
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,40 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
import { PrismaService } from '../prisma/prisma.service'; | ||
import { z } from 'zod'; | ||
import { | ||
CreateSupplyCategorySchema, | ||
UpdateSupplyCategorySchema, | ||
} from './types'; | ||
|
||
@Injectable() | ||
export class SupplyCategoriesService { | ||
constructor(private readonly prismaService: PrismaService) {} | ||
|
||
async store(body: z.infer<typeof CreateSupplyCategorySchema>) { | ||
const payload = CreateSupplyCategorySchema.parse(body); | ||
await this.prismaService.supplyCategory.create({ | ||
data: { | ||
...payload, | ||
createdAt: new Date().toISOString(), | ||
}, | ||
}); | ||
} | ||
|
||
async update(id: string, body: z.infer<typeof UpdateSupplyCategorySchema>) { | ||
const payload = UpdateSupplyCategorySchema.parse(body); | ||
await this.prismaService.supplyCategory.update({ | ||
where: { | ||
id, | ||
}, | ||
data: { | ||
...payload, | ||
updatedAt: new Date().toISOString(), | ||
}, | ||
}); | ||
} | ||
|
||
async index() { | ||
return await this.prismaService.supplyCategory.findMany({}); | ||
} | ||
} |
Oops, something went wrong.