Skip to content

Commit

Permalink
update: rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
yassinedorbozgithub committed Sep 20, 2024
2 parents 1a67c9d + 21452db commit a414246
Show file tree
Hide file tree
Showing 41 changed files with 498 additions and 193 deletions.
4 changes: 2 additions & 2 deletions .env.production.example
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ SHLP_POSTGRES_DB=
SHLP_POSTGRES_USERNAME=
SHLP_POSTGRES_PASSWORD=
POSTGRES_PRISMA_URL=postgresql://${SHLP_POSTGRES_USERNAME}:${SHLP_POSTGRES_PASSWORD}@${SHLP_POSTGRES_HOST}:5432/${SHLP_POSTGRES_DB}
PRISMA_FIELD_ENCRYPTION_KEY=
PRISMA_FIELD_ENCRYPTION_HASH_SALT=
PRISMA_FIELD_ENCRYPTION_KEY=k1.aesgcm256.Wmy6koJaJpjB0H25qBccLfQ84AvpmCjexanqCHJ5Hr0=
PRISMA_FIELD_ENCRYPTION_HASH_SALT=1a4671c31ff22834a56d4f3741aeac8cc8662a11c73802083ea5a118e5ee600e
99 changes: 0 additions & 99 deletions .github/workflows/run-tests.yml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
name: Build and Push Smart Health Links Portal Image
name: Test - Build - Push

on:
workflow_run:
workflows: ['Tests with Coverage Check']
types: [completed]
push:
branches:
- 'main'
pull_request:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: '20'
cache: 'yarn'

- name: Install Dependencies
run: yarn install --frozen-lockfile

- name: Run Jest Tests
run: yarn test --collectCoverage=false

build-and-push:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
steps:
- name: Checkout
uses: actions/checkout@v4
Expand All @@ -21,21 +38,21 @@ jobs:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build and push tag
- name: Test Docker build
if: ${{ github.ref_name != 'main' }}
uses: docker/build-push-action@v5
with:
platforms: linux/amd64,linux/arm64
push: true
platforms: linux/amd64
push: false # dont push image - Used to confirm build success on branches
file: docker/production/Dockerfile
tags: jembi/smart-health-links-portal:${{ github.ref_name }}

- name: Login to DockerHub
if: ${{ github.ref_name == 'main' }}
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build and push latest
if: ${{ github.ref_name == 'main' }}
uses: docker/build-push-action@v5
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ next-env.d.ts
# db
dev.db
dev.db-journal

#logs
/logs
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ volumes:

networks:
smart-health-links-portal-network:
driver: bridge
driver: bridge
17 changes: 12 additions & 5 deletions docker/production/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ FROM node:20-alpine AS base
# 1. Install dependencies only when needed
FROM base AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
RUN apk add --update --no-cache libc6-compat python3 make g++ && rm -rf /var/cache/apk/*

WORKDIR /app

Expand All @@ -20,7 +20,9 @@ COPY --from=deps /app/node_modules ./node_modules
COPY . .
# This will do the trick, use the corresponding env file for each environment.
COPY .env.production.example .env.production
RUN npm run build

RUN yarn build && \
yarn next-swagger-doc-cli next-swagger-doc.json

# 3. Production image, copy all the files and run next
FROM base AS runner
Expand All @@ -37,13 +39,18 @@ COPY --from=builder /app/public ./public
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
COPY --from=builder /app/prisma ./prisma

# Ensure logs directory is created under the correct permissions
RUN mkdir logs && \
chmod 755 logs && \
chown nextjs:nodejs logs

USER nextjs

EXPOSE 3000

ENV PORT 3000
ENV HOSTNAME "0.0.0.0"
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"

CMD ["node", "server.js"]
CMD ["sh", "-c", "npx --yes prisma migrate deploy --schema=./prisma/schema.prisma && node server.js"]
15 changes: 15 additions & 0 deletions logger.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"transporters": [
{
"transporterType": "file",
"options": {
"filename": "logs/logs-%DATE%.log",
"datePattern": "YYYY-MM-DD",
"zippedArchive": true,
"maxSize": "20m",
"maxFiles": "14d"
}
}
],
"format": ["timestamp", "json"]
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"qrcode": "^1.5.4",
"react": "^18",
"react-dom": "^18",
"winston-daily-rotate-file": "^5.0.0",
"swagger-ui-react": "^5.17.14",
"uuid": "^10.0.0",
"zod": "^3.23.8"
Expand All @@ -45,6 +46,7 @@
"@types/node": "^22.0.3",
"@types/react": "^18",
"@types/react-dom": "^18",
"@types/winston": "^2.4.4",
"@types/uuid": "^10.0.0",
"@typescript-eslint/eslint-plugin": "^8.4.0",
"eslint": "^8.57.0",
Expand Down
17 changes: 16 additions & 1 deletion src/app/api/auth/authOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,22 @@ export const authOptions: AuthOptions = {
],
callbacks: {
session: async ({ session, token }) => {
return { ...session, user: token.user };
return { ...session, ...token };
},
jwt: (params) => {
const tokenParam = params['token'];
if (tokenParam?.['token']) {
const { user = {}, token = {}, account = {} } = tokenParam;

return {
user,
token,
account: {
access_token: account['access_token'],
},
};
}
return params;
},
},
};
9 changes: 7 additions & 2 deletions src/app/api/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { NextResponse, NextRequest } from 'next/server';
import { NextResponse, NextRequest } from "next/server";

import { LogHandler } from "@/lib/logger";

const logger = new LogHandler(__dirname)

export async function GET(request) {
return NextResponse.json({ message: 'API Health Check' }, { status: 200 });
logger.info("API connected successfully");
return NextResponse.json({ message: "API Health Check" }, { status: 200 });
}
10 changes: 10 additions & 0 deletions src/app/api/v1/server-configs/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import { NextRequest, NextResponse } from 'next/server';

import { POST, GET } from '@/app/api/v1/server-configs/route';
import { validateUserRoles } from '@/app/utils/authentication';
import { handleApiValidationError } from '@/app/utils/error-handler';
import {
CreateServerConfigDto,
Expand All @@ -14,6 +15,10 @@ import { mapDtoToModel, mapModelToDto } from '@/mappers/server-config-mapper';
import { addServerConfigUseCase } from '@/usecases/server-configs/add-server-config';
import { getServerConfigsUseCase } from '@/usecases/server-configs/get-server-configs';

jest.mock('@/app/utils/authentication', () => ({
validateUserRoles: jest.fn(),
}));

jest.mock('@/usecases/server-configs/add-server-config', () => ({
addServerConfigUseCase: jest.fn(),
}));
Expand Down Expand Up @@ -53,6 +58,8 @@ describe('POST /api/v1/server-configs', () => {
endpointUrl: 'https://dto-endpoint-url.com',
};

const mockRoute = '/api/v1/server-configs';

const mockRequest = (body: any) =>
new NextRequest('http://localhost/api/v1/server-configs', {
method: 'POST',
Expand All @@ -65,6 +72,7 @@ describe('POST /api/v1/server-configs', () => {

it('should return server config DTO and status 201 when server config is successfully created', async () => {
(mapDtoToModel as jest.Mock).mockReturnValue(mockServerConfigModel);
(validateUserRoles as jest.Mock).mockResolvedValue(true);
(addServerConfigUseCase as jest.Mock).mockResolvedValue(
mockServerConfigModel,
);
Expand All @@ -83,6 +91,7 @@ describe('POST /api/v1/server-configs', () => {
it('should handle validation errors and return status 422', async () => {
const error = new Error('Validation error');
(addServerConfigUseCase as jest.Mock).mockRejectedValue(error);
(validateUserRoles as jest.Mock).mockResolvedValue(true);
(handleApiValidationError as jest.Mock).mockReturnValue(
NextResponse.json({ message: 'Validation error' }, { status: 422 }),
);
Expand Down Expand Up @@ -148,6 +157,7 @@ describe('GET /api/v1/server-configs', () => {
(getServerConfigsUseCase as jest.Mock).mockResolvedValue([
mockServerConfigModel,
]);
(validateUserRoles as jest.Mock).mockResolvedValue(true);
(mapModelToDto as jest.Mock).mockReturnValue(mockServerConfigDto);

const request = mockRequest();
Expand Down
Loading

0 comments on commit a414246

Please sign in to comment.