diff --git a/apps/auth-e2e/.eslintrc.json b/apps/auth-e2e/.eslintrc.json new file mode 100644 index 0000000..8852e20 --- /dev/null +++ b/apps/auth-e2e/.eslintrc.json @@ -0,0 +1,10 @@ +{ + "extends": ["../../.eslintrc.json"], + "ignorePatterns": ["!**/*"], + "overrides": [ + { + "files": ["*.ts", "*.tsx", "*.js", "*.jsx"], + "rules": {} + } + ] +} diff --git a/apps/auth-e2e/jest.config.ts b/apps/auth-e2e/jest.config.ts new file mode 100644 index 0000000..750cb86 --- /dev/null +++ b/apps/auth-e2e/jest.config.ts @@ -0,0 +1,19 @@ +/* eslint-disable */ +export default { + displayName: 'auth-e2e', + preset: '../../jest.preset.js', + globalSetup: '/src/support/global-setup.ts', + globalTeardown: '/src/support/global-teardown.ts', + setupFiles: ['/src/support/test-setup.ts'], + testEnvironment: 'node', + transform: { + '^.+\\.[tj]s$': [ + 'ts-jest', + { + tsconfig: '/tsconfig.spec.json', + }, + ], + }, + moduleFileExtensions: ['ts', 'js', 'html'], + coverageDirectory: '../../coverage/auth-e2e', +}; diff --git a/apps/auth-e2e/project.json b/apps/auth-e2e/project.json new file mode 100644 index 0000000..bcf1d3a --- /dev/null +++ b/apps/auth-e2e/project.json @@ -0,0 +1,22 @@ +{ + "name": "auth-e2e", + "$schema": "../../node_modules/nx/schemas/project-schema.json", + "implicitDependencies": ["auth"], + "targets": { + "e2e": { + "executor": "@nx/jest:jest", + "outputs": ["{workspaceRoot}/coverage/{e2eProjectRoot}"], + "options": { + "jestConfig": "apps/auth-e2e/jest.config.ts", + "passWithNoTests": true + } + }, + "lint": { + "executor": "@nx/linter:eslint", + "outputs": ["{options.outputFile}"], + "options": { + "lintFilePatterns": ["apps/auth-e2e/**/*.{js,ts}"] + } + } + } +} diff --git a/apps/auth-e2e/src/auth/auth.spec.ts b/apps/auth-e2e/src/auth/auth.spec.ts new file mode 100644 index 0000000..e8ac2a6 --- /dev/null +++ b/apps/auth-e2e/src/auth/auth.spec.ts @@ -0,0 +1,10 @@ +import axios from 'axios'; + +describe('GET /api', () => { + it('should return a message', async () => { + const res = await axios.get(`/api`); + + expect(res.status).toBe(200); + expect(res.data).toEqual({ message: 'Hello API' }); + }); +}); diff --git a/apps/auth-e2e/src/support/global-setup.ts b/apps/auth-e2e/src/support/global-setup.ts new file mode 100644 index 0000000..c1f5144 --- /dev/null +++ b/apps/auth-e2e/src/support/global-setup.ts @@ -0,0 +1,10 @@ +/* eslint-disable */ +var __TEARDOWN_MESSAGE__: string; + +module.exports = async function () { + // Start services that that the app needs to run (e.g. database, docker-compose, etc.). + console.log('\nSetting up...\n'); + + // Hint: Use `globalThis` to pass variables to global teardown. + globalThis.__TEARDOWN_MESSAGE__ = '\nTearing down...\n'; +}; diff --git a/apps/auth-e2e/src/support/global-teardown.ts b/apps/auth-e2e/src/support/global-teardown.ts new file mode 100644 index 0000000..32ea345 --- /dev/null +++ b/apps/auth-e2e/src/support/global-teardown.ts @@ -0,0 +1,7 @@ +/* eslint-disable */ + +module.exports = async function () { + // Put clean up logic here (e.g. stopping services, docker-compose, etc.). + // Hint: `globalThis` is shared between setup and teardown. + console.log(globalThis.__TEARDOWN_MESSAGE__); +}; diff --git a/apps/auth-e2e/src/support/test-setup.ts b/apps/auth-e2e/src/support/test-setup.ts new file mode 100644 index 0000000..07f2870 --- /dev/null +++ b/apps/auth-e2e/src/support/test-setup.ts @@ -0,0 +1,10 @@ +/* eslint-disable */ + +import axios from 'axios'; + +module.exports = async function () { + // Configure axios for tests to use. + const host = process.env.HOST ?? 'localhost'; + const port = process.env.PORT ?? '3000'; + axios.defaults.baseURL = `http://${host}:${port}`; +}; diff --git a/apps/auth-e2e/tsconfig.json b/apps/auth-e2e/tsconfig.json new file mode 100644 index 0000000..ed633e1 --- /dev/null +++ b/apps/auth-e2e/tsconfig.json @@ -0,0 +1,13 @@ +{ + "extends": "../../tsconfig.base.json", + "files": [], + "include": [], + "references": [ + { + "path": "./tsconfig.spec.json" + } + ], + "compilerOptions": { + "esModuleInterop": true + } +} diff --git a/apps/auth-e2e/tsconfig.spec.json b/apps/auth-e2e/tsconfig.spec.json new file mode 100644 index 0000000..d7f9cf2 --- /dev/null +++ b/apps/auth-e2e/tsconfig.spec.json @@ -0,0 +1,9 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "module": "commonjs", + "types": ["jest", "node"] + }, + "include": ["jest.config.ts", "src/**/*.ts"] +} diff --git a/apps/auth/.eslintrc.json b/apps/auth/.eslintrc.json new file mode 100644 index 0000000..9d9c0db --- /dev/null +++ b/apps/auth/.eslintrc.json @@ -0,0 +1,18 @@ +{ + "extends": ["../../.eslintrc.json"], + "ignorePatterns": ["!**/*"], + "overrides": [ + { + "files": ["*.ts", "*.tsx", "*.js", "*.jsx"], + "rules": {} + }, + { + "files": ["*.ts", "*.tsx"], + "rules": {} + }, + { + "files": ["*.js", "*.jsx"], + "rules": {} + } + ] +} diff --git a/apps/auth/jest.config.ts b/apps/auth/jest.config.ts new file mode 100644 index 0000000..b4b7e15 --- /dev/null +++ b/apps/auth/jest.config.ts @@ -0,0 +1,11 @@ +/* eslint-disable */ +export default { + displayName: 'auth', + preset: '../../jest.preset.js', + testEnvironment: 'node', + transform: { + '^.+\\.[tj]s$': ['ts-jest', { tsconfig: '/tsconfig.spec.json' }], + }, + moduleFileExtensions: ['ts', 'js', 'html'], + coverageDirectory: '../../coverage/apps/auth', +}; diff --git a/apps/auth/project.json b/apps/auth/project.json new file mode 100644 index 0000000..76da07a --- /dev/null +++ b/apps/auth/project.json @@ -0,0 +1,64 @@ +{ + "name": "auth", + "$schema": "../../node_modules/nx/schemas/project-schema.json", + "sourceRoot": "apps/auth/src", + "projectType": "application", + "targets": { + "build": { + "executor": "@nx/webpack:webpack", + "outputs": ["{options.outputPath}"], + "defaultConfiguration": "production", + "options": { + "target": "node", + "compiler": "tsc", + "outputPath": "dist/apps/auth", + "main": "apps/auth/src/main.ts", + "tsConfig": "apps/auth/tsconfig.app.json", + "assets": ["apps/auth/src/assets"], + "isolatedConfig": true, + "webpackConfig": "apps/auth/webpack.config.js" + }, + "configurations": { + "development": {}, + "production": {} + } + }, + "serve": { + "executor": "@nx/js:node", + "defaultConfiguration": "development", + "options": { + "buildTarget": "auth:build" + }, + "configurations": { + "development": { + "buildTarget": "auth:build:development" + }, + "production": { + "buildTarget": "auth:build:production" + } + } + }, + "lint": { + "executor": "@nx/linter:eslint", + "outputs": ["{options.outputFile}"], + "options": { + "lintFilePatterns": ["apps/auth/**/*.ts"] + } + }, + "test": { + "executor": "@nx/jest:jest", + "outputs": ["{workspaceRoot}/coverage/{projectRoot}"], + "options": { + "jestConfig": "apps/auth/jest.config.ts", + "passWithNoTests": true + }, + "configurations": { + "ci": { + "ci": true, + "codeCoverage": true + } + } + } + }, + "tags": [] +} diff --git a/apps/auth/src/app/app.controller.spec.ts b/apps/auth/src/app/app.controller.spec.ts new file mode 100644 index 0000000..de8007e --- /dev/null +++ b/apps/auth/src/app/app.controller.spec.ts @@ -0,0 +1,22 @@ +import { Test, TestingModule } from '@nestjs/testing'; + +import { AppController } from './app.controller'; +import { AppService } from './app.service'; + +describe('AppController', () => { + let app: TestingModule; + + beforeAll(async () => { + app = await Test.createTestingModule({ + controllers: [AppController], + providers: [AppService], + }).compile(); + }); + + describe('getData', () => { + it('should return "Hello API"', () => { + const appController = app.get(AppController); + expect(appController.getData()).toEqual({ message: 'Hello API' }); + }); + }); +}); diff --git a/apps/auth/src/app/app.controller.ts b/apps/auth/src/app/app.controller.ts new file mode 100644 index 0000000..dff210a --- /dev/null +++ b/apps/auth/src/app/app.controller.ts @@ -0,0 +1,13 @@ +import { Controller, Get } from '@nestjs/common'; + +import { AppService } from './app.service'; + +@Controller() +export class AppController { + constructor(private readonly appService: AppService) {} + + @Get() + getData() { + return this.appService.getData(); + } +} diff --git a/apps/auth/src/app/app.module.ts b/apps/auth/src/app/app.module.ts new file mode 100644 index 0000000..6a9bc16 --- /dev/null +++ b/apps/auth/src/app/app.module.ts @@ -0,0 +1,11 @@ +import { Module } from '@nestjs/common'; + +import { AppController } from './app.controller'; +import { AppService } from './app.service'; + +@Module({ + imports: [], + controllers: [AppController], + providers: [AppService], +}) +export class AppModule {} diff --git a/apps/auth/src/app/app.service.spec.ts b/apps/auth/src/app/app.service.spec.ts new file mode 100644 index 0000000..42cf0a2 --- /dev/null +++ b/apps/auth/src/app/app.service.spec.ts @@ -0,0 +1,21 @@ +import { Test } from '@nestjs/testing'; + +import { AppService } from './app.service'; + +describe('AppService', () => { + let service: AppService; + + beforeAll(async () => { + const app = await Test.createTestingModule({ + providers: [AppService], + }).compile(); + + service = app.get(AppService); + }); + + describe('getData', () => { + it('should return "Hello API"', () => { + expect(service.getData()).toEqual({ message: 'Hello API' }); + }); + }); +}); diff --git a/apps/auth/src/app/app.service.ts b/apps/auth/src/app/app.service.ts new file mode 100644 index 0000000..cd8cede --- /dev/null +++ b/apps/auth/src/app/app.service.ts @@ -0,0 +1,8 @@ +import { Injectable } from '@nestjs/common'; + +@Injectable() +export class AppService { + getData(): { message: string } { + return { message: 'Hello API' }; + } +} diff --git a/apps/auth/src/assets/.gitkeep b/apps/auth/src/assets/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/apps/auth/src/main.ts b/apps/auth/src/main.ts new file mode 100644 index 0000000..a124382 --- /dev/null +++ b/apps/auth/src/main.ts @@ -0,0 +1,22 @@ +/** + * This is not a production server yet! + * This is only a minimal backend to get started. + */ + +import { Logger } from '@nestjs/common'; +import { NestFactory } from '@nestjs/core'; + +import { AppModule } from './app/app.module'; + +async function bootstrap() { + const app = await NestFactory.create(AppModule); + const globalPrefix = 'api'; + app.setGlobalPrefix(globalPrefix); + const port = process.env.PORT || 3000; + await app.listen(port); + Logger.log( + `πŸš€ Application is running on: http://localhost:${port}/${globalPrefix}` + ); +} + +bootstrap(); diff --git a/apps/auth/tsconfig.app.json b/apps/auth/tsconfig.app.json new file mode 100644 index 0000000..a2ce765 --- /dev/null +++ b/apps/auth/tsconfig.app.json @@ -0,0 +1,12 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "module": "commonjs", + "types": ["node"], + "emitDecoratorMetadata": true, + "target": "es2021" + }, + "exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"], + "include": ["src/**/*.ts"] +} diff --git a/apps/auth/tsconfig.json b/apps/auth/tsconfig.json new file mode 100644 index 0000000..c1e2dd4 --- /dev/null +++ b/apps/auth/tsconfig.json @@ -0,0 +1,16 @@ +{ + "extends": "../../tsconfig.base.json", + "files": [], + "include": [], + "references": [ + { + "path": "./tsconfig.app.json" + }, + { + "path": "./tsconfig.spec.json" + } + ], + "compilerOptions": { + "esModuleInterop": true + } +} diff --git a/apps/auth/tsconfig.spec.json b/apps/auth/tsconfig.spec.json new file mode 100644 index 0000000..9b2a121 --- /dev/null +++ b/apps/auth/tsconfig.spec.json @@ -0,0 +1,14 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "module": "commonjs", + "types": ["jest", "node"] + }, + "include": [ + "jest.config.ts", + "src/**/*.test.ts", + "src/**/*.spec.ts", + "src/**/*.d.ts" + ] +} diff --git a/apps/auth/webpack.config.js b/apps/auth/webpack.config.js new file mode 100644 index 0000000..81db92b --- /dev/null +++ b/apps/auth/webpack.config.js @@ -0,0 +1,8 @@ +const { composePlugins, withNx } = require('@nx/webpack'); + +// Nx plugins for webpack. +module.exports = composePlugins(withNx(), (config) => { + // Update the webpack config as needed here. + // e.g. `config.plugins.push(new MyPlugin())` + return config; +}); diff --git a/apps/frontend/src/assets/CTF.jpg b/apps/frontend/src/assets/CTF.jpg new file mode 100644 index 0000000..0801447 Binary files /dev/null and b/apps/frontend/src/assets/CTF.jpg differ diff --git a/apps/user-data-e2e/.eslintrc.json b/apps/user-data-e2e/.eslintrc.json new file mode 100644 index 0000000..8852e20 --- /dev/null +++ b/apps/user-data-e2e/.eslintrc.json @@ -0,0 +1,10 @@ +{ + "extends": ["../../.eslintrc.json"], + "ignorePatterns": ["!**/*"], + "overrides": [ + { + "files": ["*.ts", "*.tsx", "*.js", "*.jsx"], + "rules": {} + } + ] +} diff --git a/apps/user-data-e2e/jest.config.ts b/apps/user-data-e2e/jest.config.ts new file mode 100644 index 0000000..3f25bd3 --- /dev/null +++ b/apps/user-data-e2e/jest.config.ts @@ -0,0 +1,19 @@ +/* eslint-disable */ +export default { + displayName: 'user-data-e2e', + preset: '../../jest.preset.js', + globalSetup: '/src/support/global-setup.ts', + globalTeardown: '/src/support/global-teardown.ts', + setupFiles: ['/src/support/test-setup.ts'], + testEnvironment: 'node', + transform: { + '^.+\\.[tj]s$': [ + 'ts-jest', + { + tsconfig: '/tsconfig.spec.json', + }, + ], + }, + moduleFileExtensions: ['ts', 'js', 'html'], + coverageDirectory: '../../coverage/user-data-e2e', +}; diff --git a/apps/user-data-e2e/project.json b/apps/user-data-e2e/project.json new file mode 100644 index 0000000..840891a --- /dev/null +++ b/apps/user-data-e2e/project.json @@ -0,0 +1,22 @@ +{ + "name": "user-data-e2e", + "$schema": "../../node_modules/nx/schemas/project-schema.json", + "implicitDependencies": ["user-data"], + "targets": { + "e2e": { + "executor": "@nx/jest:jest", + "outputs": ["{workspaceRoot}/coverage/{e2eProjectRoot}"], + "options": { + "jestConfig": "apps/user-data-e2e/jest.config.ts", + "passWithNoTests": true + } + }, + "lint": { + "executor": "@nx/linter:eslint", + "outputs": ["{options.outputFile}"], + "options": { + "lintFilePatterns": ["apps/user-data-e2e/**/*.{js,ts}"] + } + } + } +} diff --git a/apps/user-data-e2e/src/support/global-setup.ts b/apps/user-data-e2e/src/support/global-setup.ts new file mode 100644 index 0000000..c1f5144 --- /dev/null +++ b/apps/user-data-e2e/src/support/global-setup.ts @@ -0,0 +1,10 @@ +/* eslint-disable */ +var __TEARDOWN_MESSAGE__: string; + +module.exports = async function () { + // Start services that that the app needs to run (e.g. database, docker-compose, etc.). + console.log('\nSetting up...\n'); + + // Hint: Use `globalThis` to pass variables to global teardown. + globalThis.__TEARDOWN_MESSAGE__ = '\nTearing down...\n'; +}; diff --git a/apps/user-data-e2e/src/support/global-teardown.ts b/apps/user-data-e2e/src/support/global-teardown.ts new file mode 100644 index 0000000..32ea345 --- /dev/null +++ b/apps/user-data-e2e/src/support/global-teardown.ts @@ -0,0 +1,7 @@ +/* eslint-disable */ + +module.exports = async function () { + // Put clean up logic here (e.g. stopping services, docker-compose, etc.). + // Hint: `globalThis` is shared between setup and teardown. + console.log(globalThis.__TEARDOWN_MESSAGE__); +}; diff --git a/apps/user-data-e2e/src/support/test-setup.ts b/apps/user-data-e2e/src/support/test-setup.ts new file mode 100644 index 0000000..07f2870 --- /dev/null +++ b/apps/user-data-e2e/src/support/test-setup.ts @@ -0,0 +1,10 @@ +/* eslint-disable */ + +import axios from 'axios'; + +module.exports = async function () { + // Configure axios for tests to use. + const host = process.env.HOST ?? 'localhost'; + const port = process.env.PORT ?? '3000'; + axios.defaults.baseURL = `http://${host}:${port}`; +}; diff --git a/apps/user-data-e2e/src/user-data/user-data.spec.ts b/apps/user-data-e2e/src/user-data/user-data.spec.ts new file mode 100644 index 0000000..e8ac2a6 --- /dev/null +++ b/apps/user-data-e2e/src/user-data/user-data.spec.ts @@ -0,0 +1,10 @@ +import axios from 'axios'; + +describe('GET /api', () => { + it('should return a message', async () => { + const res = await axios.get(`/api`); + + expect(res.status).toBe(200); + expect(res.data).toEqual({ message: 'Hello API' }); + }); +}); diff --git a/apps/user-data-e2e/tsconfig.json b/apps/user-data-e2e/tsconfig.json new file mode 100644 index 0000000..ed633e1 --- /dev/null +++ b/apps/user-data-e2e/tsconfig.json @@ -0,0 +1,13 @@ +{ + "extends": "../../tsconfig.base.json", + "files": [], + "include": [], + "references": [ + { + "path": "./tsconfig.spec.json" + } + ], + "compilerOptions": { + "esModuleInterop": true + } +} diff --git a/apps/user-data-e2e/tsconfig.spec.json b/apps/user-data-e2e/tsconfig.spec.json new file mode 100644 index 0000000..d7f9cf2 --- /dev/null +++ b/apps/user-data-e2e/tsconfig.spec.json @@ -0,0 +1,9 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "module": "commonjs", + "types": ["jest", "node"] + }, + "include": ["jest.config.ts", "src/**/*.ts"] +} diff --git a/apps/user-data/.ASC_MANIFEST b/apps/user-data/.ASC_MANIFEST new file mode 100644 index 0000000..63abe52 --- /dev/null +++ b/apps/user-data/.ASC_MANIFEST @@ -0,0 +1,13 @@ +$$ +@version: 0.1.0; +@scuuid: 27895030-15ec-11ee-be56-0242ac120002; +@type: service; +@platform: nestjs; +@license: BSD-3-Clause; +@owner: artem-darius weber; +@author: ; +@title: user-data; +@desc: ; +@rp: kubsu it lab; +@vr: 7093; +$$ diff --git a/apps/user-data/.env.example b/apps/user-data/.env.example new file mode 100644 index 0000000..6c89035 --- /dev/null +++ b/apps/user-data/.env.example @@ -0,0 +1,8 @@ +# Environment variables declared in this file are automatically made available to Prisma. +# See the documentation for more detail: https://pris.ly/d/prisma-schema#accessing-environment-variables-from-the-schema + +# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB. +# See the documentation for all the connection string options: https://pris.ly/d/connection-strings + +DATABASE_URL=postgresql://postgres:postgres@sc-user-data-database/sc-user-data +PORT=3002 diff --git a/apps/user-data/.eslintrc.json b/apps/user-data/.eslintrc.json new file mode 100644 index 0000000..9d9c0db --- /dev/null +++ b/apps/user-data/.eslintrc.json @@ -0,0 +1,18 @@ +{ + "extends": ["../../.eslintrc.json"], + "ignorePatterns": ["!**/*"], + "overrides": [ + { + "files": ["*.ts", "*.tsx", "*.js", "*.jsx"], + "rules": {} + }, + { + "files": ["*.ts", "*.tsx"], + "rules": {} + }, + { + "files": ["*.js", "*.jsx"], + "rules": {} + } + ] +} diff --git a/apps/user-data/.gitignore b/apps/user-data/.gitignore new file mode 100644 index 0000000..11ddd8d --- /dev/null +++ b/apps/user-data/.gitignore @@ -0,0 +1,3 @@ +node_modules +# Keep environment variables out of version control +.env diff --git a/apps/user-data/Dockerfile b/apps/user-data/Dockerfile new file mode 100644 index 0000000..f0b43cf --- /dev/null +++ b/apps/user-data/Dockerfile @@ -0,0 +1,32 @@ +# syntax=docker/dockerfile:1 +FROM node:18 + +WORKDIR /app + +COPY --chown=node:node . . + +RUN rm -f *.env *.env.* + +RUN apt-get update -y && apt-get install -y dumb-init + +RUN npm install + +ENV NODE_ENV=production + +RUN npm run prefullbuild && npm run prebuild && npm run build + +RUN mkdir temp temp/.prisma temp/@prisma temp/prisma && cp -r ./node_modules/.prisma/* ./temp/.prisma/ && cp -r ./node_modules/@prisma/* ./temp/@prisma/ && cp -r ./node_modules/prisma/* ./temp/prisma/ + +RUN rm -rdf node_modules + +RUN npm install --production + +RUN cp -r ./temp/* ./node_modules/ && rm -rdf temp + +RUN ls | grep -v node_modules | grep -v dist | xargs rm -rfv + +RUN cp -r ./dist/* ./ && rm -rdf dist + +USER node + +CMD ["dumb-init", "node", "./main.js"] diff --git a/apps/user-data/LICENSE b/apps/user-data/LICENSE new file mode 100644 index 0000000..d62cc9d --- /dev/null +++ b/apps/user-data/LICENSE @@ -0,0 +1,11 @@ +Copyright 2023 SC (DJEEFT) Β© + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS β€œAS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/apps/user-data/jest.config.ts b/apps/user-data/jest.config.ts new file mode 100644 index 0000000..98babab --- /dev/null +++ b/apps/user-data/jest.config.ts @@ -0,0 +1,11 @@ +/* eslint-disable */ +export default { + displayName: 'user-data', + preset: '../../jest.preset.js', + testEnvironment: 'node', + transform: { + '^.+\\.[tj]s$': ['ts-jest', { tsconfig: '/tsconfig.spec.json' }], + }, + moduleFileExtensions: ['ts', 'js', 'html'], + coverageDirectory: '../../coverage/apps/user-data', +}; diff --git a/apps/user-data/prisma/schema.prisma b/apps/user-data/prisma/schema.prisma new file mode 100644 index 0000000..4b6ad66 --- /dev/null +++ b/apps/user-data/prisma/schema.prisma @@ -0,0 +1,53 @@ +datasource db { + provider = "postgresql" + url = env("DATABASE_URL") +} + +generator client { + provider = "prisma-client-js" +} + +model User { + uuid String @id @default(uuid()) @db.Uuid + + email Email? @relation("mainEmail") + phoneNumber PhoneNumber? @relation("mainPhoneNumber") + + reserveEmails Email[] @relation("reserveEmails") + reservePhoneNumbers PhoneNumber[] @relation("reservePhoneNumbers") + + extendedData Json @default("{}") @db.JsonB + + createdAt DateTime @db.Timestamp(3) + updatedAt DateTime @db.Timestamp(3) +} + +model Email { + uuid String @id @default(uuid()) @db.Uuid + + email String @unique @db.VarChar(256) + + userUuid String? @unique @db.Uuid + user User? @relation("mainEmail", fields: [userUuid], references: [uuid]) + + userUuidReserve String? @db.Uuid + userReserve User? @relation("reserveEmails", fields: [userUuidReserve], references: [uuid]) + + createdAt DateTime @db.Timestamp(3) + updatedAt DateTime @db.Timestamp(3) +} + +model PhoneNumber { + uuid String @id @default(uuid()) @db.Uuid + + phoneNumber String @unique @db.VarChar(20) + + userUuid String? @unique @db.Uuid + user User? @relation("mainPhoneNumber", fields: [userUuid], references: [uuid]) + + userUuidReserve String? @db.Uuid + userReserve User? @relation("reservePhoneNumbers", fields: [userUuidReserve], references: [uuid]) + + createdAt DateTime @db.Timestamp(3) + updatedAt DateTime @db.Timestamp(3) +} diff --git a/apps/user-data/project.json b/apps/user-data/project.json new file mode 100644 index 0000000..d34fe8e --- /dev/null +++ b/apps/user-data/project.json @@ -0,0 +1,64 @@ +{ + "name": "user-data", + "$schema": "../../node_modules/nx/schemas/project-schema.json", + "sourceRoot": "apps/user-data/src", + "projectType": "application", + "targets": { + "build": { + "executor": "@nx/webpack:webpack", + "outputs": ["{options.outputPath}"], + "defaultConfiguration": "production", + "options": { + "target": "node", + "compiler": "tsc", + "outputPath": "dist/apps/user-data", + "main": "apps/user-data/src/main.ts", + "tsConfig": "apps/user-data/tsconfig.app.json", + "assets": ["apps/user-data/src/assets"], + "isolatedConfig": true, + "webpackConfig": "apps/user-data/webpack.config.js" + }, + "configurations": { + "development": {}, + "production": {} + } + }, + "serve": { + "executor": "@nx/js:node", + "defaultConfiguration": "development", + "options": { + "buildTarget": "user-data:build" + }, + "configurations": { + "development": { + "buildTarget": "user-data:build:development" + }, + "production": { + "buildTarget": "user-data:build:production" + } + } + }, + "lint": { + "executor": "@nx/linter:eslint", + "outputs": ["{options.outputFile}"], + "options": { + "lintFilePatterns": ["apps/user-data/**/*.ts"] + } + }, + "test": { + "executor": "@nx/jest:jest", + "outputs": ["{workspaceRoot}/coverage/{projectRoot}"], + "options": { + "jestConfig": "apps/user-data/jest.config.ts", + "passWithNoTests": true + }, + "configurations": { + "ci": { + "ci": true, + "codeCoverage": true + } + } + } + }, + "tags": [] +} diff --git a/apps/user-data/serving/.gitignore b/apps/user-data/serving/.gitignore new file mode 100644 index 0000000..7278035 --- /dev/null +++ b/apps/user-data/serving/.gitignore @@ -0,0 +1,225 @@ +*.env +.idea + +### NotepadPP template +# Notepad++ backups # +*.bak + +### VisualStudioCode template +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +*.code-workspace + +# Local History for Visual Studio Code +.history/ + +### JetBrains template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +### Xcode template +# Xcode +# +# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore + +## User settings +xcuserdata/ + +## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9) +*.xcscmblueprint +*.xccheckout + +## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4) +build/ +DerivedData/ +*.moved-aside +*.pbxuser +!default.pbxuser +*.mode1v3 +!default.mode1v3 +*.mode2v3 +!default.mode2v3 +*.perspectivev3 +!default.perspectivev3 + +## Gcc Patch +/*.gcno + +### Linux template +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +### Kate template +# Swap Files # +.*.kate-swp +.swp.* + +### Windows template +# Windows thumbnail cache files +Thumbs.db +Thumbs.db:encryptable +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +### macOS template +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +### SublimeText template +# Cache files for Sublime Text +*.tmlanguage.cache +*.tmPreferences.cache +*.stTheme.cache + +# Workspace files are user-specific +*.sublime-workspace + +# Project files should be checked into the repository, unless a significant +# proportion of contributors will probably not be using Sublime Text +# *.sublime-project + +# SFTP configuration file +sftp-config.json +sftp-config-alt*.json + +# Package control specific files +Package Control.last-run +Package Control.ca-list +Package Control.ca-bundle +Package Control.system-ca-bundle +Package Control.cache/ +Package Control.ca-certs/ +Package Control.merged-ca-bundle +Package Control.user-ca-bundle +oscrypto-ca-bundle.crt +bh_unicode_properties.cache + +# Sublime-github package stores a github token in this file +# https://packagecontrol.io/packages/sublime-github +GitHub.sublime-settings diff --git a/apps/user-data/serving/database.env.example b/apps/user-data/serving/database.env.example new file mode 100644 index 0000000..c44f3ae --- /dev/null +++ b/apps/user-data/serving/database.env.example @@ -0,0 +1,3 @@ +POSTGRES_DB=sc-user-data +POSTGRES_USER=postgres +POSTGRES_PASSWORD=postgres diff --git a/apps/user-data/serving/docker-compose.yaml b/apps/user-data/serving/docker-compose.yaml new file mode 100644 index 0000000..0e8a651 --- /dev/null +++ b/apps/user-data/serving/docker-compose.yaml @@ -0,0 +1,34 @@ +version: "3.9" + +services: + user-data-database: + image: postgres:latest + container_name: user-data-database + restart: always + networks: + - user-data + volumes: + - user-data-database:/var/lib/postgresql + env_file: + - ./database.env + user-data-database-admin: + container_name: user-data-database-admin + image: bitnami/phppgadmin:latest + restart: always + networks: + - user-data + depends_on: + - user-data-database + environment: + - DATABASE_HOST=user-data-database + ports: + - "8083:8080" + +networks: + user-data: + name: user-data + driver: bridge + +volumes: + user-data-database: + driver: local diff --git a/apps/user-data/src/app/app.controller.spec.ts b/apps/user-data/src/app/app.controller.spec.ts new file mode 100644 index 0000000..a6e8d35 --- /dev/null +++ b/apps/user-data/src/app/app.controller.spec.ts @@ -0,0 +1,22 @@ +import { Test, TestingModule } from '@nestjs/testing'; + +import { AppController } from './app.controller'; +import { AppService } from './service/app.service'; + +describe('AppController', () => { + let app: TestingModule; + + beforeAll(async () => { + app = await Test.createTestingModule({ + controllers: [AppController], + providers: [AppService], + }).compile(); + }); + + describe('getData', () => { + it('should return "Hello API"', () => { + const appController = app.get(AppController); + expect(appController.getData()).toEqual({ message: 'Hello API' }); + }); + }); +}); diff --git a/apps/user-data/src/app/app.controller.ts b/apps/user-data/src/app/app.controller.ts new file mode 100644 index 0000000..0c6a72d --- /dev/null +++ b/apps/user-data/src/app/app.controller.ts @@ -0,0 +1,13 @@ +import { Controller, Get } from '@nestjs/common'; + +import { AppService } from './service/app.service'; + +@Controller() +export class AppController { + constructor(private readonly appService: AppService) {} + + @Get() + getData() { + return this.appService.getData(); + } +} diff --git a/apps/user-data/src/app/app.module.ts b/apps/user-data/src/app/app.module.ts new file mode 100644 index 0000000..e8f09b7 --- /dev/null +++ b/apps/user-data/src/app/app.module.ts @@ -0,0 +1,14 @@ +import { Module } from '@nestjs/common'; + +import { AppController } from './app.controller'; +import { AppService } from './service/app.service'; +import { UserModule } from "../user/user.module"; +import { EmailModule } from "../email/email.module"; +import { PhoneNumberModule } from "../phone-number/phone-number.module"; + +@Module({ + imports: [UserModule, EmailModule, PhoneNumberModule], + controllers: [AppController], + providers: [AppService], +}) +export class AppModule {} diff --git a/apps/user-data/src/app/service/app.service.spec.ts b/apps/user-data/src/app/service/app.service.spec.ts new file mode 100644 index 0000000..42cf0a2 --- /dev/null +++ b/apps/user-data/src/app/service/app.service.spec.ts @@ -0,0 +1,21 @@ +import { Test } from '@nestjs/testing'; + +import { AppService } from './app.service'; + +describe('AppService', () => { + let service: AppService; + + beforeAll(async () => { + const app = await Test.createTestingModule({ + providers: [AppService], + }).compile(); + + service = app.get(AppService); + }); + + describe('getData', () => { + it('should return "Hello API"', () => { + expect(service.getData()).toEqual({ message: 'Hello API' }); + }); + }); +}); diff --git a/apps/user-data/src/app/service/app.service.ts b/apps/user-data/src/app/service/app.service.ts new file mode 100644 index 0000000..cd8cede --- /dev/null +++ b/apps/user-data/src/app/service/app.service.ts @@ -0,0 +1,8 @@ +import { Injectable } from '@nestjs/common'; + +@Injectable() +export class AppService { + getData(): { message: string } { + return { message: 'Hello API' }; + } +} diff --git a/apps/user-data/src/assets/.gitkeep b/apps/user-data/src/assets/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/apps/user-data/src/email/email.controller.spec.ts b/apps/user-data/src/email/email.controller.spec.ts new file mode 100644 index 0000000..9470aa8 --- /dev/null +++ b/apps/user-data/src/email/email.controller.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { EmailController } from './email.controller'; + +describe('EmailController', () => { + let controller: EmailController; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + controllers: [EmailController], + }).compile(); + + controller = module.get(EmailController); + }); + + it('should be defined', () => { + expect(controller).toBeDefined(); + }); +}); diff --git a/apps/user-data/src/email/email.controller.ts b/apps/user-data/src/email/email.controller.ts new file mode 100644 index 0000000..f4dade4 --- /dev/null +++ b/apps/user-data/src/email/email.controller.ts @@ -0,0 +1,53 @@ +import { Body, Controller, HttpCode, Inject, Post, UseInterceptors } from "@nestjs/common"; +import { Prisma, Email } from "@prisma/client"; +import { EmailService } from "./service/email.service"; +import { PrismaErrorInterceptor } from "../prisma/interceptor/prisma-error.interceptor"; + +@Controller("email") +@UseInterceptors(PrismaErrorInterceptor) +export class EmailController { + constructor(@Inject(EmailService) private email: EmailService) { + } + + @Post("find/unique") + @HttpCode(200) + async FindUnique(@Body() emailFindUniqueArgs: Prisma.EmailFindUniqueArgs): Promise | null> { + return await this.email.FindUnique(emailFindUniqueArgs); + } + + @Post("find/first") + @HttpCode(200) + async FindFirst(@Body() emailFindFirstArgs: Prisma.EmailFindFirstArgs): Promise | null> { + return await this.email.FindFirst(emailFindFirstArgs); + } + + @Post("find/many") + @HttpCode(200) + async FindMany(@Body() emailFindManyArgs: Prisma.EmailFindManyArgs): Promise[]> { + return await this.email.FindMany(emailFindManyArgs); + } + + @Post("update") + @HttpCode(200) + async Update(@Body() emailUpdateArgs: Prisma.EmailUpdateArgs): Promise> { + return await this.email.Update(emailUpdateArgs); + } + + @Post("count") + @HttpCode(200) + async Count(@Body() emailCountArgs: Prisma.EmailCountArgs): Promise { + return await this.email.Count(emailCountArgs); + } + + @Post("create") + @HttpCode(200) + async Create(@Body() emailCreateArgs: Prisma.EmailCreateArgs): Promise> { + return await this.email.Create(emailCreateArgs); + } + + @Post("delete") + @HttpCode(200) + async Delete(@Body() emailDeleteArgs: Prisma.EmailDeleteArgs): Promise> { + return await this.email.Delete(emailDeleteArgs); + } +} diff --git a/apps/user-data/src/email/email.module.ts b/apps/user-data/src/email/email.module.ts new file mode 100644 index 0000000..693fee6 --- /dev/null +++ b/apps/user-data/src/email/email.module.ts @@ -0,0 +1,12 @@ +import { Module } from "@nestjs/common"; +import { EmailController } from "./email.controller"; +import { EmailService } from "./service/email.service"; +import { PrismaModule } from "../prisma/prisma.module"; + +@Module({ + imports: [PrismaModule], + controllers: [EmailController], + providers: [EmailService] +}) +export class EmailModule { +} diff --git a/apps/user-data/src/email/service/email.service.spec.ts b/apps/user-data/src/email/service/email.service.spec.ts new file mode 100644 index 0000000..27719da --- /dev/null +++ b/apps/user-data/src/email/service/email.service.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { EmailService } from './email.service'; + +describe('EmailService', () => { + let service: EmailService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [EmailService], + }).compile(); + + service = module.get(EmailService); + }); + + it('should be defined', () => { + expect(service).toBeDefined(); + }); +}); diff --git a/apps/user-data/src/email/service/email.service.ts b/apps/user-data/src/email/service/email.service.ts new file mode 100644 index 0000000..b64799c --- /dev/null +++ b/apps/user-data/src/email/service/email.service.ts @@ -0,0 +1,62 @@ +import { Inject, Injectable } from "@nestjs/common"; +import { PrismaService } from '../../prisma/service/prisma.service'; +import { Email, Prisma } from "@prisma/client"; +import { PrismaClientKnownRequestError } from "@prisma/client/runtime"; +import { UniqueConstraintFailedError } from "../../prisma/error/unique-constraint-failed.error"; +import { RecordNotFoundError } from "../../prisma/error/record-not-found.error"; + +@Injectable() +export class EmailService { + constructor(@Inject(PrismaService) private prisma: PrismaService) { + } + + async FindUnique(emailFindUniqueArgs: Prisma.EmailFindUniqueArgs): Promise | null> { + return await this.prisma.email.findUnique(emailFindUniqueArgs); + } + + async FindFirst(emailFindFirstArgs: Prisma.EmailFindFirstArgs): Promise | null> { + return await this.prisma.email.findFirst(emailFindFirstArgs); + } + + async FindMany(emailFindManyArgs: Prisma.EmailFindManyArgs): Promise[]> { + return await this.prisma.email.findMany(emailFindManyArgs); + } + + async Update(emailUpdateArgs: Prisma.EmailUpdateArgs): Promise> { + try { + return await this.prisma.email.update(emailUpdateArgs); + } catch (e) { + if (e instanceof PrismaClientKnownRequestError) { + if (e.code === "P2025") throw new RecordNotFoundError("Record to update not found."); + if (e.code === "P2002") throw new UniqueConstraintFailedError("Unique constant failed"); + } + throw e; + } + } + + async Count(emailCountArgs: Prisma.EmailCountArgs): Promise { + return await this.prisma.email.count(emailCountArgs); + } + + async Create(emailCreateArgs: Prisma.EmailCreateArgs): Promise> { + try { + return await this.prisma.email.create(emailCreateArgs); + } catch (e) { + if (e instanceof PrismaClientKnownRequestError) { + if (e.code === "P2002") throw new UniqueConstraintFailedError("Unique constant failed"); + } + throw e; + } + } + + async Delete(emailDeleteArgs: Prisma.EmailDeleteArgs): Promise> { + try { + return await this.prisma.email.delete(emailDeleteArgs); + } catch (e) { + if (e instanceof PrismaClientKnownRequestError) { + if (e.code === "P2025") throw new RecordNotFoundError("Record to delete not found."); + } + throw e; + } + } +} diff --git a/apps/user-data/src/main.ts b/apps/user-data/src/main.ts new file mode 100644 index 0000000..5794fe8 --- /dev/null +++ b/apps/user-data/src/main.ts @@ -0,0 +1,22 @@ +/** + * This is not a production server yet! + * This is only a minimal backend to get started. + */ + +import { Logger } from '@nestjs/common'; +import { NestFactory } from '@nestjs/core'; + +import { AppModule } from './app/app.module'; + +async function bootstrap() { + const app = await NestFactory.create(AppModule); + const globalPrefix = 'api'; + app.setGlobalPrefix(globalPrefix); + const port = process.env.PORT || 3002; + await app.listen(port); + Logger.log( + `πŸš€ User Data Application is running on: http://localhost:${port}/${globalPrefix}` + ); +} + +bootstrap(); diff --git a/apps/user-data/src/phone-number/phone-number.controller.spec.ts b/apps/user-data/src/phone-number/phone-number.controller.spec.ts new file mode 100644 index 0000000..ed89b8a --- /dev/null +++ b/apps/user-data/src/phone-number/phone-number.controller.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { PhoneNumberController } from './phone-number.controller'; + +describe('PhoneNumberController', () => { + let controller: PhoneNumberController; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + controllers: [PhoneNumberController], + }).compile(); + + controller = module.get(PhoneNumberController); + }); + + it('should be defined', () => { + expect(controller).toBeDefined(); + }); +}); diff --git a/apps/user-data/src/phone-number/phone-number.controller.ts b/apps/user-data/src/phone-number/phone-number.controller.ts new file mode 100644 index 0000000..a4fae62 --- /dev/null +++ b/apps/user-data/src/phone-number/phone-number.controller.ts @@ -0,0 +1,53 @@ +import { Body, Controller, HttpCode, Inject, Post, UseInterceptors } from "@nestjs/common"; +import { PhoneNumber, Prisma } from "@prisma/client"; +import { PhoneNumberService } from "./service/phone-number.service"; +import { PrismaErrorInterceptor } from "../prisma/interceptor/prisma-error.interceptor"; + +@Controller("phoneNumber") +@UseInterceptors(PrismaErrorInterceptor) +export class PhoneNumberController { + constructor(@Inject(PhoneNumberService) private phoneNumber: PhoneNumberService) { + } + + @Post("find/unique") + @HttpCode(200) + async FindUnique(@Body() phoneNumberFindUniqueArgs: Prisma.PhoneNumberFindUniqueArgs): Promise | null> { + return await this.phoneNumber.FindUnique(phoneNumberFindUniqueArgs); + } + + @Post("find/first") + @HttpCode(200) + async FindFirst(@Body() phoneNumberFindFirstArgs: Prisma.PhoneNumberFindFirstArgs): Promise | null> { + return await this.phoneNumber.FindFirst(phoneNumberFindFirstArgs); + } + + @Post("find/many") + @HttpCode(200) + async FindMany(@Body() phoneNumberFindManyArgs: Prisma.PhoneNumberFindManyArgs): Promise[]> { + return await this.phoneNumber.FindMany(phoneNumberFindManyArgs); + } + + @Post("update") + @HttpCode(200) + async Update(@Body() phoneNumberUpdateArgs: Prisma.PhoneNumberUpdateArgs): Promise> { + return await this.phoneNumber.Update(phoneNumberUpdateArgs); + } + + @Post("count") + @HttpCode(200) + async Count(@Body() phoneNumberCountArgs: Prisma.PhoneNumberCountArgs): Promise { + return await this.phoneNumber.Count(phoneNumberCountArgs); + } + + @Post("create") + @HttpCode(200) + async Create(@Body() phoneNumberCreateArgs: Prisma.PhoneNumberCreateArgs): Promise> { + return await this.phoneNumber.Create(phoneNumberCreateArgs); + } + + @Post("delete") + @HttpCode(200) + async Delete(@Body() phoneNumberDeleteArgs: Prisma.PhoneNumberDeleteArgs): Promise> { + return await this.phoneNumber.Delete(phoneNumberDeleteArgs); + } +} diff --git a/apps/user-data/src/phone-number/phone-number.module.ts b/apps/user-data/src/phone-number/phone-number.module.ts new file mode 100644 index 0000000..4a8e411 --- /dev/null +++ b/apps/user-data/src/phone-number/phone-number.module.ts @@ -0,0 +1,12 @@ +import { Module } from "@nestjs/common"; +import { PhoneNumberController } from "./phone-number.controller"; +import { PhoneNumberService } from "./service/phone-number.service"; +import { PrismaModule } from "../prisma/prisma.module"; + +@Module({ + imports: [PrismaModule], + controllers: [PhoneNumberController], + providers: [PhoneNumberService] +}) +export class PhoneNumberModule { +} diff --git a/apps/user-data/src/phone-number/service/phone-number.service.spec.ts b/apps/user-data/src/phone-number/service/phone-number.service.spec.ts new file mode 100644 index 0000000..9b49efb --- /dev/null +++ b/apps/user-data/src/phone-number/service/phone-number.service.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { PhoneNumberService } from './phone-number.service'; + +describe('PhoneNumberService', () => { + let service: PhoneNumberService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [PhoneNumberService], + }).compile(); + + service = module.get(PhoneNumberService); + }); + + it('should be defined', () => { + expect(service).toBeDefined(); + }); +}); diff --git a/apps/user-data/src/phone-number/service/phone-number.service.ts b/apps/user-data/src/phone-number/service/phone-number.service.ts new file mode 100644 index 0000000..150bf3d --- /dev/null +++ b/apps/user-data/src/phone-number/service/phone-number.service.ts @@ -0,0 +1,62 @@ +import { Inject, Injectable } from "@nestjs/common"; +import { PrismaService } from "../../prisma/service/prisma.service"; +import { PhoneNumber, Prisma } from "@prisma/client"; +import { PrismaClientKnownRequestError } from "@prisma/client/runtime"; +import { UniqueConstraintFailedError } from "../../prisma/error/unique-constraint-failed.error"; +import { RecordNotFoundError } from "../../prisma/error/record-not-found.error"; + +@Injectable() +export class PhoneNumberService { + constructor(@Inject(PrismaService) private prisma: PrismaService) { + } + + async FindUnique(phoneNumberFindUniqueArgs: Prisma.PhoneNumberFindUniqueArgs): Promise | null> { + return await this.prisma.phoneNumber.findUnique(phoneNumberFindUniqueArgs); + } + + async FindFirst(phoneNumberFindFirstArgs: Prisma.PhoneNumberFindFirstArgs): Promise | null> { + return await this.prisma.phoneNumber.findFirst(phoneNumberFindFirstArgs); + } + + async FindMany(phoneNumberFindManyArgs: Prisma.PhoneNumberFindManyArgs): Promise[]> { + return await this.prisma.phoneNumber.findMany(phoneNumberFindManyArgs); + } + + async Update(phoneNumberUpdateArgs: Prisma.PhoneNumberUpdateArgs): Promise> { + try { + return await this.prisma.phoneNumber.update(phoneNumberUpdateArgs); + } catch (e) { + if (e instanceof PrismaClientKnownRequestError) { + if (e.code === "P2025") throw new RecordNotFoundError("Record to update not found."); + if (e.code === "P2002") throw new UniqueConstraintFailedError("Unique constant failed"); + } + throw e; + } + } + + async Count(phoneNumberCountArgs: Prisma.PhoneNumberCountArgs): Promise { + return await this.prisma.phoneNumber.count(phoneNumberCountArgs); + } + + async Create(phoneNumberCreateArgs: Prisma.PhoneNumberCreateArgs): Promise> { + try { + return await this.prisma.phoneNumber.create(phoneNumberCreateArgs); + } catch (e) { + if (e instanceof PrismaClientKnownRequestError) { + if (e.code === "P2002") throw new UniqueConstraintFailedError("Unique constant failed"); + } + throw e; + } + } + + async Delete(phoneNumberDeleteArgs: Prisma.PhoneNumberDeleteArgs): Promise> { + try { + return await this.prisma.phoneNumber.delete(phoneNumberDeleteArgs); + } catch (e) { + if (e instanceof PrismaClientKnownRequestError) { + if (e.code === "P2025") throw new RecordNotFoundError("Record to delete not found."); + } + throw e; + } + } +} diff --git a/apps/user-data/src/prisma/error/record-not-found.error.ts b/apps/user-data/src/prisma/error/record-not-found.error.ts new file mode 100644 index 0000000..e1c9cb1 --- /dev/null +++ b/apps/user-data/src/prisma/error/record-not-found.error.ts @@ -0,0 +1 @@ +export class RecordNotFoundError extends Error {} diff --git a/apps/user-data/src/prisma/error/unique-constraint-failed.error.ts b/apps/user-data/src/prisma/error/unique-constraint-failed.error.ts new file mode 100644 index 0000000..647b582 --- /dev/null +++ b/apps/user-data/src/prisma/error/unique-constraint-failed.error.ts @@ -0,0 +1 @@ +export class UniqueConstraintFailedError extends Error {} diff --git a/apps/user-data/src/prisma/interceptor/prisma-error.interceptor.ts b/apps/user-data/src/prisma/interceptor/prisma-error.interceptor.ts new file mode 100644 index 0000000..7ad4b9a --- /dev/null +++ b/apps/user-data/src/prisma/interceptor/prisma-error.interceptor.ts @@ -0,0 +1,26 @@ +import { + CallHandler, + ConflictException, + ExecutionContext, + Injectable, + NestInterceptor, + NotFoundException +} from "@nestjs/common"; +import { catchError, Observable } from "rxjs"; +import { RecordNotFoundError } from "../error/record-not-found.error"; +import { UniqueConstraintFailedError } from "../error/unique-constraint-failed.error"; + +@Injectable() +export class PrismaErrorInterceptor implements NestInterceptor { + intercept(context: ExecutionContext, next: CallHandler): Observable { + return next + .handle() + .pipe( + catchError(err => { + if (err instanceof RecordNotFoundError) throw new NotFoundException(err.message); + if (err instanceof UniqueConstraintFailedError) throw new ConflictException(err.message); + throw err; + }) + ); + } +} diff --git a/apps/user-data/src/prisma/prisma.module.ts b/apps/user-data/src/prisma/prisma.module.ts new file mode 100644 index 0000000..44400fd --- /dev/null +++ b/apps/user-data/src/prisma/prisma.module.ts @@ -0,0 +1,10 @@ +import { Global, Module } from "@nestjs/common"; +import { PrismaService } from "./service/prisma.service"; + +@Global() +@Module({ + providers: [PrismaService], + exports: [PrismaService] +}) +export class PrismaModule { +} diff --git a/apps/user-data/src/prisma/service/prisma.service.spec.ts b/apps/user-data/src/prisma/service/prisma.service.spec.ts new file mode 100644 index 0000000..a68cb9e --- /dev/null +++ b/apps/user-data/src/prisma/service/prisma.service.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { PrismaService } from './prisma.service'; + +describe('PrismaService', () => { + let service: PrismaService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [PrismaService], + }).compile(); + + service = module.get(PrismaService); + }); + + it('should be defined', () => { + expect(service).toBeDefined(); + }); +}); diff --git a/apps/user-data/src/prisma/service/prisma.service.ts b/apps/user-data/src/prisma/service/prisma.service.ts new file mode 100644 index 0000000..aaa764a --- /dev/null +++ b/apps/user-data/src/prisma/service/prisma.service.ts @@ -0,0 +1,15 @@ +import { INestApplication, Injectable, OnModuleInit } from "@nestjs/common"; +import { PrismaClient } from "@prisma/client"; + +@Injectable() +export class PrismaService extends PrismaClient implements OnModuleInit { + async onModuleInit() { + await this.$connect(); + } + + async enableShutdownHooks(app: INestApplication) { + this.$on("beforeExit", async () => { + await app.close(); + }); + } +} diff --git a/apps/user-data/src/user/service/user.service.spec.ts b/apps/user-data/src/user/service/user.service.spec.ts new file mode 100644 index 0000000..873de8a --- /dev/null +++ b/apps/user-data/src/user/service/user.service.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { UserService } from './user.service'; + +describe('UserService', () => { + let service: UserService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [UserService], + }).compile(); + + service = module.get(UserService); + }); + + it('should be defined', () => { + expect(service).toBeDefined(); + }); +}); diff --git a/apps/user-data/src/user/service/user.service.ts b/apps/user-data/src/user/service/user.service.ts new file mode 100644 index 0000000..413d64a --- /dev/null +++ b/apps/user-data/src/user/service/user.service.ts @@ -0,0 +1,62 @@ +import { Inject, Injectable } from "@nestjs/common"; +import { PrismaService } from "../../prisma/service/prisma.service"; +import { Prisma, User } from "@prisma/client"; +import { PrismaClientKnownRequestError } from "@prisma/client/runtime"; +import { UniqueConstraintFailedError } from "../../prisma/error/unique-constraint-failed.error"; +import { RecordNotFoundError } from "../../prisma/error/record-not-found.error"; + +@Injectable() +export class UserService { + constructor(@Inject(PrismaService) private prisma: PrismaService) { + } + + async FindUnique(userFindUniqueArgs: Prisma.UserFindUniqueArgs): Promise | null> { + return await this.prisma.user.findUnique(userFindUniqueArgs); + } + + async FindFirst(userFindFirstArgs: Prisma.UserFindFirstArgs): Promise | null> { + return await this.prisma.user.findFirst(userFindFirstArgs); + } + + async FindMany(userFindManyArgs: Prisma.UserFindManyArgs): Promise[]> { + return await this.prisma.user.findMany(userFindManyArgs); + } + + async Update(userUpdateArgs: Prisma.UserUpdateArgs): Promise> { + try { + return await this.prisma.user.update(userUpdateArgs); + } catch (e) { + if (e instanceof PrismaClientKnownRequestError) { + if (e.code === "P2025") throw new RecordNotFoundError("Record to update not found."); + if (e.code === "P2002") throw new UniqueConstraintFailedError("Unique constant failed"); + } + throw e; + } + } + + async Count(userCountArgs: Prisma.UserCountArgs): Promise { + return await this.prisma.user.count(userCountArgs); + } + + async Create(userCreateArgs: Prisma.UserCreateArgs): Promise> { + try { + return await this.prisma.user.create(userCreateArgs); + } catch (e) { + if (e instanceof PrismaClientKnownRequestError) { + if (e.code === "P2002") throw new UniqueConstraintFailedError("Unique constant failed"); + } + throw e; + } + } + + async Delete(userDeleteArgs: Prisma.UserDeleteArgs): Promise> { + try { + return await this.prisma.user.delete(userDeleteArgs); + } catch (e) { + if (e instanceof PrismaClientKnownRequestError) { + if (e.code === "P2025") throw new RecordNotFoundError("Record to delete not found."); + } + throw e; + } + } +} diff --git a/apps/user-data/src/user/user.controller.spec.ts b/apps/user-data/src/user/user.controller.spec.ts new file mode 100644 index 0000000..7057a1a --- /dev/null +++ b/apps/user-data/src/user/user.controller.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { UserController } from './user.controller'; + +describe('UserController', () => { + let controller: UserController; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + controllers: [UserController], + }).compile(); + + controller = module.get(UserController); + }); + + it('should be defined', () => { + expect(controller).toBeDefined(); + }); +}); diff --git a/apps/user-data/src/user/user.controller.ts b/apps/user-data/src/user/user.controller.ts new file mode 100644 index 0000000..66dbd28 --- /dev/null +++ b/apps/user-data/src/user/user.controller.ts @@ -0,0 +1,53 @@ +import { Body, Controller, HttpCode, Inject, Post, UseInterceptors } from "@nestjs/common"; +import { Prisma, User } from "@prisma/client"; +import { UserService } from "./service/user.service"; +import { PrismaErrorInterceptor } from "../prisma/interceptor/prisma-error.interceptor"; + +@Controller("user") +@UseInterceptors(PrismaErrorInterceptor) +export class UserController { + constructor(@Inject(UserService) private user: UserService) { + } + + @Post("find/unique") + @HttpCode(200) + async FindUnique(@Body() userFindUniqueArgs: Prisma.UserFindUniqueArgs): Promise | null> { + return await this.user.FindUnique(userFindUniqueArgs); + } + + @Post("find/first") + @HttpCode(200) + async FindFirst(@Body() userFindFirstArgs: Prisma.UserFindFirstArgs): Promise | null> { + return await this.user.FindFirst(userFindFirstArgs); + } + + @Post("find/many") + @HttpCode(200) + async FindMany(@Body() userFindManyArgs: Prisma.UserFindManyArgs): Promise[]> { + return await this.user.FindMany(userFindManyArgs); + } + + @Post("update") + @HttpCode(200) + async Update(@Body() userUpdateArgs: Prisma.UserUpdateArgs): Promise> { + return await this.user.Update(userUpdateArgs); + } + + @Post("count") + @HttpCode(200) + async Count(@Body() userCountArgs: Prisma.UserCountArgs): Promise { + return await this.user.Count(userCountArgs); + } + + @Post("create") + @HttpCode(200) + async Create(@Body() userCreateArgs: Prisma.UserCreateArgs): Promise> { + return await this.user.Create(userCreateArgs); + } + + @Post("delete") + @HttpCode(200) + async Delete(@Body() userDeleteArgs: Prisma.UserDeleteArgs): Promise> { + return await this.user.Delete(userDeleteArgs); + } +} diff --git a/apps/user-data/src/user/user.module.ts b/apps/user-data/src/user/user.module.ts new file mode 100644 index 0000000..250f120 --- /dev/null +++ b/apps/user-data/src/user/user.module.ts @@ -0,0 +1,12 @@ +import { Module } from "@nestjs/common"; +import { UserController } from "./user.controller"; +import { UserService } from "./service/user.service"; +import { PrismaModule } from "../prisma/prisma.module"; + +@Module({ + imports: [PrismaModule], + controllers: [UserController], + providers: [UserService] +}) +export class UserModule { +} diff --git a/apps/user-data/tsconfig.app.json b/apps/user-data/tsconfig.app.json new file mode 100644 index 0000000..a2ce765 --- /dev/null +++ b/apps/user-data/tsconfig.app.json @@ -0,0 +1,12 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "module": "commonjs", + "types": ["node"], + "emitDecoratorMetadata": true, + "target": "es2021" + }, + "exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"], + "include": ["src/**/*.ts"] +} diff --git a/apps/user-data/tsconfig.json b/apps/user-data/tsconfig.json new file mode 100644 index 0000000..8fe9526 --- /dev/null +++ b/apps/user-data/tsconfig.json @@ -0,0 +1,31 @@ +{ + "extends": "../../tsconfig.base.json", + "files": [], + "include": [], + "references": [ + { + "path": "./tsconfig.app.json" + }, + { + "path": "./tsconfig.spec.json" + } + ], + "compilerOptions": { + "esModuleInterop": true + }, + "declaration": true, + "removeComments": true, + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "allowSyntheticDefaultImports": true, + "target": "es2017", + "sourceMap": true, + "incremental": true, + "skipLibCheck": true, + "strict": true, + "strictNullChecks": true, + "noImplicitAny": true, + "strictBindCallApply": true, + "forceConsistentCasingInFileNames": true, + "noFallthroughCasesInSwitch": true +} diff --git a/apps/user-data/tsconfig.spec.json b/apps/user-data/tsconfig.spec.json new file mode 100644 index 0000000..9b2a121 --- /dev/null +++ b/apps/user-data/tsconfig.spec.json @@ -0,0 +1,14 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "module": "commonjs", + "types": ["jest", "node"] + }, + "include": [ + "jest.config.ts", + "src/**/*.test.ts", + "src/**/*.spec.ts", + "src/**/*.d.ts" + ] +} diff --git a/apps/user-data/webpack.config.js b/apps/user-data/webpack.config.js new file mode 100644 index 0000000..81db92b --- /dev/null +++ b/apps/user-data/webpack.config.js @@ -0,0 +1,8 @@ +const { composePlugins, withNx } = require('@nx/webpack'); + +// Nx plugins for webpack. +module.exports = composePlugins(withNx(), (config) => { + // Update the webpack config as needed here. + // e.g. `config.plugins.push(new MyPlugin())` + return config; +}); diff --git a/docker-compose.yml b/docker-compose.yml index e69de29..cc4fc41 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -0,0 +1,47 @@ +version: '3' +services: + zookeeper-log: + image: confluentinc/cp-zookeeper:latest + container_name: zookeeper + ports: + - "2181:2181" + environment: + - ZOOKEEPER_CLIENT_PORT=2181 + - ZOOKEEPER_TICK_TIME=2000 + networks: + - dev-panels + - kafka-log + + kafka-log: + image: confluentinc/cp-kafka:latest + container_name: kafka + ports: + - "9092:9092" + environment: + - KAFKA_BROKER_ID=1 + - KAFKA_LISTENERS=PLAINTEXT://:9092 + - KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://localhost:9092 + - KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=1 + - KAFKA_ZOOKEEPER_CONNECT=zookeeper-log:2181 + depends_on: + - zookeeper-log + networks: + - kafka-log + + kafka-log-manager: + image: hlebalbau/kafka-manager:latest + container_name: kafka-manager + ports: + - "9000:9000" + environment: + - ZK_HOSTS=zookeeper:2181 + depends_on: + - zookeeper-log + networks: + - kafka-log + +networks: + dev-panels: + driver: bridge + kafka-log: + driver: bridge diff --git a/package-lock.json b/package-lock.json index fa8b283..e705bc4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,6 +16,7 @@ "@nestjs/graphql": "^12.0.3", "@nestjs/platform-express": "^10.0.2", "@nestjs/typeorm": "^10.0.0", + "@prisma/client": "^4.16.1", "@swc/helpers": "~0.5.0", "axios": "^1.0.0", "graphql": "^16.7.1", @@ -66,6 +67,7 @@ "nx": "16.4.0", "nx-cloud": "latest", "prettier": "^2.6.2", + "prisma": "^4.16.1", "react-refresh": "^0.10.0", "ts-jest": "^29.1.0", "ts-node": "10.9.1", @@ -4469,6 +4471,38 @@ "node": ">= 8" } }, + "node_modules/@prisma/client": { + "version": "4.16.1", + "resolved": "https://registry.npmjs.org/@prisma/client/-/client-4.16.1.tgz", + "integrity": "sha512-CoDHu7Bt+NuDo40ijoeHP79EHtECsPBTy3yte5Yo3op8TqXt/kV0OT5OrsWewKvQGKFMHhYQ+ePed3zzjYdGAw==", + "hasInstallScript": true, + "dependencies": { + "@prisma/engines-version": "4.16.0-66.b20ead4d3ab9e78ac112966e242ded703f4a052c" + }, + "engines": { + "node": ">=14.17" + }, + "peerDependencies": { + "prisma": "*" + }, + "peerDependenciesMeta": { + "prisma": { + "optional": true + } + } + }, + "node_modules/@prisma/engines": { + "version": "4.16.1", + "resolved": "https://registry.npmjs.org/@prisma/engines/-/engines-4.16.1.tgz", + "integrity": "sha512-gpZG0kGGxfemgvK/LghHdBIz+crHkZjzszja94xp4oytpsXrgt/Ice82MvPsWMleVIniKuARrowtsIsim0PFJQ==", + "devOptional": true, + "hasInstallScript": true + }, + "node_modules/@prisma/engines-version": { + "version": "4.16.0-66.b20ead4d3ab9e78ac112966e242ded703f4a052c", + "resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-4.16.0-66.b20ead4d3ab9e78ac112966e242ded703f4a052c.tgz", + "integrity": "sha512-tMWAF/qF00fbUH1HB4Yjmz6bjh7fzkb7Y3NRoUfMlHu6V+O45MGvqwYxqwBjn1BIUXkl3r04W351D4qdJjrgvA==" + }, "node_modules/@protobufjs/aspromise": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/@protobufjs/aspromise/-/aspromise-1.1.2.tgz", @@ -15387,6 +15421,23 @@ "url": "https://github.com/chalk/ansi-styles?sponsor=1" } }, + "node_modules/prisma": { + "version": "4.16.1", + "resolved": "https://registry.npmjs.org/prisma/-/prisma-4.16.1.tgz", + "integrity": "sha512-C2Xm7yxHxjFjjscBEW4tmoraPHH/Vyu/A0XABdbaFtoiOZARsxvOM7rwc2iZ0qVxbh0bGBGBWZUSXO/52/nHBQ==", + "devOptional": true, + "hasInstallScript": true, + "dependencies": { + "@prisma/engines": "4.16.1" + }, + "bin": { + "prisma": "build/index.js", + "prisma2": "build/index.js" + }, + "engines": { + "node": ">=14.17" + } + }, "node_modules/process-nextick-args": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", diff --git a/package.json b/package.json index 44ff2ca..176c8cb 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "@nestjs/graphql": "^12.0.3", "@nestjs/platform-express": "^10.0.2", "@nestjs/typeorm": "^10.0.0", + "@prisma/client": "^4.16.1", "@swc/helpers": "~0.5.0", "axios": "^1.0.0", "graphql": "^16.7.1", @@ -62,6 +63,7 @@ "nx": "16.4.0", "nx-cloud": "latest", "prettier": "^2.6.2", + "prisma": "^4.16.1", "react-refresh": "^0.10.0", "ts-jest": "^29.1.0", "ts-node": "10.9.1",