Compare commits
11 Commits
main
...
feature/ca
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e3e9f1cf0d | ||
|
|
17528d12c7 | ||
|
|
7fcf55abda | ||
|
|
b43d166b2e | ||
|
|
0039de3499 | ||
|
|
eef1338cd2 | ||
|
|
50ef49d01f | ||
|
|
b8b8ca6004 | ||
|
|
9d9ba6540b | ||
|
|
54f69f7c36 | ||
|
|
8cb283d4ba |
13
.github/workflows/deploy.yml
vendored
13
.github/workflows/deploy.yml
vendored
@ -1,4 +1,4 @@
|
||||
name: Build & Deploy Web & Bot
|
||||
name: Build & Deploy Web, Bot & Cache Proxy
|
||||
|
||||
on:
|
||||
push:
|
||||
@ -12,6 +12,7 @@ jobs:
|
||||
outputs:
|
||||
web_tag: ${{ steps.vars.outputs.web_tag }}
|
||||
bot_tag: ${{ steps.vars.outputs.bot_tag }}
|
||||
cache_proxy_tag: ${{ steps.vars.outputs.cache_proxy_tag }}
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v3
|
||||
@ -33,6 +34,7 @@ jobs:
|
||||
run: |
|
||||
echo "web_tag=web-${GITHUB_SHA::7}" >> $GITHUB_OUTPUT
|
||||
echo "bot_tag=bot-${GITHUB_SHA::7}" >> $GITHUB_OUTPUT
|
||||
echo "cache_proxy_tag=cache-proxy-${GITHUB_SHA::7}" >> $GITHUB_OUTPUT
|
||||
|
||||
- name: Login to Docker Hub
|
||||
run: echo "${{ secrets.DOCKERHUB_TOKEN }}" | docker login -u "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin
|
||||
@ -53,6 +55,14 @@ jobs:
|
||||
run: |
|
||||
docker push ${{ secrets.DOCKERHUB_USERNAME }}/zapishis-bot:${{ steps.vars.outputs.bot_tag }}
|
||||
|
||||
- name: Build cache-proxy image
|
||||
run: |
|
||||
docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/zapishis-cache-proxy:${{ steps.vars.outputs.cache_proxy_tag }} -f ./apps/cache-proxy/Dockerfile .
|
||||
|
||||
- name: Push cache-proxy image to Docker Hub
|
||||
run: |
|
||||
docker push ${{ secrets.DOCKERHUB_USERNAME }}/zapishis-cache-proxy:${{ steps.vars.outputs.cache_proxy_tag }}
|
||||
|
||||
deploy:
|
||||
name: Deploy to VPS
|
||||
needs: build-and-push
|
||||
@ -84,6 +94,7 @@ jobs:
|
||||
echo "BOT_URL=${{ secrets.BOT_URL }}" >> .env
|
||||
echo "WEB_IMAGE_TAG=${{ needs.build-and-push.outputs.web_tag }}" >> .env
|
||||
echo "BOT_IMAGE_TAG=${{ needs.build-and-push.outputs.bot_tag }}" >> .env
|
||||
echo "CACHE_PROXY_IMAGE_TAG=${{ needs.build-and-push.outputs.cache_proxy_tag }}" >> .env
|
||||
echo "DOCKERHUB_USERNAME=${{ secrets.DOCKERHUB_USERNAME }}" >> .env
|
||||
echo "REDIS_PASSWORD=${{ secrets.REDIS_PASSWORD }}" >> .env
|
||||
echo "BOT_PROVIDER_TOKEN=${{ secrets.BOT_PROVIDER_TOKEN }}" >> .env
|
||||
|
||||
7
apps/cache-proxy/.dockerignore
Normal file
7
apps/cache-proxy/.dockerignore
Normal file
@ -0,0 +1,7 @@
|
||||
.git
|
||||
Dockerfile
|
||||
.dockerignore
|
||||
node_modules
|
||||
*.log
|
||||
dist
|
||||
README.md
|
||||
13
apps/cache-proxy/.eslintrc.js
Normal file
13
apps/cache-proxy/.eslintrc.js
Normal file
@ -0,0 +1,13 @@
|
||||
import { typescript } from '@repo/eslint-config/typescript';
|
||||
|
||||
/** @type {import("eslint").Linter.Config} */
|
||||
export default [
|
||||
...typescript,
|
||||
{
|
||||
ignores: ['**/types/**', '*.config.*', '*.config.js', '.eslintrc.js'],
|
||||
rules: {
|
||||
'import/no-duplicates': 'off',
|
||||
'import/consistent-type-specifier-style': 'off',
|
||||
},
|
||||
},
|
||||
];
|
||||
56
apps/cache-proxy/.gitignore
vendored
Normal file
56
apps/cache-proxy/.gitignore
vendored
Normal file
@ -0,0 +1,56 @@
|
||||
# compiled output
|
||||
/dist
|
||||
/node_modules
|
||||
/build
|
||||
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
pnpm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
lerna-debug.log*
|
||||
|
||||
# OS
|
||||
.DS_Store
|
||||
|
||||
# Tests
|
||||
/coverage
|
||||
/.nyc_output
|
||||
|
||||
# IDEs and editors
|
||||
/.idea
|
||||
.project
|
||||
.classpath
|
||||
.c9/
|
||||
*.launch
|
||||
.settings/
|
||||
*.sublime-workspace
|
||||
|
||||
# IDE - VSCode
|
||||
.vscode/*
|
||||
!.vscode/settings.json
|
||||
!.vscode/tasks.json
|
||||
!.vscode/launch.json
|
||||
!.vscode/extensions.json
|
||||
|
||||
# dotenv environment variable files
|
||||
.env
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
.env.local
|
||||
|
||||
# temp directory
|
||||
.temp
|
||||
.tmp
|
||||
|
||||
# Runtime data
|
||||
pids
|
||||
*.pid
|
||||
*.seed
|
||||
*.pid.lock
|
||||
|
||||
# Diagnostic reports (https://nodejs.org/api/report.html)
|
||||
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
||||
51
apps/cache-proxy/Dockerfile
Normal file
51
apps/cache-proxy/Dockerfile
Normal file
@ -0,0 +1,51 @@
|
||||
ARG NODE_VERSION=22
|
||||
ARG PROJECT=cache-proxy
|
||||
|
||||
# Alpine image
|
||||
FROM node:${NODE_VERSION}-alpine AS alpine
|
||||
RUN apk update
|
||||
RUN apk add --no-cache libc6-compat
|
||||
|
||||
FROM alpine as base
|
||||
ENV PNPM_HOME="/pnpm"
|
||||
ENV PATH="$PNPM_HOME:$PATH"
|
||||
RUN apk add --no-cache libc6-compat && \
|
||||
corepack enable && \
|
||||
pnpm install turbo@2.3.2 dotenv-cli --global
|
||||
|
||||
FROM base AS pruner
|
||||
ARG PROJECT
|
||||
WORKDIR /app
|
||||
COPY . .
|
||||
RUN turbo prune --scope=${PROJECT} --docker
|
||||
|
||||
FROM base AS builder
|
||||
WORKDIR /app
|
||||
|
||||
COPY --from=pruner /app/out/json/ .
|
||||
COPY --from=pruner /app/out/pnpm-lock.yaml ./pnpm-lock.yaml
|
||||
COPY --from=pruner /app/out/pnpm-workspace.yaml ./pnpm-workspace.yaml
|
||||
|
||||
RUN --mount=type=cache,id=pnpm,target=~/.pnpm-store pnpm install --prod --frozen-lockfile
|
||||
|
||||
COPY --from=pruner /app/out/full/ .
|
||||
|
||||
COPY turbo.json turbo.json
|
||||
COPY .env .env
|
||||
|
||||
RUN dotenv -e .env turbo run build --filter=${PROJECT}...
|
||||
RUN --mount=type=cache,id=pnpm,target=~/.pnpm-store pnpm prune --prod --no-optional
|
||||
RUN rm -rf ./**/*/src
|
||||
|
||||
FROM alpine AS runner
|
||||
ARG PROJECT
|
||||
|
||||
RUN addgroup --system --gid 1001 nodejs
|
||||
RUN adduser --system --uid 1001 appuser
|
||||
USER appuser
|
||||
|
||||
WORKDIR /app
|
||||
COPY --from=builder --chown=nodejs:nodejs /app .
|
||||
WORKDIR /app/apps/${PROJECT}
|
||||
|
||||
CMD ["node", "dist/main.js"]
|
||||
73
apps/cache-proxy/README.md
Normal file
73
apps/cache-proxy/README.md
Normal file
@ -0,0 +1,73 @@
|
||||
<p align="center">
|
||||
<a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo-small.svg" width="200" alt="Nest Logo" /></a>
|
||||
</p>
|
||||
|
||||
[circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456
|
||||
[circleci-url]: https://circleci.com/gh/nestjs/nest
|
||||
|
||||
<p align="center">A progressive <a href="http://nodejs.org" target="_blank">Node.js</a> framework for building efficient and scalable server-side applications.</p>
|
||||
<p align="center">
|
||||
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/v/@nestjs/core.svg" alt="NPM Version" /></a>
|
||||
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/l/@nestjs/core.svg" alt="Package License" /></a>
|
||||
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/dm/@nestjs/common.svg" alt="NPM Downloads" /></a>
|
||||
<a href="https://circleci.com/gh/nestjs/nest" target="_blank"><img src="https://img.shields.io/circleci/build/github/nestjs/nest/master" alt="CircleCI" /></a>
|
||||
<a href="https://coveralls.io/github/nestjs/nest?branch=master" target="_blank"><img src="https://coveralls.io/repos/github/nestjs/nest/badge.svg?branch=master#9" alt="Coverage" /></a>
|
||||
<a href="https://discord.gg/G7Qnnhy" target="_blank"><img src="https://img.shields.io/badge/discord-online-brightgreen.svg" alt="Discord"/></a>
|
||||
<a href="https://opencollective.com/nest#backer" target="_blank"><img src="https://opencollective.com/nest/backers/badge.svg" alt="Backers on Open Collective" /></a>
|
||||
<a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://opencollective.com/nest/sponsors/badge.svg" alt="Sponsors on Open Collective" /></a>
|
||||
<a href="https://paypal.me/kamilmysliwiec" target="_blank"><img src="https://img.shields.io/badge/Donate-PayPal-ff3f59.svg"/></a>
|
||||
<a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://img.shields.io/badge/Support%20us-Open%20Collective-41B883.svg" alt="Support us"></a>
|
||||
<a href="https://twitter.com/nestframework" target="_blank"><img src="https://img.shields.io/twitter/follow/nestframework.svg?style=social&label=Follow"></a>
|
||||
</p>
|
||||
<!--[](https://opencollective.com/nest#backer)
|
||||
[](https://opencollective.com/nest#sponsor)-->
|
||||
|
||||
## Description
|
||||
|
||||
[Nest](https://github.com/nestjs/nest) framework TypeScript starter repository.
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
$ pnpm install
|
||||
```
|
||||
|
||||
## Running the app
|
||||
|
||||
```bash
|
||||
# development
|
||||
$ pnpm run start
|
||||
|
||||
# watch mode
|
||||
$ pnpm run start:dev
|
||||
|
||||
# production mode
|
||||
$ pnpm run start:prod
|
||||
```
|
||||
|
||||
## Test
|
||||
|
||||
```bash
|
||||
# unit tests
|
||||
$ pnpm run test
|
||||
|
||||
# e2e tests
|
||||
$ pnpm run test:e2e
|
||||
|
||||
# test coverage
|
||||
$ pnpm run test:cov
|
||||
```
|
||||
|
||||
## Support
|
||||
|
||||
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support).
|
||||
|
||||
## Stay in touch
|
||||
|
||||
- Author - [Kamil Myśliwiec](https://kamilmysliwiec.com)
|
||||
- Website - [https://nestjs.com](https://nestjs.com/)
|
||||
- Twitter - [@nestframework](https://twitter.com/nestframework)
|
||||
|
||||
## License
|
||||
|
||||
Nest is [MIT licensed](LICENSE).
|
||||
9
apps/cache-proxy/nest-cli.json
Normal file
9
apps/cache-proxy/nest-cli.json
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/nest-cli",
|
||||
"collection": "@nestjs/schematics",
|
||||
"sourceRoot": "src",
|
||||
"monorepo": true,
|
||||
"compilerOptions": {
|
||||
"deleteOutDir": true
|
||||
}
|
||||
}
|
||||
75
apps/cache-proxy/package.json
Normal file
75
apps/cache-proxy/package.json
Normal file
@ -0,0 +1,75 @@
|
||||
{
|
||||
"name": "cache-proxy",
|
||||
"version": "0.0.1",
|
||||
"description": "",
|
||||
"author": "",
|
||||
"private": true,
|
||||
"license": "UNLICENSED",
|
||||
"scripts": {
|
||||
"build": "nest build",
|
||||
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
|
||||
"start": "nest start",
|
||||
"dev": "dotenv -e ../../.env.local nest start -- --watch",
|
||||
"start:debug": "nest start --debug --watch",
|
||||
"start:prod": "node dist/main",
|
||||
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
|
||||
"test": "jest",
|
||||
"test:watch": "jest --watch",
|
||||
"test:cov": "jest --coverage",
|
||||
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
|
||||
"test:e2e": "jest --config ./test/jest-e2e.json"
|
||||
},
|
||||
"dependencies": {
|
||||
"@nestjs/cli": "^10.0.0",
|
||||
"@nestjs/schematics": "^10.0.0",
|
||||
"@nestjs/cache-manager": "^2.2.1",
|
||||
"@nestjs/common": "^10.0.0",
|
||||
"@nestjs/config": "^3.2.0",
|
||||
"@nestjs/core": "^10.0.0",
|
||||
"@nestjs/platform-express": "^10.0.0",
|
||||
"@nestjs/platform-fastify": "^10.3.3",
|
||||
"@types/node": "catalog:",
|
||||
"fastify": "^4.26.1",
|
||||
"dotenv-cli": "catalog:",
|
||||
"cache-manager": "^5.4.0",
|
||||
"cache-manager-ioredis": "^2.1.0",
|
||||
"ioredis": "^5.3.2",
|
||||
"reflect-metadata": "^0.2.0",
|
||||
"rxjs": "^7.8.1",
|
||||
"source-map-support": "^0.5.21",
|
||||
"tsconfig-paths": "^4.2.0",
|
||||
"typescript": "catalog:",
|
||||
"zod": "catalog:"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@nestjs/testing": "^10.0.0",
|
||||
"@repo/eslint-config": "workspace:*",
|
||||
"@repo/typescript-config": "workspace:*",
|
||||
"@types/jest": "^29.5.2",
|
||||
"@types/supertest": "^6.0.0",
|
||||
"eslint": "catalog:",
|
||||
"jest": "^29.5.0",
|
||||
"prettier": "catalog:",
|
||||
"supertest": "^6.3.3",
|
||||
"ts-jest": "29.1.1",
|
||||
"ts-loader": "^9.4.3",
|
||||
"ts-node": "^10.9.1"
|
||||
},
|
||||
"jest": {
|
||||
"moduleFileExtensions": [
|
||||
"js",
|
||||
"json",
|
||||
"ts"
|
||||
],
|
||||
"rootDir": "src",
|
||||
"testRegex": ".*\\.spec\\.ts$",
|
||||
"transform": {
|
||||
"^.+\\.(t|j)s$": "ts-jest"
|
||||
},
|
||||
"collectCoverageFrom": [
|
||||
"**/*.(t|j)s"
|
||||
],
|
||||
"coverageDirectory": "../coverage",
|
||||
"testEnvironment": "node"
|
||||
}
|
||||
}
|
||||
18
apps/cache-proxy/src/app.module.ts
Normal file
18
apps/cache-proxy/src/app.module.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { ProxyModule } from './proxy/proxy.module';
|
||||
import { HealthController } from './health/health.controller';
|
||||
import { Global, Module } from '@nestjs/common';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule.forRoot({
|
||||
isGlobal: true,
|
||||
}),
|
||||
ProxyModule,
|
||||
],
|
||||
controllers: [HealthController],
|
||||
providers: [],
|
||||
})
|
||||
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
|
||||
export class AppModule {}
|
||||
3
apps/cache-proxy/src/config/constants.ts
Normal file
3
apps/cache-proxy/src/config/constants.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import { seconds } from 'src/utils/time';
|
||||
|
||||
export const DEFAULT_CACHE_TTL = seconds().fromMinutes(5);
|
||||
3
apps/cache-proxy/src/config/env.ts
Normal file
3
apps/cache-proxy/src/config/env.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import envSchema from './schema/env';
|
||||
|
||||
export const env = envSchema.parse(process.env);
|
||||
22
apps/cache-proxy/src/config/schema/env.ts
Normal file
22
apps/cache-proxy/src/config/schema/env.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { DEFAULT_CACHE_TTL } from '../constants';
|
||||
import { z } from 'zod';
|
||||
|
||||
const envSchema = z.object({
|
||||
CACHE_TTL: z
|
||||
.string()
|
||||
.transform((val) => Number.parseInt(val, 10))
|
||||
.default(DEFAULT_CACHE_TTL.toString()),
|
||||
PORT: z
|
||||
.string()
|
||||
.transform((val) => Number.parseInt(val, 10))
|
||||
.default('5000'),
|
||||
REDIS_HOST: z.string(),
|
||||
REDIS_PORT: z
|
||||
.string()
|
||||
.transform((val) => Number.parseInt(val, 10))
|
||||
.default('6379'),
|
||||
REDIS_PASSWORD: z.string(),
|
||||
URL_GRAPHQL: z.string(),
|
||||
});
|
||||
|
||||
export default envSchema;
|
||||
11
apps/cache-proxy/src/health/health.controller.ts
Normal file
11
apps/cache-proxy/src/health/health.controller.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
|
||||
@Controller('api')
|
||||
export class HealthController {
|
||||
@Get('health')
|
||||
public health() {
|
||||
return { status: 'ok' };
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
15
apps/cache-proxy/src/main.ts
Normal file
15
apps/cache-proxy/src/main.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import { AppModule } from './app.module';
|
||||
import { env } from './config/env';
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import type { NestFastifyApplication } from '@nestjs/platform-fastify';
|
||||
import { FastifyAdapter } from '@nestjs/platform-fastify';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create<NestFastifyApplication>(
|
||||
AppModule,
|
||||
new FastifyAdapter(),
|
||||
);
|
||||
|
||||
await app.listen(env.PORT, '0.0.0.0');
|
||||
}
|
||||
bootstrap();
|
||||
13
apps/cache-proxy/src/proxy/lib/config.ts
Normal file
13
apps/cache-proxy/src/proxy/lib/config.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { seconds } from 'src/utils/time';
|
||||
|
||||
export const queryTTL: Record<string, number | false> = {
|
||||
Login: false,
|
||||
GetCustomer: seconds().fromHours(24),
|
||||
GetOrder: seconds().fromHours(24),
|
||||
GetService: seconds().fromHours(24),
|
||||
GetSlot: seconds().fromHours(24),
|
||||
GetSlotsOrders: false,
|
||||
GetSubscriptionPrices: seconds().fromHours(24),
|
||||
GetSubscriptions: seconds().fromHours(24),
|
||||
GetSubscriptionSettings: seconds().fromHours(1),
|
||||
};
|
||||
138
apps/cache-proxy/src/proxy/proxy.controller.ts
Normal file
138
apps/cache-proxy/src/proxy/proxy.controller.ts
Normal file
@ -0,0 +1,138 @@
|
||||
import type { GQLRequest, GQLResponse } from './types';
|
||||
import { CACHE_MANAGER } from '@nestjs/cache-manager';
|
||||
import {
|
||||
All,
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
Inject,
|
||||
Query,
|
||||
Req,
|
||||
Res,
|
||||
} from '@nestjs/common';
|
||||
import type { Cache } from 'cache-manager';
|
||||
import { FastifyReply, FastifyRequest } from 'fastify';
|
||||
import { env } from 'src/config/env';
|
||||
import { queryTTL } from './lib/config';
|
||||
import { extractDocumentId, getQueryType } from 'src/utils/query';
|
||||
|
||||
type RedisStore = Omit<Cache, 'set'> & {
|
||||
set: (key: string, value: unknown, { ttl }: { ttl: number }) => Promise<void>;
|
||||
};
|
||||
|
||||
@Controller('api')
|
||||
export class ProxyController {
|
||||
constructor(@Inject(CACHE_MANAGER) private readonly cacheManager: RedisStore) {}
|
||||
|
||||
@All('/graphql')
|
||||
public async graphql(@Req() req: FastifyRequest, @Res() reply: FastifyReply) {
|
||||
const { operationName, query, variables } = req.body as GQLRequest;
|
||||
|
||||
const queryType = getQueryType(query);
|
||||
|
||||
const key = `${operationName} ${JSON.stringify(variables)}`;
|
||||
|
||||
if (queryType.action === 'query') {
|
||||
const cached = await this.cacheManager.get(key);
|
||||
if (cached) return reply.send(cached);
|
||||
}
|
||||
|
||||
const response = await fetch(env.URL_GRAPHQL, {
|
||||
body: JSON.stringify({ operationName, query, variables }),
|
||||
headers: {
|
||||
Authorization: req.headers.authorization,
|
||||
'Content-Type': 'application/json',
|
||||
Cookie: req.headers.cookie,
|
||||
},
|
||||
method: req.method,
|
||||
});
|
||||
|
||||
const data = (await response.json()) as GQLResponse;
|
||||
|
||||
if (!response.ok || data?.error || data?.errors?.length)
|
||||
throw new HttpException(
|
||||
response.statusText,
|
||||
response.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
|
||||
if (queryType.action === 'mutation' && queryType.entity) {
|
||||
const documentId = extractDocumentId(data);
|
||||
const keys = await this.cacheManager.store.keys(`*${queryType.entity}*`);
|
||||
for (const key of keys) {
|
||||
if (key.includes(documentId)) {
|
||||
await this.cacheManager.del(key);
|
||||
|
||||
// console.log(`🗑 Cache invalidated (by key): ${key}`);
|
||||
continue;
|
||||
}
|
||||
|
||||
const value = await this.cacheManager.get(key);
|
||||
const serialized = typeof value === 'string' ? value : JSON.stringify(value);
|
||||
if (serialized?.includes(documentId)) {
|
||||
await this.cacheManager.del(key);
|
||||
|
||||
// console.log(`🗑 Cache invalidated (by value): ${key}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const ttl = queryTTL[operationName];
|
||||
if (queryType.action === 'query' && data && ttl !== false)
|
||||
await this.cacheManager.set(key, data, { ttl: ttl || env.CACHE_TTL });
|
||||
|
||||
return reply.send(data);
|
||||
}
|
||||
|
||||
@Get('/get-queries')
|
||||
public async getQueriesList(@Res() reply: FastifyReply) {
|
||||
const keys: string[] = await this.cacheManager.store.keys('*');
|
||||
|
||||
const entries = await Promise.all(
|
||||
keys.map(async (key) => {
|
||||
try {
|
||||
const value = await this.cacheManager.get(key);
|
||||
return { key, value };
|
||||
} catch (e) {
|
||||
return { key, error: e.message };
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
return reply.send(entries);
|
||||
}
|
||||
|
||||
@Delete('/delete-query')
|
||||
public async deleteQuery(@Query('queryKey') queryKey: string, @Res() reply: FastifyReply) {
|
||||
try {
|
||||
await this.cacheManager.del(queryKey);
|
||||
|
||||
return reply.send('ok');
|
||||
} catch (error) {
|
||||
throw new HttpException(error, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
@Delete('/reset')
|
||||
public async reset(@Res() reply: FastifyReply) {
|
||||
try {
|
||||
await this.cacheManager.reset();
|
||||
|
||||
return reply.send('ok');
|
||||
} catch (error) {
|
||||
throw new HttpException(error, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
@Get('/get-query')
|
||||
public async getQueryValue(@Query('queryKey') queryKey: string, @Res() reply: FastifyReply) {
|
||||
try {
|
||||
const value = await this.cacheManager.get(queryKey);
|
||||
|
||||
return reply.send(value);
|
||||
} catch (error) {
|
||||
throw new HttpException(error, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
||||
22
apps/cache-proxy/src/proxy/proxy.module.ts
Normal file
22
apps/cache-proxy/src/proxy/proxy.module.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { ProxyController } from './proxy.controller';
|
||||
import { CacheModule } from '@nestjs/cache-manager';
|
||||
import { Module } from '@nestjs/common';
|
||||
import * as redisStore from 'cache-manager-ioredis';
|
||||
import type { RedisOptions } from 'ioredis';
|
||||
import { env } from 'src/config/env';
|
||||
|
||||
@Module({
|
||||
controllers: [ProxyController],
|
||||
imports: [
|
||||
CacheModule.register<RedisOptions>({
|
||||
host: env.REDIS_HOST,
|
||||
port: env.REDIS_PORT,
|
||||
store: redisStore,
|
||||
ttl: env.CACHE_TTL,
|
||||
password: env.REDIS_PASSWORD,
|
||||
db: 1,
|
||||
}),
|
||||
],
|
||||
})
|
||||
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
|
||||
export class ProxyModule {}
|
||||
16
apps/cache-proxy/src/proxy/types.ts
Normal file
16
apps/cache-proxy/src/proxy/types.ts
Normal file
@ -0,0 +1,16 @@
|
||||
export type GQLRequest = {
|
||||
operationName: string;
|
||||
query: string;
|
||||
variables: string;
|
||||
};
|
||||
|
||||
export type GQLResponse = {
|
||||
data: unknown;
|
||||
error?: unknown;
|
||||
errors?: unknown[];
|
||||
};
|
||||
|
||||
export type QueryItem = {
|
||||
queries: string[];
|
||||
ttl: number | false;
|
||||
};
|
||||
22
apps/cache-proxy/src/utils/query.ts
Normal file
22
apps/cache-proxy/src/utils/query.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { GQLResponse } from 'src/proxy/types';
|
||||
|
||||
export function getQueryType(query: string) {
|
||||
const actionMatch = query.match(/\b(query|mutation)\b/u);
|
||||
const action = actionMatch ? (actionMatch[1] as 'query' | 'mutation') : null;
|
||||
|
||||
const entityMatch = query.match(
|
||||
/\b(mutation|query)\s+\w*([A-Z][A-Za-z0-9_]+)/u,
|
||||
);
|
||||
const entity = entityMatch ? entityMatch[2] : null;
|
||||
|
||||
return { action, entity };
|
||||
}
|
||||
|
||||
export function extractDocumentId(data: GQLResponse) {
|
||||
if (!data?.data) return null;
|
||||
|
||||
const firstKey = Object.keys(data.data)[0];
|
||||
if (!firstKey) return null;
|
||||
|
||||
return data.data[firstKey]?.documentId || null;
|
||||
}
|
||||
13
apps/cache-proxy/src/utils/time.ts
Normal file
13
apps/cache-proxy/src/utils/time.ts
Normal file
@ -0,0 +1,13 @@
|
||||
export function seconds() {
|
||||
return {
|
||||
fromDays(days: number) {
|
||||
return days * 24 * 60 * 60;
|
||||
},
|
||||
fromHours(hours: number) {
|
||||
return hours * 60 * 60;
|
||||
},
|
||||
fromMinutes(minutes: number) {
|
||||
return minutes * 60;
|
||||
},
|
||||
};
|
||||
}
|
||||
4
apps/cache-proxy/tsconfig.build.json
Normal file
4
apps/cache-proxy/tsconfig.build.json
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
|
||||
}
|
||||
23
apps/cache-proxy/tsconfig.json
Normal file
23
apps/cache-proxy/tsconfig.json
Normal file
@ -0,0 +1,23 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"declaration": true,
|
||||
"removeComments": true,
|
||||
"emitDecoratorMetadata": true,
|
||||
"experimentalDecorators": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"target": "ES2022",
|
||||
"sourceMap": true,
|
||||
"outDir": "./dist",
|
||||
"baseUrl": "./",
|
||||
"incremental": true,
|
||||
"skipLibCheck": true,
|
||||
"strictNullChecks": false,
|
||||
"noImplicitAny": false,
|
||||
"strictBindCallApply": false,
|
||||
"forceConsistentCasingInFileNames": false,
|
||||
"noFallthroughCasesInSwitch": false
|
||||
},
|
||||
"include": ["."],
|
||||
"exclude": ["dist", "build", "node_modules"]
|
||||
}
|
||||
@ -1,25 +1,27 @@
|
||||
'use client';
|
||||
|
||||
import { useCustomerMutation } from '@/hooks/api/customers';
|
||||
import { useClientOnce } from '@/hooks/telegram';
|
||||
import { initData, useSignal } from '@telegram-apps/sdk-react';
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
export function UpdateProfile() {
|
||||
const initDataUser = useSignal(initData.user);
|
||||
const { mutate: updateProfile } = useCustomerMutation();
|
||||
const [hasUpdated, setHasUpdated] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (!hasUpdated) {
|
||||
useClientOnce(() => {
|
||||
if (
|
||||
localStorage.getItem('firstLogin') === null ||
|
||||
localStorage.getItem('firstLogin') === 'true'
|
||||
) {
|
||||
updateProfile({
|
||||
data: {
|
||||
active: true,
|
||||
photoUrl: initDataUser?.photoUrl || undefined,
|
||||
},
|
||||
});
|
||||
setHasUpdated(true);
|
||||
localStorage.setItem('firstLogin', 'false');
|
||||
}
|
||||
}, [hasUpdated, initDataUser?.photoUrl, updateProfile]);
|
||||
});
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -1,4 +1,13 @@
|
||||
services:
|
||||
cache-proxy:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: ./apps/cache-proxy/Dockerfile
|
||||
env_file:
|
||||
- .env
|
||||
depends_on:
|
||||
- redis
|
||||
restart: always
|
||||
web:
|
||||
env_file:
|
||||
- .env
|
||||
@ -6,6 +15,8 @@ services:
|
||||
context: .
|
||||
dockerfile: ./apps/web/Dockerfile
|
||||
restart: always
|
||||
depends_on:
|
||||
- cache-proxy
|
||||
ports:
|
||||
- 3000:3000
|
||||
bot:
|
||||
@ -16,6 +27,7 @@ services:
|
||||
- .env
|
||||
depends_on:
|
||||
- redis
|
||||
- cache-proxy
|
||||
restart: always
|
||||
|
||||
redis:
|
||||
|
||||
@ -1,4 +1,19 @@
|
||||
services:
|
||||
cache-proxy:
|
||||
image: ${DOCKERHUB_USERNAME}/zapishis-cache-proxy:${CACHE_PROXY_IMAGE_TAG}
|
||||
env_file:
|
||||
- .env
|
||||
restart: always
|
||||
depends_on:
|
||||
- redis
|
||||
networks:
|
||||
- app
|
||||
- web
|
||||
healthcheck:
|
||||
test: ['CMD', 'wget', '-qO-', 'http://localhost:5000/api/health']
|
||||
interval: 10s
|
||||
timeout: 3s
|
||||
retries: 5
|
||||
web:
|
||||
image: ${DOCKERHUB_USERNAME}/zapishis-web:${WEB_IMAGE_TAG}
|
||||
env_file:
|
||||
@ -9,6 +24,8 @@ services:
|
||||
interval: 10s
|
||||
timeout: 3s
|
||||
retries: 5
|
||||
depends_on:
|
||||
- cache-proxy
|
||||
networks:
|
||||
- app
|
||||
- web
|
||||
@ -20,6 +37,7 @@ services:
|
||||
- .env
|
||||
depends_on:
|
||||
- redis
|
||||
- cache-proxy
|
||||
networks:
|
||||
- app
|
||||
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
import { getClientWithToken } from '../apollo/client';
|
||||
import { ERRORS as SHARED_ERRORS } from '../constants/errors';
|
||||
import * as GQL from '../types';
|
||||
import { type ApolloClient, type NormalizedCacheObject } from '@apollo/client';
|
||||
import { isCustomerBanned } from '@repo/utils/customer';
|
||||
|
||||
export const ERRORS = {
|
||||
@ -16,16 +17,20 @@ type UserProfile = {
|
||||
export class BaseService {
|
||||
protected _user: UserProfile;
|
||||
|
||||
protected graphQL: ApolloClient<NormalizedCacheObject> | null;
|
||||
|
||||
constructor(user: UserProfile) {
|
||||
if (!user?.telegramId) {
|
||||
throw new Error(ERRORS.MISSING_TELEGRAM_ID);
|
||||
}
|
||||
|
||||
this._user = user;
|
||||
|
||||
this.graphQL = null;
|
||||
}
|
||||
|
||||
protected async _getUser() {
|
||||
const { query } = await getClientWithToken();
|
||||
const { query } = await this.getGraphQLClient();
|
||||
|
||||
const result = await query({
|
||||
query: GQL.GetCustomerDocument,
|
||||
@ -44,7 +49,7 @@ export class BaseService {
|
||||
}
|
||||
|
||||
protected async checkIsBanned() {
|
||||
const { query } = await getClientWithToken();
|
||||
const { query } = await this.getGraphQLClient();
|
||||
|
||||
const result = await query({
|
||||
query: GQL.GetCustomerDocument,
|
||||
@ -61,4 +66,10 @@ export class BaseService {
|
||||
|
||||
return { customer };
|
||||
}
|
||||
|
||||
protected async getGraphQLClient() {
|
||||
if (!this.graphQL) this.graphQL = await getClientWithToken();
|
||||
|
||||
return this.graphQL;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
import { getClientWithToken } from '../apollo/client';
|
||||
import { ERRORS } from '../constants/errors';
|
||||
import * as GQL from '../types';
|
||||
import { BaseService } from './base';
|
||||
@ -17,7 +16,7 @@ export class CustomersService extends BaseService {
|
||||
throw new Error(ERRORS.NO_PERMISSION);
|
||||
}
|
||||
|
||||
const { mutate, query } = await getClientWithToken();
|
||||
const { mutate, query } = await this.getGraphQLClient();
|
||||
const getInvitedByResult = await query({
|
||||
query: GQL.GetInvitedByDocument,
|
||||
variables,
|
||||
@ -46,7 +45,7 @@ export class CustomersService extends BaseService {
|
||||
}
|
||||
|
||||
async getCustomer(variables: VariablesOf<typeof GQL.GetCustomerDocument>) {
|
||||
const { query } = await getClientWithToken();
|
||||
const { query } = await this.getGraphQLClient();
|
||||
|
||||
const result = await query({
|
||||
query: GQL.GetCustomerDocument,
|
||||
@ -61,7 +60,7 @@ export class CustomersService extends BaseService {
|
||||
async getCustomers(variables: VariablesOf<typeof GQL.GetCustomersDocument>) {
|
||||
await this.checkIsBanned();
|
||||
|
||||
const { query } = await getClientWithToken();
|
||||
const { query } = await this.getGraphQLClient();
|
||||
|
||||
const result = await query({
|
||||
query: GQL.GetCustomersDocument,
|
||||
@ -77,7 +76,7 @@ export class CustomersService extends BaseService {
|
||||
async getInvited(variables?: VariablesOf<typeof GQL.GetInvitedDocument>) {
|
||||
await this.checkIsBanned();
|
||||
|
||||
const { query } = await getClientWithToken();
|
||||
const { query } = await this.getGraphQLClient();
|
||||
|
||||
const result = await query({
|
||||
query: GQL.GetInvitedDocument,
|
||||
@ -92,7 +91,7 @@ export class CustomersService extends BaseService {
|
||||
async getInvitedBy(variables?: VariablesOf<typeof GQL.GetInvitedByDocument>) {
|
||||
await this.checkIsBanned();
|
||||
|
||||
const { query } = await getClientWithToken();
|
||||
const { query } = await this.getGraphQLClient();
|
||||
|
||||
const result = await query({
|
||||
query: GQL.GetInvitedByDocument,
|
||||
@ -116,7 +115,7 @@ export class CustomersService extends BaseService {
|
||||
throw new Error(ERRORS.NO_PERMISSION);
|
||||
}
|
||||
|
||||
const { mutate } = await getClientWithToken();
|
||||
const { mutate } = await this.getGraphQLClient();
|
||||
|
||||
const mutationResult = await mutate({
|
||||
mutation: GQL.UpdateCustomerDocument,
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
/* eslint-disable sonarjs/cognitive-complexity */
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
import { getClientWithToken } from '../apollo/client';
|
||||
import { ERRORS as SHARED_ERRORS } from '../constants/errors';
|
||||
import * as GQL from '../types';
|
||||
import { BaseService } from './base';
|
||||
@ -123,7 +122,7 @@ export class OrdersService extends BaseService {
|
||||
|
||||
const isSlotMaster = slot.master.documentId === customer.documentId;
|
||||
|
||||
const { mutate } = await getClientWithToken();
|
||||
const { mutate } = await this.getGraphQLClient();
|
||||
|
||||
const mutationResult = await mutate({
|
||||
mutation: GQL.CreateOrderDocument,
|
||||
@ -145,7 +144,7 @@ export class OrdersService extends BaseService {
|
||||
|
||||
async getOrder(variables: VariablesOf<typeof GQL.GetOrderDocument>) {
|
||||
await this.checkIsBanned();
|
||||
const { query } = await getClientWithToken();
|
||||
const { query } = await this.getGraphQLClient();
|
||||
|
||||
const result = await query({
|
||||
query: GQL.GetOrderDocument,
|
||||
@ -157,7 +156,7 @@ export class OrdersService extends BaseService {
|
||||
|
||||
async getOrders(variables: VariablesOf<typeof GQL.GetOrdersDocument>) {
|
||||
await this.checkIsBanned();
|
||||
const { query } = await getClientWithToken();
|
||||
const { query } = await this.getGraphQLClient();
|
||||
|
||||
const result = await query({
|
||||
query: GQL.GetOrdersDocument,
|
||||
@ -177,7 +176,7 @@ export class OrdersService extends BaseService {
|
||||
|
||||
const { customer } = await this._getUser();
|
||||
|
||||
const { query } = await getClientWithToken();
|
||||
const { query } = await this.getGraphQLClient();
|
||||
|
||||
const {
|
||||
data: { order },
|
||||
@ -204,7 +203,7 @@ export class OrdersService extends BaseService {
|
||||
throw new Error(SHARED_ERRORS.NO_PERMISSION);
|
||||
}
|
||||
|
||||
const { mutate } = await getClientWithToken();
|
||||
const { mutate } = await this.getGraphQLClient();
|
||||
|
||||
const lastOrderNumber = await this.getLastOrderNumber(variables);
|
||||
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
import { getClientWithToken } from '../apollo/client';
|
||||
import { ERRORS } from '../constants/errors';
|
||||
import * as GQL from '../types';
|
||||
import { BaseService } from './base';
|
||||
@ -10,7 +9,7 @@ export class ServicesService extends BaseService {
|
||||
|
||||
const { customer } = await this._getUser();
|
||||
|
||||
const { mutate } = await getClientWithToken();
|
||||
const { mutate } = await this.getGraphQLClient();
|
||||
|
||||
const mutationResult = await mutate({
|
||||
mutation: GQL.CreateServiceDocument,
|
||||
@ -32,7 +31,7 @@ export class ServicesService extends BaseService {
|
||||
async getService(variables: VariablesOf<typeof GQL.GetServiceDocument>) {
|
||||
await this.checkIsBanned();
|
||||
|
||||
const { query } = await getClientWithToken();
|
||||
const { query } = await this.getGraphQLClient();
|
||||
|
||||
const result = await query({
|
||||
query: GQL.GetServiceDocument,
|
||||
@ -45,7 +44,7 @@ export class ServicesService extends BaseService {
|
||||
async getServices(variables: VariablesOf<typeof GQL.GetServicesDocument>) {
|
||||
await this.checkIsBanned();
|
||||
|
||||
const { query } = await getClientWithToken();
|
||||
const { query } = await this.getGraphQLClient();
|
||||
|
||||
const result = await query({
|
||||
query: GQL.GetServicesDocument,
|
||||
@ -60,7 +59,7 @@ export class ServicesService extends BaseService {
|
||||
|
||||
await this.checkPermission(variables);
|
||||
|
||||
const { mutate } = await getClientWithToken();
|
||||
const { mutate } = await this.getGraphQLClient();
|
||||
|
||||
const mutationResult = await mutate({
|
||||
mutation: GQL.UpdateServiceDocument,
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
import { getClientWithToken } from '../apollo/client';
|
||||
import { ERRORS as SHARED_ERRORS } from '../constants/errors';
|
||||
import * as GQL from '../types';
|
||||
import { BaseService } from './base';
|
||||
@ -30,7 +29,7 @@ export class SlotsService extends BaseService {
|
||||
|
||||
const { customer } = await this._getUser();
|
||||
|
||||
const { mutate } = await getClientWithToken();
|
||||
const { mutate } = await this.getGraphQLClient();
|
||||
|
||||
const mutationResult = await mutate({
|
||||
mutation: GQL.CreateSlotDocument,
|
||||
@ -60,7 +59,7 @@ export class SlotsService extends BaseService {
|
||||
throw new Error(ERRORS.SLOT_HAS_ORDERS);
|
||||
}
|
||||
|
||||
const { mutate } = await getClientWithToken();
|
||||
const { mutate } = await this.getGraphQLClient();
|
||||
|
||||
const mutationResult = await mutate({
|
||||
mutation: GQL.DeleteSlotDocument,
|
||||
@ -102,7 +101,7 @@ export class SlotsService extends BaseService {
|
||||
0,
|
||||
);
|
||||
|
||||
const { query } = await getClientWithToken();
|
||||
const { query } = await this.getGraphQLClient();
|
||||
|
||||
const getSlotsResult = await query({
|
||||
query: GQL.GetSlotsOrdersDocument,
|
||||
@ -154,7 +153,7 @@ export class SlotsService extends BaseService {
|
||||
async getSlot(variables: VariablesOf<typeof GQL.GetSlotDocument>) {
|
||||
await this.checkIsBanned();
|
||||
|
||||
const { query } = await getClientWithToken();
|
||||
const { query } = await this.getGraphQLClient();
|
||||
|
||||
const result = await query({
|
||||
query: GQL.GetSlotDocument,
|
||||
@ -167,7 +166,7 @@ export class SlotsService extends BaseService {
|
||||
async getSlots(variables: VariablesOf<typeof GQL.GetSlotsDocument>) {
|
||||
await this.checkIsBanned();
|
||||
|
||||
const { query } = await getClientWithToken();
|
||||
const { query } = await this.getGraphQLClient();
|
||||
|
||||
const result = await query({
|
||||
query: GQL.GetSlotsDocument,
|
||||
@ -183,7 +182,7 @@ export class SlotsService extends BaseService {
|
||||
await this.checkPermission(variables);
|
||||
await this.checkBeforeUpdateDatetime(variables);
|
||||
|
||||
const { mutate } = await getClientWithToken();
|
||||
const { mutate } = await this.getGraphQLClient();
|
||||
|
||||
const mutationResult = await mutate({
|
||||
mutation: GQL.UpdateSlotDocument,
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
import { getClientWithToken } from '../apollo/client';
|
||||
import * as GQL from '../types';
|
||||
import { BaseService } from './base';
|
||||
import { OrdersService } from './orders';
|
||||
@ -86,7 +85,7 @@ export class SubscriptionsService extends BaseService {
|
||||
async createSubscription(variables: VariablesOf<typeof GQL.CreateSubscriptionDocument>) {
|
||||
await this.checkIsBanned();
|
||||
|
||||
const { mutate } = await getClientWithToken();
|
||||
const { mutate } = await this.getGraphQLClient();
|
||||
|
||||
const mutationResult = await mutate({
|
||||
mutation: GQL.CreateSubscriptionDocument,
|
||||
@ -104,7 +103,7 @@ export class SubscriptionsService extends BaseService {
|
||||
) {
|
||||
await this.checkIsBanned();
|
||||
|
||||
const { mutate } = await getClientWithToken();
|
||||
const { mutate } = await this.getGraphQLClient();
|
||||
|
||||
const mutationResult = await mutate({
|
||||
mutation: GQL.CreateSubscriptionHistoryDocument,
|
||||
@ -209,7 +208,7 @@ export class SubscriptionsService extends BaseService {
|
||||
}
|
||||
|
||||
async getSubscriptionHistory(variables: VariablesOf<typeof GQL.GetSubscriptionHistoryDocument>) {
|
||||
const { query } = await getClientWithToken();
|
||||
const { query } = await this.getGraphQLClient();
|
||||
|
||||
const result = await query({
|
||||
query: GQL.GetSubscriptionHistoryDocument,
|
||||
@ -224,7 +223,7 @@ export class SubscriptionsService extends BaseService {
|
||||
async getSubscriptionPrices(variables?: VariablesOf<typeof GQL.GetSubscriptionPricesDocument>) {
|
||||
await this.checkIsBanned();
|
||||
|
||||
const { query } = await getClientWithToken();
|
||||
const { query } = await this.getGraphQLClient();
|
||||
|
||||
const result = await query({
|
||||
query: GQL.GetSubscriptionPricesDocument,
|
||||
@ -237,7 +236,7 @@ export class SubscriptionsService extends BaseService {
|
||||
async getSubscriptions(variables?: VariablesOf<typeof GQL.GetSubscriptionsDocument>) {
|
||||
await this.checkIsBanned();
|
||||
|
||||
const { query } = await getClientWithToken();
|
||||
const { query } = await this.getGraphQLClient();
|
||||
|
||||
const result = await query({
|
||||
query: GQL.GetSubscriptionsDocument,
|
||||
@ -250,7 +249,7 @@ export class SubscriptionsService extends BaseService {
|
||||
async getSubscriptionSettings() {
|
||||
await this.checkIsBanned();
|
||||
|
||||
const { query } = await getClientWithToken();
|
||||
const { query } = await this.getGraphQLClient();
|
||||
|
||||
const result = await query({
|
||||
query: GQL.GetSubscriptionSettingsDocument,
|
||||
@ -259,40 +258,6 @@ export class SubscriptionsService extends BaseService {
|
||||
return result.data;
|
||||
}
|
||||
|
||||
async updateSubscription(variables: VariablesOf<typeof GQL.UpdateSubscriptionDocument>) {
|
||||
await this.checkIsBanned();
|
||||
|
||||
const { mutate } = await getClientWithToken();
|
||||
|
||||
const mutationResult = await mutate({
|
||||
mutation: GQL.UpdateSubscriptionDocument,
|
||||
variables,
|
||||
});
|
||||
|
||||
const error = mutationResult.errors?.at(0);
|
||||
if (error) throw new Error(error.message);
|
||||
|
||||
return mutationResult.data;
|
||||
}
|
||||
|
||||
async updateSubscriptionHistory(
|
||||
variables: VariablesOf<typeof GQL.UpdateSubscriptionHistoryDocument>,
|
||||
) {
|
||||
await this.checkIsBanned();
|
||||
|
||||
const { mutate } = await getClientWithToken();
|
||||
|
||||
const mutationResult = await mutate({
|
||||
mutation: GQL.UpdateSubscriptionHistoryDocument,
|
||||
variables,
|
||||
});
|
||||
|
||||
const error = mutationResult.errors?.at(0);
|
||||
if (error) throw new Error(error.message);
|
||||
|
||||
return mutationResult.data;
|
||||
}
|
||||
|
||||
async hasTrialSubscription() {
|
||||
const { customer } = await this._getUser();
|
||||
|
||||
@ -326,6 +291,40 @@ export class SubscriptionsService extends BaseService {
|
||||
);
|
||||
}
|
||||
|
||||
async updateSubscription(variables: VariablesOf<typeof GQL.UpdateSubscriptionDocument>) {
|
||||
await this.checkIsBanned();
|
||||
|
||||
const { mutate } = await this.getGraphQLClient();
|
||||
|
||||
const mutationResult = await mutate({
|
||||
mutation: GQL.UpdateSubscriptionDocument,
|
||||
variables,
|
||||
});
|
||||
|
||||
const error = mutationResult.errors?.at(0);
|
||||
if (error) throw new Error(error.message);
|
||||
|
||||
return mutationResult.data;
|
||||
}
|
||||
|
||||
async updateSubscriptionHistory(
|
||||
variables: VariablesOf<typeof GQL.UpdateSubscriptionHistoryDocument>,
|
||||
) {
|
||||
await this.checkIsBanned();
|
||||
|
||||
const { mutate } = await this.getGraphQLClient();
|
||||
|
||||
const mutationResult = await mutate({
|
||||
mutation: GQL.UpdateSubscriptionHistoryDocument,
|
||||
variables,
|
||||
});
|
||||
|
||||
const error = mutationResult.errors?.at(0);
|
||||
if (error) throw new Error(error.message);
|
||||
|
||||
return mutationResult.data;
|
||||
}
|
||||
|
||||
private getRemainingDays(subscription: GQL.SubscriptionFieldsFragment) {
|
||||
if (!subscription) return 0;
|
||||
|
||||
|
||||
@ -1,18 +1,17 @@
|
||||
import { env as environment } from '../config/env';
|
||||
import { getToken } from '../config/token';
|
||||
import { createLink } from './link';
|
||||
import { ApolloClient, InMemoryCache } from '@apollo/client/core';
|
||||
|
||||
type Parameters_ = { token: null | string | undefined };
|
||||
type Parameters = { token: null | string | undefined };
|
||||
|
||||
export function createApolloClient(parameters?: Parameters_) {
|
||||
export function createApolloClient(parameters?: Parameters) {
|
||||
return new ApolloClient({
|
||||
cache: new InMemoryCache(),
|
||||
headers: parameters?.token
|
||||
? {
|
||||
Authorization: `Bearer ${parameters.token}`,
|
||||
}
|
||||
: undefined,
|
||||
uri: environment.URL_GRAPHQL,
|
||||
link: createLink({
|
||||
token: parameters?.token,
|
||||
uri: environment.URL_GRAPHQL_CACHED,
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
16
packages/graphql/apollo/link.ts
Normal file
16
packages/graphql/apollo/link.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import { ApolloLink, from, HttpLink } from '@apollo/client/core';
|
||||
|
||||
type Parameters = { token: null | string | undefined; uri: string };
|
||||
|
||||
export function createLink({ token, uri }: Parameters) {
|
||||
const cacheLink = new ApolloLink((operation, forward) => {
|
||||
return forward(operation);
|
||||
});
|
||||
|
||||
const httpLink = new HttpLink({
|
||||
headers: token ? { Authorization: `Bearer ${token}` } : undefined,
|
||||
uri,
|
||||
});
|
||||
|
||||
return from([cacheLink, httpLink]);
|
||||
}
|
||||
@ -1,3 +1,4 @@
|
||||
/* eslint-disable sonarjs/no-clear-text-protocols */
|
||||
/* eslint-disable unicorn/prevent-abbreviations */
|
||||
import { z } from 'zod';
|
||||
|
||||
@ -6,6 +7,7 @@ export const envSchema = z.object({
|
||||
LOGIN_GRAPHQL: z.string(),
|
||||
PASSWORD_GRAPHQL: z.string(),
|
||||
URL_GRAPHQL: z.string(),
|
||||
URL_GRAPHQL_CACHED: z.string().default('http://cache-proxy:5000/api/graphql'),
|
||||
});
|
||||
|
||||
export const env = envSchema.parse(process.env);
|
||||
|
||||
@ -58,7 +58,7 @@ query GetSubscriptions($filters: SubscriptionFiltersInput) {
|
||||
}
|
||||
}
|
||||
|
||||
query getSubscriptionSettings {
|
||||
query GetSubscriptionSettings {
|
||||
subscriptionSetting {
|
||||
...SubscriptionSettingFields
|
||||
}
|
||||
|
||||
@ -14,6 +14,7 @@
|
||||
"@repo/utils": "workspace:",
|
||||
"@types/jsonwebtoken": "^9.0.7",
|
||||
"dayjs": "catalog:",
|
||||
"ioredis": "^5.7.0",
|
||||
"jsonwebtoken": "catalog:",
|
||||
"radashi": "catalog:",
|
||||
"vite-tsconfig-paths": "catalog:",
|
||||
|
||||
@ -996,7 +996,7 @@ export const GetSlotDocument = {"kind":"Document","definitions":[{"kind":"Operat
|
||||
export const UpdateSlotDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"UpdateSlot"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"documentId"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"ID"}}}},{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"data"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"SlotInput"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"updateSlot"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"documentId"},"value":{"kind":"Variable","name":{"kind":"Name","value":"documentId"}}},{"kind":"Argument","name":{"kind":"Name","value":"data"},"value":{"kind":"Variable","name":{"kind":"Name","value":"data"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"SlotFields"}}]}}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"CustomerFields"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Customer"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"active"}},{"kind":"Field","name":{"kind":"Name","value":"bannedUntil"}},{"kind":"Field","name":{"kind":"Name","value":"documentId"}},{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","name":{"kind":"Name","value":"phone"}},{"kind":"Field","name":{"kind":"Name","value":"photoUrl"}},{"kind":"Field","name":{"kind":"Name","value":"role"}},{"kind":"Field","name":{"kind":"Name","value":"telegramId"}},{"kind":"Field","name":{"kind":"Name","value":"services"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"filters"},"value":{"kind":"ObjectValue","fields":[{"kind":"ObjectField","name":{"kind":"Name","value":"active"},"value":{"kind":"ObjectValue","fields":[{"kind":"ObjectField","name":{"kind":"Name","value":"eq"},"value":{"kind":"BooleanValue","value":true}}]}}]}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"documentId"}},{"kind":"Field","name":{"kind":"Name","value":"name"}}]}}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"SlotFields"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Slot"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"documentId"}},{"kind":"Field","name":{"kind":"Name","value":"datetime_start"}},{"kind":"Field","name":{"kind":"Name","value":"datetime_end"}},{"kind":"Field","name":{"kind":"Name","value":"state"}},{"kind":"Field","name":{"kind":"Name","value":"master"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"CustomerFields"}}]}}]}}]} as unknown as DocumentNode<UpdateSlotMutation, UpdateSlotMutationVariables>;
|
||||
export const DeleteSlotDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"DeleteSlot"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"documentId"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"ID"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"deleteSlot"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"documentId"},"value":{"kind":"Variable","name":{"kind":"Name","value":"documentId"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"documentId"}}]}}]}}]} as unknown as DocumentNode<DeleteSlotMutation, DeleteSlotMutationVariables>;
|
||||
export const GetSubscriptionsDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetSubscriptions"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"filters"}},"type":{"kind":"NamedType","name":{"kind":"Name","value":"SubscriptionFiltersInput"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"subscriptions"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"filters"},"value":{"kind":"Variable","name":{"kind":"Name","value":"filters"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"SubscriptionFields"}}]}}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"SubscriptionFields"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Subscription"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"documentId"}},{"kind":"Field","name":{"kind":"Name","value":"active"}},{"kind":"Field","name":{"kind":"Name","value":"expiresAt"}},{"kind":"Field","name":{"kind":"Name","value":"nextSubscription"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"documentId"}}]}}]}}]} as unknown as DocumentNode<GetSubscriptionsQuery, GetSubscriptionsQueryVariables>;
|
||||
export const GetSubscriptionSettingsDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"getSubscriptionSettings"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"subscriptionSetting"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"SubscriptionSettingFields"}}]}}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"SubscriptionSettingFields"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"SubscriptionSetting"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"documentId"}},{"kind":"Field","name":{"kind":"Name","value":"maxOrdersPerMonth"}},{"kind":"Field","name":{"kind":"Name","value":"referralRewardDays"}},{"kind":"Field","name":{"kind":"Name","value":"proEnabled"}}]}}]} as unknown as DocumentNode<GetSubscriptionSettingsQuery, GetSubscriptionSettingsQueryVariables>;
|
||||
export const GetSubscriptionSettingsDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetSubscriptionSettings"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"subscriptionSetting"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"SubscriptionSettingFields"}}]}}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"SubscriptionSettingFields"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"SubscriptionSetting"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"documentId"}},{"kind":"Field","name":{"kind":"Name","value":"maxOrdersPerMonth"}},{"kind":"Field","name":{"kind":"Name","value":"referralRewardDays"}},{"kind":"Field","name":{"kind":"Name","value":"proEnabled"}}]}}]} as unknown as DocumentNode<GetSubscriptionSettingsQuery, GetSubscriptionSettingsQueryVariables>;
|
||||
export const GetSubscriptionPricesDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetSubscriptionPrices"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"filters"}},"type":{"kind":"NamedType","name":{"kind":"Name","value":"SubscriptionPriceFiltersInput"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"subscriptionPrices"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"filters"},"value":{"kind":"Variable","name":{"kind":"Name","value":"filters"}}},{"kind":"Argument","name":{"kind":"Name","value":"sort"},"value":{"kind":"StringValue","value":"amount:asc","block":false}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"SubscriptionPriceFields"}}]}}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"SubscriptionPriceFields"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"SubscriptionPrice"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"documentId"}},{"kind":"Field","name":{"kind":"Name","value":"period"}},{"kind":"Field","name":{"kind":"Name","value":"days"}},{"kind":"Field","name":{"kind":"Name","value":"amount"}},{"kind":"Field","name":{"kind":"Name","value":"currency"}},{"kind":"Field","name":{"kind":"Name","value":"active"}},{"kind":"Field","name":{"kind":"Name","value":"description"}}]}}]} as unknown as DocumentNode<GetSubscriptionPricesQuery, GetSubscriptionPricesQueryVariables>;
|
||||
export const GetSubscriptionHistoryDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetSubscriptionHistory"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"filters"}},"type":{"kind":"NamedType","name":{"kind":"Name","value":"SubscriptionHistoryFiltersInput"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"subscriptionHistories"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"filters"},"value":{"kind":"Variable","name":{"kind":"Name","value":"filters"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"SubscriptionHistoryFields"}}]}}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"SubscriptionFields"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Subscription"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"documentId"}},{"kind":"Field","name":{"kind":"Name","value":"active"}},{"kind":"Field","name":{"kind":"Name","value":"expiresAt"}},{"kind":"Field","name":{"kind":"Name","value":"nextSubscription"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"documentId"}}]}}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"SubscriptionHistoryFields"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"SubscriptionHistory"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"documentId"}},{"kind":"Field","name":{"kind":"Name","value":"period"}},{"kind":"Field","name":{"kind":"Name","value":"amount"}},{"kind":"Field","name":{"kind":"Name","value":"currency"}},{"kind":"Field","name":{"kind":"Name","value":"state"}},{"kind":"Field","name":{"kind":"Name","value":"paymentId"}},{"kind":"Field","name":{"kind":"Name","value":"source"}},{"kind":"Field","name":{"kind":"Name","value":"description"}},{"kind":"Field","name":{"kind":"Name","value":"subscription"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"SubscriptionFields"}}]}}]}}]} as unknown as DocumentNode<GetSubscriptionHistoryQuery, GetSubscriptionHistoryQueryVariables>;
|
||||
export const CreateSubscriptionDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"CreateSubscription"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"data"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"SubscriptionInput"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"createSubscription"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"data"},"value":{"kind":"Variable","name":{"kind":"Name","value":"data"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"FragmentSpread","name":{"kind":"Name","value":"SubscriptionFields"}}]}}]}},{"kind":"FragmentDefinition","name":{"kind":"Name","value":"SubscriptionFields"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Subscription"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"documentId"}},{"kind":"Field","name":{"kind":"Name","value":"active"}},{"kind":"Field","name":{"kind":"Name","value":"expiresAt"}},{"kind":"Field","name":{"kind":"Name","value":"nextSubscription"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"documentId"}}]}}]}}]} as unknown as DocumentNode<CreateSubscriptionMutation, CreateSubscriptionMutationVariables>;
|
||||
|
||||
4651
pnpm-lock.yaml
generated
4651
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@ -13,7 +13,10 @@
|
||||
"BOT_TOKEN",
|
||||
"NEXTAUTH_SECRET",
|
||||
"BOT_URL",
|
||||
"BOT_PROVIDER_TOKEN"
|
||||
"BOT_PROVIDER_TOKEN",
|
||||
"REDIS_HOST",
|
||||
"REDIS_PORT",
|
||||
"REDIS_PASSWORD"
|
||||
]
|
||||
},
|
||||
"lint": {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user