mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-08-26 10:09:39 +00:00
Compare commits
123 Commits
c0b95a8b4b
...
task/add-a
Author | SHA1 | Date | |
---|---|---|---|
dcdb10a1fb | |||
2aa6a40af7 | |||
7ae826eb71 | |||
beed6fcfb7 | |||
015470b5ea | |||
36640b104b | |||
d4af9eaccc | |||
f447cfa065 | |||
9d952d2fb0 | |||
c0849c2252 | |||
9254db08f9 | |||
b9c4308d1c | |||
db95cc0dab | |||
87c380ab6f | |||
212d0d1974 | |||
a060f92208 | |||
6d529ee0ae | |||
85687e7950 | |||
7e2c3136cf | |||
61348aa351 | |||
dea942f11e | |||
d62e620828 | |||
f0556813ac | |||
6d2252a403 | |||
8d265c9105 | |||
a4095c837b | |||
65d4a56135 | |||
fffa27b6ee | |||
12579fcd6e | |||
036c8bea17 | |||
e5970c02c1 | |||
2c3b985594 | |||
a9cb1b6704 | |||
a17a271213 | |||
712b7443ac | |||
945328c0ce | |||
009deaf403 | |||
3cfed2b452 | |||
09322c5b80 | |||
74d3620d0e | |||
83be80d9f6 | |||
2589e391ed | |||
5cf45c30f4 | |||
0bb178ed10 | |||
9971fb953d | |||
7a07f39f16 | |||
18b21d697c | |||
66391bafd8 | |||
25599b9fe2 | |||
807c5b7dd3 | |||
60bc03cf79 | |||
a9eaf44d31 | |||
d232c06ebe | |||
5c916ed445 | |||
b2fb378e52 | |||
c5f8f96977 | |||
8f9b15f49f | |||
0b9eef276e | |||
b3f8b92826 | |||
b9da00aaa6 | |||
5bf44a18e1 | |||
2b2772e4ca | |||
13c0f87fc6 | |||
c9d794d988 | |||
5d4e5ca87e | |||
f4e748d735 | |||
f4f7999ae0 | |||
82c82d521c | |||
c7a4ff1194 | |||
8a4633b158 | |||
f80d097ff8 | |||
04bd156df1 | |||
731819aeaa | |||
68d2d3b53d | |||
3fcfe2d92f | |||
c0a069b460 | |||
30724d7d37 | |||
324661e1ee | |||
a83424f45b | |||
71f6ccb4db | |||
68692b7c8b | |||
4d60c1ed54 | |||
27dbe04299 | |||
9bebcb2f3e | |||
43ab0030f0 | |||
c48adb73b5 | |||
d255e6811e | |||
e58d2d4831 | |||
147cf0b582 | |||
4e6b6f6ac5 | |||
932a3efd1c | |||
0a1ccad120 | |||
f337e6c681 | |||
f5bf857071 | |||
d1d4d529a8 | |||
37b582f521 | |||
cf19f08dca | |||
ff370b2baa | |||
04f64407e1 | |||
d7eef5d03e | |||
c8d691b380 | |||
75d03366c2 | |||
52cb69cc84 | |||
a6053b3971 | |||
60d2c8330b | |||
fddd06e06d | |||
3160773c2a | |||
d3d84da5e3 | |||
110ed4157a | |||
aa9e90bf08 | |||
c5dd5e28fd | |||
603e74af09 | |||
0e36f32ed6 | |||
705ceeba29 | |||
a37d5bb299 | |||
49cc762962 | |||
a94d4610ed | |||
274cdf741f | |||
df59e9a4a3 | |||
8c34c68ba6 | |||
689a38ee0c | |||
332b2f5851 | |||
8d44b66dd3 |
@ -21,6 +21,7 @@ module.exports = {
|
|||||||
'@typescript-eslint/explicit-function-return-type': 'off',
|
'@typescript-eslint/explicit-function-return-type': 'off',
|
||||||
'@typescript-eslint/explicit-module-boundary-types': 'off',
|
'@typescript-eslint/explicit-module-boundary-types': 'off',
|
||||||
'@typescript-eslint/no-explicit-any': 'off',
|
'@typescript-eslint/no-explicit-any': 'off',
|
||||||
|
"@typescript-eslint/no-unused-vars": 'warn',
|
||||||
},
|
},
|
||||||
settings: {
|
settings: {
|
||||||
'import/resolver': {
|
'import/resolver': {
|
||||||
|
17
.github/pull_request_template.md
vendored
Normal file
17
.github/pull_request_template.md
vendored
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<!--
|
||||||
|
Thanks for contributing!
|
||||||
|
|
||||||
|
Provide a description of your changes below and a general summary in the title.
|
||||||
|
-->
|
||||||
|
|
||||||
|
## Jira Ticket
|
||||||
|
|
||||||
|
[SP-0000](https://syncrow.atlassian.net/browse/SP-0000)
|
||||||
|
|
||||||
|
## Description
|
||||||
|
|
||||||
|
<!--- Describe your changes in detail -->
|
||||||
|
|
||||||
|
## How to Test
|
||||||
|
|
||||||
|
<!--- Describe the created APIs / Logic -->
|
64
.github/workflows/pr-description.yml
vendored
Normal file
64
.github/workflows/pr-description.yml
vendored
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
name: 🤖 AI PR Description Commenter (100% Safe with jq)
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
types: [opened, edited]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
generate-description:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout Repo
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Install GitHub CLI and jq
|
||||||
|
run: |
|
||||||
|
sudo apt-get update
|
||||||
|
sudo apt-get install gh jq -y
|
||||||
|
|
||||||
|
- name: Fetch PR Commits
|
||||||
|
id: fetch_commits
|
||||||
|
run: |
|
||||||
|
COMMITS=$(gh pr view ${{ github.event.pull_request.number }} --json commits --jq '.commits[].message' | sed 's/^/- /')
|
||||||
|
echo "commits<<EOF" >> $GITHUB_ENV
|
||||||
|
echo "$COMMITS" >> $GITHUB_ENV
|
||||||
|
echo "EOF" >> $GITHUB_ENV
|
||||||
|
env:
|
||||||
|
GH_TOKEN: ${{ secrets.GH_PERSONAL_TOKEN }}
|
||||||
|
|
||||||
|
- name: Generate PR Description with OpenAI (Safe JSON with jq)
|
||||||
|
run: |
|
||||||
|
REQUEST_BODY=$(jq -n \
|
||||||
|
--arg model "gpt-4o" \
|
||||||
|
--arg content "Given the following commit messages:\n\n${commits}\n\nGenerate a clear and professional pull request description." \
|
||||||
|
'{
|
||||||
|
model: $model,
|
||||||
|
messages: [{ role: "user", content: $content }]
|
||||||
|
}'
|
||||||
|
)
|
||||||
|
|
||||||
|
RESPONSE=$(curl -s https://api.openai.com/v1/chat/completions \
|
||||||
|
-H "Authorization: Bearer $OPENAI_API_KEY" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-d "$REQUEST_BODY")
|
||||||
|
|
||||||
|
DESCRIPTION=$(echo "$RESPONSE" | jq -r '.choices[0].message.content')
|
||||||
|
|
||||||
|
echo "---------- OpenAI Raw Response ----------"
|
||||||
|
echo "$RESPONSE"
|
||||||
|
echo "---------- Extracted Description ----------"
|
||||||
|
echo "$DESCRIPTION"
|
||||||
|
|
||||||
|
echo "description<<EOF" >> $GITHUB_ENV
|
||||||
|
echo "$DESCRIPTION" >> $GITHUB_ENV
|
||||||
|
echo "EOF" >> $GITHUB_ENV
|
||||||
|
env:
|
||||||
|
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
|
||||||
|
commits: ${{ env.commits }}
|
||||||
|
|
||||||
|
- name: Post AI Generated Description as Comment
|
||||||
|
run: |
|
||||||
|
gh pr comment ${{ github.event.pull_request.number }} --body "${{ env.description }}"
|
||||||
|
env:
|
||||||
|
GH_TOKEN: ${{ secrets.GH_PERSONAL_TOKEN }}
|
40
.github/workflows/production.yml
vendored
40
.github/workflows/production.yml
vendored
@ -1,40 +0,0 @@
|
|||||||
name: 🚀 Production Deployment
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
deploy:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: ⬇️ Checkout Code
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: 🐢 Set up Node.js 20.x
|
|
||||||
uses: actions/setup-node@v4
|
|
||||||
with:
|
|
||||||
node-version: '20'
|
|
||||||
|
|
||||||
- name: 🐳 Set up Docker Buildx
|
|
||||||
uses: docker/setup-buildx-action@v3
|
|
||||||
|
|
||||||
- name: 🔐 Configure AWS Credentials
|
|
||||||
uses: aws-actions/configure-aws-credentials@v4
|
|
||||||
with:
|
|
||||||
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
|
|
||||||
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
|
|
||||||
aws-region: me-central-1
|
|
||||||
|
|
||||||
- name: 🗂️ Write .env file from ENV_FILE Secret
|
|
||||||
run: echo "${{ secrets.ENV_FILE }}" > .env
|
|
||||||
|
|
||||||
- name: 📦 Install Dependencies
|
|
||||||
run: npm install
|
|
||||||
|
|
||||||
- name: 🛠️ Run Production Build & Deploy Script
|
|
||||||
run: |
|
|
||||||
chmod +x ./build.sh
|
|
||||||
./build.sh
|
|
9
.gitignore
vendored
9
.gitignore
vendored
@ -4,7 +4,7 @@
|
|||||||
/build
|
/build
|
||||||
|
|
||||||
#github
|
#github
|
||||||
/.github
|
/.github/workflows
|
||||||
|
|
||||||
# Logs
|
# Logs
|
||||||
logs
|
logs
|
||||||
@ -58,9 +58,4 @@ pids
|
|||||||
# Diagnostic reports (https://nodejs.org/api/report.html)
|
# Diagnostic reports (https://nodejs.org/api/report.html)
|
||||||
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
||||||
|
|
||||||
config.dev
|
config.dev
|
||||||
cdk.out
|
|
||||||
backend-cdk-new.out
|
|
||||||
web-cdk.out
|
|
||||||
backend-cdk.out
|
|
||||||
backend-cdk-final.out
|
|
22
Dockerfile
22
Dockerfile
@ -1,28 +1,16 @@
|
|||||||
FROM --platform=linux/amd64 node:20-alpine
|
FROM node:20-alpine
|
||||||
|
|
||||||
# curl for health checks
|
|
||||||
RUN apk add --no-cache curl
|
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
|
|
||||||
COPY package*.json ./
|
COPY package*.json ./
|
||||||
|
|
||||||
RUN npm install --production --ignore-scripts
|
RUN npm install
|
||||||
|
RUN npm install -g @nestjs/cli
|
||||||
|
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
RUN npm run build
|
RUN npm run build
|
||||||
|
|
||||||
RUN addgroup -g 1001 -S nodejs
|
EXPOSE 4000
|
||||||
RUN adduser -S nestjs -u 1001
|
|
||||||
|
|
||||||
RUN chown -R nestjs:nodejs /app
|
CMD ["npm", "run", "start"]
|
||||||
USER nestjs
|
|
||||||
|
|
||||||
EXPOSE 3000
|
|
||||||
|
|
||||||
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
|
|
||||||
CMD curl -f http://localhost:3000/health || exit 1
|
|
||||||
|
|
||||||
CMD ["npm", "run", "start:prod"]
|
|
||||||
|
129
GITHUB_SETUP.md
129
GITHUB_SETUP.md
@ -1,129 +0,0 @@
|
|||||||
# GitHub Actions Setup Guide
|
|
||||||
|
|
||||||
## Required GitHub Secrets
|
|
||||||
|
|
||||||
Add these secrets to your GitHub repository (Settings > Secrets and variables > Actions):
|
|
||||||
|
|
||||||
### AWS Credentials
|
|
||||||
|
|
||||||
```
|
|
||||||
AWS_ACCESS_KEY_ID=your-aws-access-key
|
|
||||||
AWS_SECRET_ACCESS_KEY=your-aws-secret-key
|
|
||||||
```
|
|
||||||
|
|
||||||
### JWT Configuration (CRITICAL - Generate secure random strings)
|
|
||||||
|
|
||||||
```
|
|
||||||
JWT_SECRET=your-super-secure-jwt-secret-key-here
|
|
||||||
JWT_SECRET_REFRESH=your-super-secure-refresh-secret-key-here
|
|
||||||
SECRET_KEY=your-general-encryption-secret-key-here
|
|
||||||
```
|
|
||||||
|
|
||||||
### Admin Configuration
|
|
||||||
|
|
||||||
```
|
|
||||||
SUPER_ADMIN_EMAIL=admin@syncrow.ae
|
|
||||||
SUPER_ADMIN_PASSWORD=YourSecureAdminPassword123!
|
|
||||||
```
|
|
||||||
|
|
||||||
### Tuya IoT Configuration
|
|
||||||
|
|
||||||
```
|
|
||||||
TUYA_ACCESS_ID=your-tuya-access-id
|
|
||||||
TUYA_ACCESS_KEY=your-tuya-access-key
|
|
||||||
TRUN_ON_TUYA_SOCKET=true-or-false
|
|
||||||
```
|
|
||||||
|
|
||||||
### Firebase Configuration
|
|
||||||
|
|
||||||
```
|
|
||||||
FIREBASE_API_KEY=your-firebase-api-key
|
|
||||||
FIREBASE_AUTH_DOMAIN=your-project.firebaseapp.com
|
|
||||||
FIREBASE_PROJECT_ID=your-project-id
|
|
||||||
FIREBASE_STORAGE_BUCKET=your-project.appspot.com
|
|
||||||
FIREBASE_MESSAGING_SENDER_ID=your-sender-id
|
|
||||||
FIREBASE_APP_ID=your-app-id
|
|
||||||
FIREBASE_MEASUREMENT_ID=your-measurement-id
|
|
||||||
FIREBASE_DATABASE_URL=https://your-project.firebaseio.com
|
|
||||||
```
|
|
||||||
|
|
||||||
### Google OAuth
|
|
||||||
|
|
||||||
```
|
|
||||||
GOOGLE_CLIENT_ID=your-google-client-id
|
|
||||||
GOOGLE_CLIENT_SECRET=your-google-client-secret
|
|
||||||
```
|
|
||||||
|
|
||||||
### OneSignal Push Notifications
|
|
||||||
|
|
||||||
```
|
|
||||||
ONESIGNAL_APP_ID=your-onesignal-app-id
|
|
||||||
ONESIGNAL_API_KEY=your-onesignal-api-key
|
|
||||||
```
|
|
||||||
|
|
||||||
### Email Configuration (SMTP)
|
|
||||||
|
|
||||||
```
|
|
||||||
SMTP_HOST=your-smtp-host
|
|
||||||
SMTP_USER=your-smtp-username
|
|
||||||
SMTP_PASSWORD=your-smtp-password
|
|
||||||
```
|
|
||||||
|
|
||||||
### Mailtrap Configuration
|
|
||||||
|
|
||||||
```
|
|
||||||
MAILTRAP_API_TOKEN=your-mailtrap-api-token
|
|
||||||
MAILTRAP_ENABLE_TEMPLATE_UUID=template-uuid
|
|
||||||
MAILTRAP_DISABLE_TEMPLATE_UUID=template-uuid
|
|
||||||
MAILTRAP_INVITATION_TEMPLATE_UUID=template-uuid
|
|
||||||
MAILTRAP_DELETE_USER_TEMPLATE_UUID=template-uuid
|
|
||||||
MAILTRAP_EDIT_USER_TEMPLATE_UUID=template-uuid
|
|
||||||
```
|
|
||||||
|
|
||||||
### Optional Services (leave empty if not used)
|
|
||||||
|
|
||||||
```
|
|
||||||
AZURE_REDIS_CONNECTIONSTRING=your-redis-connection-string
|
|
||||||
DOPPLER_PROJECT=your-doppler-project
|
|
||||||
DOPPLER_CONFIG=your-doppler-config
|
|
||||||
DOPPLER_ENVIRONMENT=your-doppler-environment
|
|
||||||
ACCESS_KEY=your-access-key
|
|
||||||
DOCKER_REGISTRY_SERVER_URL=your-registry-url
|
|
||||||
DOCKER_REGISTRY_SERVER_USERNAME=your-registry-username
|
|
||||||
DOCKER_REGISTRY_SERVER_PASSWORD=your-registry-password
|
|
||||||
```
|
|
||||||
|
|
||||||
## Setup Steps
|
|
||||||
|
|
||||||
1. **Add AWS Credentials**
|
|
||||||
- Create IAM user with ECR, ECS, CloudFormation permissions
|
|
||||||
- Add AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY to GitHub Secrets
|
|
||||||
|
|
||||||
2. **Generate JWT Secrets**
|
|
||||||
- Use a secure random string generator
|
|
||||||
- Make JWT_SECRET and JWT_SECRET_REFRESH different values
|
|
||||||
- Keep these values secure and never share them
|
|
||||||
|
|
||||||
3. **Configure Services**
|
|
||||||
- Add secrets for each service you're using
|
|
||||||
- Leave unused services empty (they'll default to empty strings)
|
|
||||||
|
|
||||||
4. **Test Deployment**
|
|
||||||
- Push to master/main branch
|
|
||||||
- Check GitHub Actions tab for deployment status
|
|
||||||
- Verify API is accessible at https://api.syncos.syncrow.ae
|
|
||||||
|
|
||||||
## Security Notes
|
|
||||||
|
|
||||||
- Never commit secrets to the repository
|
|
||||||
- Use GitHub Secrets for all sensitive values
|
|
||||||
- Rotate secrets regularly
|
|
||||||
- Monitor GitHub Actions logs for any exposed values
|
|
||||||
- Database password is automatically managed by AWS Secrets Manager
|
|
||||||
|
|
||||||
## Troubleshooting
|
|
||||||
|
|
||||||
- Check GitHub Actions logs for deployment errors
|
|
||||||
- Verify all required secrets are set
|
|
||||||
- Ensure AWS credentials have sufficient permissions
|
|
||||||
- Check ECS service logs in CloudWatch for runtime errors
|
|
39
README.md
39
README.md
@ -1,19 +1,17 @@
|
|||||||
# Backend
|
# Backend
|
||||||
|
|
||||||
## Overview
|
## Overview
|
||||||
|
|
||||||
This is the backend for an IoT application built using NestJS. It interfaces with the Tuya IoT cloud platform to manage homes, rooms, devices, ...etc.
|
This is the backend for an IoT application built using NestJS. It interfaces with the Tuya IoT cloud platform to manage homes, rooms, devices, ...etc.
|
||||||
This is the backend APIs project, developed with NestJS for Syncrow IOT Project.
|
This is the backend APIs project, developed with NestJS for Syncrow IOT Project.
|
||||||
|
|
||||||
## Database Model
|
## Database Model
|
||||||
|
|
||||||
The database uses PostgreSQL and TypeORM. Below is an entity relationship diagram:
|
The database uses PostgreSQL and TypeORM. Below is an entity relationship diagram:
|
||||||
|
|
||||||
The main entities are:
|
The main entities are:
|
||||||
|
|
||||||
User - Stores user account information
|
User - Stores user account information
|
||||||
Home - Represents a home/space
|
Home - Represents a home/space
|
||||||
Room - Represents a room/sub-space
|
Room - Represents a room/sub-space
|
||||||
Device - Represents a connected device
|
Device - Represents a connected device
|
||||||
Product - Stores metadata about device products
|
Product - Stores metadata about device products
|
||||||
Other Entities - sessions, OTPs, etc.
|
Other Entities - sessions, OTPs, etc.
|
||||||
@ -21,11 +19,10 @@ Other Entities - sessions, OTPs, etc.
|
|||||||
The entities have a one-to-many relationship - a user has multiple homes, a home has multiple rooms, and a room has multiple devices.
|
The entities have a one-to-many relationship - a user has multiple homes, a home has multiple rooms, and a room has multiple devices.
|
||||||
|
|
||||||
## Architecture
|
## Architecture
|
||||||
|
|
||||||
The application is deployed on Azure App Service using Docker containers. There are separate deployment slots for development, staging, and production environments.
|
The application is deployed on Azure App Service using Docker containers. There are separate deployment slots for development, staging, and production environments.
|
||||||
|
|
||||||
## Installation
|
|
||||||
|
|
||||||
|
## Installation
|
||||||
First, ensure that you have Node.js `v20.11` or newer (LTS ONLY) installed on your system.
|
First, ensure that you have Node.js `v20.11` or newer (LTS ONLY) installed on your system.
|
||||||
|
|
||||||
To install the project dependencies, run the following command in the project root directory:
|
To install the project dependencies, run the following command in the project root directory:
|
||||||
@ -64,8 +61,8 @@ $ npm run test:cov
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
## Architecture
|
|
||||||
|
|
||||||
|
## Architecture
|
||||||
+----------------------------------+
|
+----------------------------------+
|
||||||
| |
|
| |
|
||||||
| Applications |
|
| Applications |
|
||||||
@ -110,29 +107,3 @@ $ npm run test:cov
|
|||||||
| | Standby Node | | |
|
| | Standby Node | | |
|
||||||
| +------------------+----------------+ |
|
| +------------------+----------------+ |
|
||||||
+-----------------------------------------------------------------+
|
+-----------------------------------------------------------------+
|
||||||
|
|
||||||
## CDK Deployment
|
|
||||||
|
|
||||||
• Bootstrap CDK (first time only): npx cdk bootstrap aws://482311766496/me-central-1
|
|
||||||
• List available stacks: npx cdk list
|
|
||||||
• Deploy infrastructure: npx cdk deploy --require-approval never
|
|
||||||
• View changes before deploy: npx cdk diff
|
|
||||||
• Generate CloudFormation template: npx cdk synth
|
|
||||||
• Destroy infrastructure: npx cdk destroy
|
|
||||||
• Environment variables are configured in infrastructure/stack.ts
|
|
||||||
• After code changes: build Docker image, push to ECR, force ECS deployment
|
|
||||||
• Database seeding happens automatically on first deployment with DB_SYNC=true
|
|
||||||
• Admin credentials: admin@syncrow.ae / YourSecureAdminPassword123!
|
|
||||||
• Production API: https://api.syncos.syncrow.ae
|
|
||||||
• Health check: https://api.syncos.syncrow.ae/health
|
|
||||||
|
|
||||||
## GitHub Actions Deployment
|
|
||||||
|
|
||||||
• Automatic deployment on push to master/main branch
|
|
||||||
• Configure GitHub Secrets (see GITHUB_SETUP.md for complete list)
|
|
||||||
• Required secrets: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, JWT_SECRET, JWT_SECRET_REFRESH
|
|
||||||
• Workflow builds Docker image, pushes to ECR, and deploys CDK stack
|
|
||||||
• Environment variables are passed securely via GitHub Secrets
|
|
||||||
• Manual deployment: Go to Actions tab and run "Deploy Backend to AWS" workflow
|
|
||||||
• Check deployment status in GitHub Actions tab
|
|
||||||
• Logs available in CloudWatch under /ecs/syncrow-backend log group
|
|
||||||
|
46
build.sh
46
build.sh
@ -1,46 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
set -e
|
|
||||||
|
|
||||||
ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
|
|
||||||
REGION=${AWS_DEFAULT_REGION:-me-central-1}
|
|
||||||
REPO_NAME=syncrow-backend
|
|
||||||
IMAGE_TAG=latest
|
|
||||||
CLUSTER_NAME=syncrow-backend-cluster
|
|
||||||
STACK_NAME=SyncrowBackendStack
|
|
||||||
CERTIFICATE_ARN="arn:aws:acm:$REGION:$ACCOUNT_ID:certificate/bea1e2ae-84a1-414e-8dbf-4599397e7ed0"
|
|
||||||
|
|
||||||
echo "🔐 Logging into ECR..."
|
|
||||||
aws ecr get-login-password --region $REGION | docker login --username AWS --password-stdin "$ACCOUNT_ID.dkr.ecr.$REGION.amazonaws.com"
|
|
||||||
|
|
||||||
echo "🐳 Building Docker image..."
|
|
||||||
docker build --platform=linux/amd64 -t $REPO_NAME .
|
|
||||||
|
|
||||||
echo "🏷️ Tagging image..."
|
|
||||||
docker tag $REPO_NAME:$IMAGE_TAG "$ACCOUNT_ID.dkr.ecr.$REGION.amazonaws.com/$REPO_NAME:$IMAGE_TAG"
|
|
||||||
|
|
||||||
echo "📤 Pushing image to ECR..."
|
|
||||||
docker push "$ACCOUNT_ID.dkr.ecr.$REGION.amazonaws.com/$REPO_NAME:$IMAGE_TAG"
|
|
||||||
|
|
||||||
echo "🔍 Checking if ECS service exists..."
|
|
||||||
SERVICE_ARN=$(aws ecs list-services \
|
|
||||||
--cluster $CLUSTER_NAME \
|
|
||||||
--query 'serviceArns[0]' \
|
|
||||||
--output text \
|
|
||||||
--region $REGION 2>/dev/null || echo "")
|
|
||||||
|
|
||||||
echo "📦 Deploying CDK Stack..."
|
|
||||||
npx cdk deploy $STACK_NAME \
|
|
||||||
--context certificateArn=$CERTIFICATE_ARN \
|
|
||||||
--require-approval never
|
|
||||||
|
|
||||||
if [[ "$SERVICE_ARN" != "" && "$SERVICE_ARN" != "None" ]]; then
|
|
||||||
SERVICE_NAME=$(basename "$SERVICE_ARN")
|
|
||||||
echo "🚀 Redeploying ECS Service: $SERVICE_NAME"
|
|
||||||
aws ecs update-service \
|
|
||||||
--cluster $CLUSTER_NAME \
|
|
||||||
--service $SERVICE_NAME \
|
|
||||||
--force-new-deployment \
|
|
||||||
--region $REGION
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "✅ All done."
|
|
@ -1,29 +0,0 @@
|
|||||||
{
|
|
||||||
"availability-zones:account=426265406140:region=us-east-2": [
|
|
||||||
"us-east-2a",
|
|
||||||
"us-east-2b",
|
|
||||||
"us-east-2c"
|
|
||||||
],
|
|
||||||
"availability-zones:account=482311766496:region=us-east-2": [
|
|
||||||
"us-east-2a",
|
|
||||||
"us-east-2b",
|
|
||||||
"us-east-2c"
|
|
||||||
],
|
|
||||||
"hosted-zone:account=482311766496:domainName=syncrow.me:region=us-east-2": {
|
|
||||||
"Id": "/hostedzone/Z02085662NLJECF4DGJV3",
|
|
||||||
"Name": "syncrow.me."
|
|
||||||
},
|
|
||||||
"availability-zones:account=482311766496:region=me-central-1": [
|
|
||||||
"me-central-1a",
|
|
||||||
"me-central-1b",
|
|
||||||
"me-central-1c"
|
|
||||||
],
|
|
||||||
"hosted-zone:account=482311766496:domainName=syncrow.me:region=me-central-1": {
|
|
||||||
"Id": "/hostedzone/Z02085662NLJECF4DGJV3",
|
|
||||||
"Name": "syncrow.me."
|
|
||||||
},
|
|
||||||
"hosted-zone:account=482311766496:domainName=syncrow.ae:region=me-central-1": {
|
|
||||||
"Id": "/hostedzone/Z01153152LRHQTA1370P4",
|
|
||||||
"Name": "syncrow.ae."
|
|
||||||
}
|
|
||||||
}
|
|
58
cdk.json
58
cdk.json
@ -1,58 +0,0 @@
|
|||||||
{
|
|
||||||
"app": "npx ts-node --prefer-ts-exts infrastructure/app.ts",
|
|
||||||
"watch": {
|
|
||||||
"include": [
|
|
||||||
"**"
|
|
||||||
],
|
|
||||||
"exclude": [
|
|
||||||
"README.md",
|
|
||||||
"cdk*.json",
|
|
||||||
"**/*.d.ts",
|
|
||||||
"**/*.js",
|
|
||||||
"tsconfig.json",
|
|
||||||
"package*.json",
|
|
||||||
"yarn.lock",
|
|
||||||
"node_modules",
|
|
||||||
"test"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"context": {
|
|
||||||
"@aws-cdk/aws-lambda:recognizeLayerVersion": true,
|
|
||||||
"@aws-cdk/core:checkSecretUsage": true,
|
|
||||||
"@aws-cdk/core:target-partitions": [
|
|
||||||
"aws",
|
|
||||||
"aws-cn"
|
|
||||||
],
|
|
||||||
"@aws-cdk-containers/ecs-service-extensions:enableDefaultLogDriver": true,
|
|
||||||
"@aws-cdk/aws-ec2:uniqueImdsv2TemplateName": true,
|
|
||||||
"@aws-cdk/aws-ecs:arnFormatIncludesClusterName": true,
|
|
||||||
"@aws-cdk/aws-iam:minimizePolicies": true,
|
|
||||||
"@aws-cdk/core:validateSnapshotRemovalPolicy": true,
|
|
||||||
"@aws-cdk/aws-codepipeline:crossAccountKeyAliasStackSafeResourceName": true,
|
|
||||||
"@aws-cdk/aws-s3:createDefaultLoggingPolicy": true,
|
|
||||||
"@aws-cdk/aws-sns-subscriptions:restrictSqsDescryption": true,
|
|
||||||
"@aws-cdk/aws-apigateway:disableCloudWatchRole": true,
|
|
||||||
"@aws-cdk/core:enablePartitionLiterals": true,
|
|
||||||
"@aws-cdk/aws-events:eventsTargetQueueSameAccount": true,
|
|
||||||
"@aws-cdk/aws-iam:standardizedServicePrincipals": true,
|
|
||||||
"@aws-cdk/aws-ecs:disableExplicitDeploymentControllerForCircuitBreaker": true,
|
|
||||||
"@aws-cdk/aws-iam:importedRoleStackSafeDefaultPolicyName": true,
|
|
||||||
"@aws-cdk/aws-s3:serverAccessLogsUseBucketPolicy": true,
|
|
||||||
"@aws-cdk/aws-route53-patters:useCertificate": true,
|
|
||||||
"@aws-cdk/customresources:installLatestAwsSdkDefault": false,
|
|
||||||
"@aws-cdk/aws-rds:databaseProxyUniqueResourceName": true,
|
|
||||||
"@aws-cdk/aws-codedeploy:removeAlarmsFromDeploymentGroup": true,
|
|
||||||
"@aws-cdk/aws-apigateway:authorizerChangeDeploymentLogicalId": true,
|
|
||||||
"@aws-cdk/aws-ec2:launchTemplateDefaultUserData": true,
|
|
||||||
"@aws-cdk/aws-secretsmanager:useAttachedSecretResourcePolicyForSecretTargetAttachments": true,
|
|
||||||
"@aws-cdk/aws-redshift:columnId": true,
|
|
||||||
"@aws-cdk/aws-stepfunctions-tasks:enableLogging": true,
|
|
||||||
"@aws-cdk/aws-ec2:restrictDefaultSecurityGroup": true,
|
|
||||||
"@aws-cdk/aws-apigateway:requestValidatorUniqueId": true,
|
|
||||||
"@aws-cdk/aws-kms:aliasNameRef": true,
|
|
||||||
"@aws-cdk/aws-autoscaling:generateLaunchTemplateInsteadOfLaunchConfig": true,
|
|
||||||
"@aws-cdk/aws-ecs:removeDefaultDeploymentAlarm": true,
|
|
||||||
"@aws-cdk/aws-rds:preventRenderingDeprecatedCredentials": true,
|
|
||||||
"@aws-cdk/aws-codepipeline-actions:useNewDefaultBranchForSourceAction": true
|
|
||||||
}
|
|
||||||
}
|
|
22
deploy.sh
22
deploy.sh
@ -1,22 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
set -e
|
|
||||||
|
|
||||||
ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
|
|
||||||
REGION=${AWS_DEFAULT_REGION:-me-central-1}
|
|
||||||
|
|
||||||
npx cdk deploy SyncrowBackendStack --context certificateArn=arn:aws:acm:me-central-1:482311766496:certificate/bea1e2ae-84a1-414e-8dbf-4599397e7ed0 --require-approval never
|
|
||||||
|
|
||||||
aws ecr get-login-password --region $REGION | docker login --username AWS --password-stdin $ACCOUNT_ID.dkr.ecr.$REGION.amazonaws.com
|
|
||||||
|
|
||||||
docker build --platform=linux/amd64 -t syncrow-backend .
|
|
||||||
docker tag syncrow-backend:latest $ACCOUNT_ID.dkr.ecr.$REGION.amazonaws.com/syncrow-backend:latest
|
|
||||||
docker push $ACCOUNT_ID.dkr.ecr.$REGION.amazonaws.com/syncrow-backend:latest
|
|
||||||
|
|
||||||
SERVICE_ARN=$(aws ecs list-services --cluster syncrow-backend-cluster --query 'serviceArns[0]' --output text --region $REGION 2>/dev/null || echo "")
|
|
||||||
|
|
||||||
if [ "$SERVICE_ARN" != "" ] && [ "$SERVICE_ARN" != "None" ]; then
|
|
||||||
SERVICE_NAME=$(echo $SERVICE_ARN | cut -d'/' -f3)
|
|
||||||
aws ecs update-service --cluster syncrow-backend-cluster --service $SERVICE_NAME --force-new-deployment --region $REGION
|
|
||||||
else
|
|
||||||
npx cdk deploy SyncrowBackendStack --context certificateArn=arn:aws:acm:me-central-1:482311766496:certificate/bea1e2ae-84a1-414e-8dbf-4599397e7ed0 --require-approval never
|
|
||||||
fi
|
|
@ -1,16 +0,0 @@
|
|||||||
#!/usr/bin/env node
|
|
||||||
import * as cdk from 'aws-cdk-lib';
|
|
||||||
import 'source-map-support/register';
|
|
||||||
import { BackendStack } from './stack';
|
|
||||||
|
|
||||||
const app = new cdk.App();
|
|
||||||
|
|
||||||
new BackendStack(app, 'SyncrowBackendStack', {
|
|
||||||
env: {
|
|
||||||
account: process.env.CDK_DEFAULT_ACCOUNT,
|
|
||||||
region: 'me-central-1',
|
|
||||||
},
|
|
||||||
databaseName: 'postgres',
|
|
||||||
certificateArn:
|
|
||||||
'arn:aws:acm:me-central-1:482311766496:certificate/423b343e-402b-4978-89bd-cda25f7a8873',
|
|
||||||
});
|
|
@ -1,393 +0,0 @@
|
|||||||
import * as cdk from 'aws-cdk-lib';
|
|
||||||
import * as acm from 'aws-cdk-lib/aws-certificatemanager';
|
|
||||||
import * as ec2 from 'aws-cdk-lib/aws-ec2';
|
|
||||||
import * as ecr from 'aws-cdk-lib/aws-ecr';
|
|
||||||
import * as ecs from 'aws-cdk-lib/aws-ecs';
|
|
||||||
import * as ecsPatterns from 'aws-cdk-lib/aws-ecs-patterns';
|
|
||||||
import * as elbv2 from 'aws-cdk-lib/aws-elasticloadbalancingv2';
|
|
||||||
import * as logs from 'aws-cdk-lib/aws-logs';
|
|
||||||
import * as rds from 'aws-cdk-lib/aws-rds';
|
|
||||||
import * as route53 from 'aws-cdk-lib/aws-route53';
|
|
||||||
import { Construct } from 'constructs';
|
|
||||||
import * as dotenv from 'dotenv';
|
|
||||||
|
|
||||||
export interface BackendStackProps extends cdk.StackProps {
|
|
||||||
vpcId?: string;
|
|
||||||
databaseName?: string;
|
|
||||||
certificateArn?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export class BackendStack extends cdk.Stack {
|
|
||||||
public readonly apiUrl: string;
|
|
||||||
public readonly databaseEndpoint: string;
|
|
||||||
public readonly vpc: ec2.IVpc;
|
|
||||||
|
|
||||||
constructor(scope: Construct, id: string, props?: BackendStackProps) {
|
|
||||||
super(scope, id, props);
|
|
||||||
|
|
||||||
// Load environment variables from .env file
|
|
||||||
dotenv.config({ path: '.env' });
|
|
||||||
|
|
||||||
// VPC - either use existing or create new
|
|
||||||
this.vpc = props?.vpcId
|
|
||||||
? ec2.Vpc.fromLookup(this, 'ExistingVpc', { vpcId: props.vpcId })
|
|
||||||
: new ec2.Vpc(this, 'SyncrowVpc', {
|
|
||||||
maxAzs: 2,
|
|
||||||
natGateways: 1,
|
|
||||||
subnetConfiguration: [
|
|
||||||
{
|
|
||||||
cidrMask: 24,
|
|
||||||
name: 'public',
|
|
||||||
subnetType: ec2.SubnetType.PUBLIC,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
cidrMask: 24,
|
|
||||||
name: 'private',
|
|
||||||
subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
// Security Groups
|
|
||||||
const dbSecurityGroup = new ec2.SecurityGroup(
|
|
||||||
this,
|
|
||||||
'DatabaseSecurityGroup',
|
|
||||||
{
|
|
||||||
vpc: this.vpc,
|
|
||||||
description: 'Security group for RDS PostgreSQL',
|
|
||||||
allowAllOutbound: false,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
const ecsSecurityGroup = new ec2.SecurityGroup(this, 'EcsSecurityGroup', {
|
|
||||||
vpc: this.vpc,
|
|
||||||
description: 'Security group for ECS Fargate service',
|
|
||||||
allowAllOutbound: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
const albSecurityGroup = new ec2.SecurityGroup(this, 'AlbSecurityGroup', {
|
|
||||||
vpc: this.vpc,
|
|
||||||
description: 'Security group for Application Load Balancer',
|
|
||||||
allowAllOutbound: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Allow ALB to connect to ECS
|
|
||||||
ecsSecurityGroup.addIngressRule(
|
|
||||||
albSecurityGroup,
|
|
||||||
ec2.Port.tcp(3000),
|
|
||||||
'Allow ALB to connect to ECS service',
|
|
||||||
);
|
|
||||||
|
|
||||||
// Allow ECS to connect to RDS
|
|
||||||
dbSecurityGroup.addIngressRule(
|
|
||||||
ecsSecurityGroup,
|
|
||||||
ec2.Port.tcp(5432),
|
|
||||||
'Allow ECS to connect to PostgreSQL',
|
|
||||||
);
|
|
||||||
|
|
||||||
// Temporary access for admin IP
|
|
||||||
dbSecurityGroup.addIngressRule(
|
|
||||||
ec2.Peer.ipv4('216.126.231.231/32'),
|
|
||||||
ec2.Port.tcp(5432),
|
|
||||||
'Temporary access from admin IP',
|
|
||||||
);
|
|
||||||
|
|
||||||
// Allow HTTP/HTTPS traffic to ALB
|
|
||||||
albSecurityGroup.addIngressRule(
|
|
||||||
ec2.Peer.anyIpv4(),
|
|
||||||
ec2.Port.tcp(80),
|
|
||||||
'Allow HTTP traffic',
|
|
||||||
);
|
|
||||||
albSecurityGroup.addIngressRule(
|
|
||||||
ec2.Peer.anyIpv4(),
|
|
||||||
ec2.Port.tcp(443),
|
|
||||||
'Allow HTTPS traffic',
|
|
||||||
);
|
|
||||||
|
|
||||||
const dbCluster = rds.DatabaseCluster.fromDatabaseClusterAttributes(
|
|
||||||
this,
|
|
||||||
'SyncrowDatabase',
|
|
||||||
{
|
|
||||||
clusterIdentifier: 'syncrow-backend',
|
|
||||||
instanceIdentifiers: ['syncrowdatabase-instance-1'],
|
|
||||||
engine: rds.DatabaseClusterEngine.auroraPostgres({
|
|
||||||
version: rds.AuroraPostgresEngineVersion.VER_16_6,
|
|
||||||
}),
|
|
||||||
port: 5432,
|
|
||||||
securityGroups: [
|
|
||||||
ec2.SecurityGroup.fromSecurityGroupId(
|
|
||||||
this,
|
|
||||||
'ImportedDbSecurityGroup',
|
|
||||||
'sg-07e163f588b2bac25',
|
|
||||||
),
|
|
||||||
],
|
|
||||||
clusterEndpointAddress:
|
|
||||||
'syncrow-backend.cluster-criskv1sdkq4.me-central-1.rds.amazonaws.com',
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
// Import the existing database secret separately
|
|
||||||
const dbSecret = rds.DatabaseSecret.fromSecretCompleteArn(
|
|
||||||
this,
|
|
||||||
'ImportedDbSecret',
|
|
||||||
'arn:aws:secretsmanager:me-central-1:482311766496:secret:rds!cluster-43ec14cd-9301-43e2-aa79-d330a429a126-v0JDQN',
|
|
||||||
);
|
|
||||||
|
|
||||||
// ECR Repository for Docker images - import existing repository
|
|
||||||
const ecrRepository = ecr.Repository.fromRepositoryName(
|
|
||||||
this,
|
|
||||||
'SyncrowBackendRepo',
|
|
||||||
'syncrow-backend',
|
|
||||||
);
|
|
||||||
|
|
||||||
// Output the correct ECR URI for this region
|
|
||||||
new cdk.CfnOutput(this, 'EcrRepositoryUriRegional', {
|
|
||||||
value: ecrRepository.repositoryUri,
|
|
||||||
description: `ECR Repository URI in region ${this.region}`,
|
|
||||||
exportName: `${this.stackName}-EcrRepositoryUriRegional`,
|
|
||||||
});
|
|
||||||
|
|
||||||
// ECS Cluster
|
|
||||||
const cluster = new ecs.Cluster(this, 'SyncrowCluster', {
|
|
||||||
vpc: this.vpc,
|
|
||||||
clusterName: 'syncrow-backend-cluster',
|
|
||||||
});
|
|
||||||
|
|
||||||
// CloudWatch Log Group
|
|
||||||
const logGroup = new logs.LogGroup(this, 'SyncrowBackendLogs', {
|
|
||||||
logGroupName: '/ecs/syncrow-backend',
|
|
||||||
retention: logs.RetentionDays.ONE_WEEK,
|
|
||||||
removalPolicy: cdk.RemovalPolicy.DESTROY,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Use existing wildcard certificate or create new one
|
|
||||||
const apiCertificate = props?.certificateArn
|
|
||||||
? acm.Certificate.fromCertificateArn(
|
|
||||||
this,
|
|
||||||
'ApiCertificate',
|
|
||||||
props.certificateArn,
|
|
||||||
)
|
|
||||||
: new acm.Certificate(this, 'ApiCertificate', {
|
|
||||||
domainName: 'api.syncos.syncrow.ae',
|
|
||||||
validation: acm.CertificateValidation.fromDns(),
|
|
||||||
});
|
|
||||||
|
|
||||||
// ECS Fargate Service with Application Load Balancer
|
|
||||||
const fargateService =
|
|
||||||
new ecsPatterns.ApplicationLoadBalancedFargateService(
|
|
||||||
this,
|
|
||||||
'SyncrowBackendService',
|
|
||||||
{
|
|
||||||
cluster,
|
|
||||||
memoryLimitMiB: 1024,
|
|
||||||
cpu: 512,
|
|
||||||
desiredCount: 2,
|
|
||||||
domainName: 'api.syncos.syncrow.ae',
|
|
||||||
domainZone: route53.HostedZone.fromLookup(this, 'SyncrowZone', {
|
|
||||||
domainName: 'syncrow.ae',
|
|
||||||
}),
|
|
||||||
certificate: apiCertificate,
|
|
||||||
protocol: elbv2.ApplicationProtocol.HTTPS,
|
|
||||||
redirectHTTP: true,
|
|
||||||
taskImageOptions: {
|
|
||||||
image: ecs.ContainerImage.fromEcrRepository(
|
|
||||||
ecrRepository,
|
|
||||||
'latest',
|
|
||||||
),
|
|
||||||
containerPort: 3000,
|
|
||||||
enableLogging: true,
|
|
||||||
environment: {
|
|
||||||
// App settings
|
|
||||||
NODE_ENV: process.env.NODE_ENV || 'production',
|
|
||||||
PORT: process.env.PORT || '3000',
|
|
||||||
BASE_URL: process.env.BASE_URL || '',
|
|
||||||
|
|
||||||
// Database connection (CDK provides these automatically)
|
|
||||||
AZURE_POSTGRESQL_HOST: dbCluster.clusterEndpoint.hostname,
|
|
||||||
AZURE_POSTGRESQL_PORT: '5432',
|
|
||||||
AZURE_POSTGRESQL_DATABASE: props?.databaseName || 'postgres',
|
|
||||||
AZURE_POSTGRESQL_USER: 'postgres',
|
|
||||||
AZURE_POSTGRESQL_SSL: process.env.AZURE_POSTGRESQL_SSL || 'false',
|
|
||||||
AZURE_POSTGRESQL_SYNC:
|
|
||||||
process.env.AZURE_POSTGRESQL_SYNC || 'false',
|
|
||||||
|
|
||||||
// JWT Configuration - CRITICAL: These must be set
|
|
||||||
JWT_SECRET:
|
|
||||||
process.env.JWT_SECRET ||
|
|
||||||
'syncrow-jwt-secret-key-2025-production-environment-very-secure-random-string',
|
|
||||||
JWT_SECRET_REFRESH:
|
|
||||||
process.env.JWT_SECRET_REFRESH ||
|
|
||||||
'syncrow-refresh-secret-key-2025-production-environment-different-secure-string',
|
|
||||||
JWT_EXPIRE_TIME: process.env.JWT_EXPIRE_TIME || '1h',
|
|
||||||
JWT_EXPIRE_TIME_REFRESH:
|
|
||||||
process.env.JWT_EXPIRE_TIME_REFRESH || '7d',
|
|
||||||
|
|
||||||
// Firebase Configuration
|
|
||||||
FIREBASE_API_KEY: process.env.FIREBASE_API_KEY || '',
|
|
||||||
FIREBASE_AUTH_DOMAIN: process.env.FIREBASE_AUTH_DOMAIN || '',
|
|
||||||
FIREBASE_PROJECT_ID: process.env.FIREBASE_PROJECT_ID || '',
|
|
||||||
FIREBASE_STORAGE_BUCKET:
|
|
||||||
process.env.FIREBASE_STORAGE_BUCKET || '',
|
|
||||||
FIREBASE_MESSAGING_SENDER_ID:
|
|
||||||
process.env.FIREBASE_MESSAGING_SENDER_ID || '',
|
|
||||||
FIREBASE_APP_ID: process.env.FIREBASE_APP_ID || '',
|
|
||||||
FIREBASE_MEASUREMENT_ID:
|
|
||||||
process.env.FIREBASE_MEASUREMENT_ID || '',
|
|
||||||
FIREBASE_DATABASE_URL: process.env.FIREBASE_DATABASE_URL || '',
|
|
||||||
|
|
||||||
// Tuya IoT Configuration
|
|
||||||
TUYA_EU_URL:
|
|
||||||
process.env.TUYA_EU_URL || 'https://openapi.tuyaeu.com',
|
|
||||||
TUYA_ACCESS_ID: process.env.TUYA_ACCESS_ID || '',
|
|
||||||
TUYA_ACCESS_KEY: process.env.TUYA_ACCESS_KEY || '',
|
|
||||||
TRUN_ON_TUYA_SOCKET: process.env.TRUN_ON_TUYA_SOCKET || '',
|
|
||||||
|
|
||||||
// Email Configuration
|
|
||||||
SMTP_HOST: process.env.SMTP_HOST || '',
|
|
||||||
SMTP_PORT: process.env.SMTP_PORT || '587',
|
|
||||||
SMTP_SECURE: process.env.SMTP_SECURE || 'true',
|
|
||||||
SMTP_USER: process.env.SMTP_USER || '',
|
|
||||||
SMTP_PASSWORD: process.env.SMTP_PASSWORD || '',
|
|
||||||
|
|
||||||
// Mailtrap Configuration
|
|
||||||
MAILTRAP_API_TOKEN: process.env.MAILTRAP_API_TOKEN || '',
|
|
||||||
MAILTRAP_INVITATION_TEMPLATE_UUID:
|
|
||||||
process.env.MAILTRAP_INVITATION_TEMPLATE_UUID || '',
|
|
||||||
MAILTRAP_EDIT_USER_TEMPLATE_UUID:
|
|
||||||
process.env.MAILTRAP_EDIT_USER_TEMPLATE_UUID || '',
|
|
||||||
MAILTRAP_DISABLE_TEMPLATE_UUID:
|
|
||||||
process.env.MAILTRAP_DISABLE_TEMPLATE_UUID || '',
|
|
||||||
MAILTRAP_ENABLE_TEMPLATE_UUID:
|
|
||||||
process.env.MAILTRAP_ENABLE_TEMPLATE_UUID || '',
|
|
||||||
MAILTRAP_DELETE_USER_TEMPLATE_UUID:
|
|
||||||
process.env.MAILTRAP_DELETE_USER_TEMPLATE_UUID || '',
|
|
||||||
|
|
||||||
// OneSignal Push Notifications
|
|
||||||
ONESIGNAL_APP_ID: process.env.ONESIGNAL_APP_ID || '',
|
|
||||||
ONESIGNAL_API_KEY: process.env.ONESIGNAL_API_KEY || '',
|
|
||||||
|
|
||||||
// Admin Configuration
|
|
||||||
SUPER_ADMIN_EMAIL:
|
|
||||||
process.env.SUPER_ADMIN_EMAIL || 'admin@yourdomain.com',
|
|
||||||
SUPER_ADMIN_PASSWORD:
|
|
||||||
process.env.SUPER_ADMIN_PASSWORD ||
|
|
||||||
'YourSecureAdminPassword123!',
|
|
||||||
|
|
||||||
// Google OAuth
|
|
||||||
GOOGLE_CLIENT_ID: process.env.GOOGLE_CLIENT_ID || '',
|
|
||||||
GOOGLE_CLIENT_SECRET: process.env.GOOGLE_CLIENT_SECRET || '',
|
|
||||||
|
|
||||||
// Other Configuration
|
|
||||||
OTP_LIMITER: process.env.OTP_LIMITER || '5',
|
|
||||||
SECRET_KEY:
|
|
||||||
process.env.SECRET_KEY ||
|
|
||||||
'another-random-secret-key-for-general-encryption',
|
|
||||||
ACCESS_KEY: process.env.ACCESS_KEY || '',
|
|
||||||
DB_SYNC: process.env.DB_SYNC || 'txsrue',
|
|
||||||
|
|
||||||
// Redis (used?)
|
|
||||||
AZURE_REDIS_CONNECTIONSTRING:
|
|
||||||
process.env.AZURE_REDIS_CONNECTIONSTRING || '',
|
|
||||||
|
|
||||||
// Docker Registry (for deployment)
|
|
||||||
DOCKER_REGISTRY_SERVER_URL:
|
|
||||||
process.env.DOCKER_REGISTRY_SERVER_URL || '',
|
|
||||||
DOCKER_REGISTRY_SERVER_USERNAME:
|
|
||||||
process.env.DOCKER_REGISTRY_SERVER_USERNAME || '',
|
|
||||||
DOCKER_REGISTRY_SERVER_PASSWORD:
|
|
||||||
process.env.DOCKER_REGISTRY_SERVER_PASSWORD || '',
|
|
||||||
|
|
||||||
// Doppler (if used for secrets management)
|
|
||||||
DOPPLER_PROJECT: process.env.DOPPLER_PROJECT || '',
|
|
||||||
DOPPLER_CONFIG: process.env.DOPPLER_CONFIG || '',
|
|
||||||
DOPPLER_ENVIRONMENT: process.env.DOPPLER_ENVIRONMENT || '',
|
|
||||||
|
|
||||||
// Azure specific
|
|
||||||
WEBSITES_ENABLE_APP_SERVICE_STORAGE:
|
|
||||||
process.env.WEBSITES_ENABLE_APP_SERVICE_STORAGE || 'false',
|
|
||||||
},
|
|
||||||
secrets: {
|
|
||||||
AZURE_POSTGRESQL_PASSWORD: ecs.Secret.fromSecretsManager(
|
|
||||||
dbSecret,
|
|
||||||
'password',
|
|
||||||
),
|
|
||||||
},
|
|
||||||
logDriver: ecs.LogDrivers.awsLogs({
|
|
||||||
streamPrefix: 'syncrow-backend',
|
|
||||||
logGroup,
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
publicLoadBalancer: true,
|
|
||||||
securityGroups: [ecsSecurityGroup],
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
// Add security group to load balancer after creation
|
|
||||||
fargateService.loadBalancer.addSecurityGroup(albSecurityGroup);
|
|
||||||
|
|
||||||
// Configure health check
|
|
||||||
fargateService.targetGroup.configureHealthCheck({
|
|
||||||
path: '/health',
|
|
||||||
healthyHttpCodes: '200',
|
|
||||||
interval: cdk.Duration.seconds(30),
|
|
||||||
timeout: cdk.Duration.seconds(5),
|
|
||||||
healthyThresholdCount: 2,
|
|
||||||
unhealthyThresholdCount: 3,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Auto Scaling
|
|
||||||
const scalableTarget = fargateService.service.autoScaleTaskCount({
|
|
||||||
minCapacity: 1,
|
|
||||||
maxCapacity: 10,
|
|
||||||
});
|
|
||||||
|
|
||||||
scalableTarget.scaleOnCpuUtilization('CpuScaling', {
|
|
||||||
targetUtilizationPercent: 70,
|
|
||||||
scaleInCooldown: cdk.Duration.minutes(5),
|
|
||||||
scaleOutCooldown: cdk.Duration.minutes(2),
|
|
||||||
});
|
|
||||||
|
|
||||||
scalableTarget.scaleOnMemoryUtilization('MemoryScaling', {
|
|
||||||
targetUtilizationPercent: 80,
|
|
||||||
scaleInCooldown: cdk.Duration.minutes(5),
|
|
||||||
scaleOutCooldown: cdk.Duration.minutes(2),
|
|
||||||
});
|
|
||||||
|
|
||||||
// Grant ECS task access to RDS credentials
|
|
||||||
dbSecret.grantRead(fargateService.taskDefinition.taskRole);
|
|
||||||
|
|
||||||
this.apiUrl = 'https://api.syncos.syncrow.ae';
|
|
||||||
this.databaseEndpoint = dbCluster.clusterEndpoint.hostname;
|
|
||||||
|
|
||||||
// Outputs
|
|
||||||
new cdk.CfnOutput(this, 'ApiUrl', {
|
|
||||||
value: this.apiUrl,
|
|
||||||
description: 'Application Load Balancer URL',
|
|
||||||
exportName: `${this.stackName}-ApiUrl`,
|
|
||||||
});
|
|
||||||
|
|
||||||
new cdk.CfnOutput(this, 'DatabaseEndpoint', {
|
|
||||||
value: this.databaseEndpoint,
|
|
||||||
description: 'RDS Cluster Endpoint',
|
|
||||||
exportName: `${this.stackName}-DatabaseEndpoint`,
|
|
||||||
});
|
|
||||||
|
|
||||||
new cdk.CfnOutput(this, 'EcrRepositoryUri', {
|
|
||||||
value: ecrRepository.repositoryUri,
|
|
||||||
description: 'ECR Repository URI',
|
|
||||||
exportName: `${this.stackName}-EcrRepositoryUri`,
|
|
||||||
});
|
|
||||||
|
|
||||||
new cdk.CfnOutput(this, 'ClusterName', {
|
|
||||||
value: cluster.clusterName,
|
|
||||||
description: 'ECS Cluster Name',
|
|
||||||
exportName: `${this.stackName}-ClusterName`,
|
|
||||||
});
|
|
||||||
|
|
||||||
new cdk.CfnOutput(this, 'ServiceName', {
|
|
||||||
value: fargateService.service.serviceName,
|
|
||||||
description: 'ECS Service Name',
|
|
||||||
exportName: `${this.stackName}-ServiceName`,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +1,6 @@
|
|||||||
import { PlatformType } from '@app/common/constants/platform-type.enum';
|
import { PlatformType } from '@app/common/constants/platform-type.enum';
|
||||||
import { RoleType } from '@app/common/constants/role.type.enum';
|
import { RoleType } from '@app/common/constants/role.type.enum';
|
||||||
|
import { UserEntity } from '@app/common/modules/user/entities';
|
||||||
import {
|
import {
|
||||||
BadRequestException,
|
BadRequestException,
|
||||||
Injectable,
|
Injectable,
|
||||||
@ -32,7 +33,7 @@ export class AuthService {
|
|||||||
pass: string,
|
pass: string,
|
||||||
regionUuid?: string,
|
regionUuid?: string,
|
||||||
platform?: PlatformType,
|
platform?: PlatformType,
|
||||||
): Promise<any> {
|
): Promise<Omit<UserEntity, 'password'>> {
|
||||||
const user = await this.userRepository.findOne({
|
const user = await this.userRepository.findOne({
|
||||||
where: {
|
where: {
|
||||||
email,
|
email,
|
||||||
@ -70,8 +71,9 @@ export class AuthService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
const { password, ...result } = user;
|
// const { password, ...result } = user;
|
||||||
return result;
|
delete user.password;
|
||||||
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
async createSession(data): Promise<UserSessionEntity> {
|
async createSession(data): Promise<UserSessionEntity> {
|
||||||
@ -114,6 +116,7 @@ export class AuthService {
|
|||||||
hasAcceptedWebAgreement: user.hasAcceptedWebAgreement,
|
hasAcceptedWebAgreement: user.hasAcceptedWebAgreement,
|
||||||
hasAcceptedAppAgreement: user.hasAcceptedAppAgreement,
|
hasAcceptedAppAgreement: user.hasAcceptedAppAgreement,
|
||||||
project: user?.project,
|
project: user?.project,
|
||||||
|
bookingPoints: user?.bookingPoints,
|
||||||
};
|
};
|
||||||
if (payload.googleCode) {
|
if (payload.googleCode) {
|
||||||
const profile = await this.getProfile(payload.googleCode);
|
const profile = await this.getProfile(payload.googleCode);
|
||||||
|
@ -1,12 +1,11 @@
|
|||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
|
import { ConfigModule } from '@nestjs/config';
|
||||||
|
import { ErrorMessageService } from 'src/error-message/error-message.service';
|
||||||
|
import { AuthModule } from './auth/auth.module';
|
||||||
import { CommonService } from './common.service';
|
import { CommonService } from './common.service';
|
||||||
|
import config from './config';
|
||||||
import { DatabaseModule } from './database/database.module';
|
import { DatabaseModule } from './database/database.module';
|
||||||
import { HelperModule } from './helper/helper.module';
|
import { HelperModule } from './helper/helper.module';
|
||||||
import { AuthModule } from './auth/auth.module';
|
|
||||||
import { ConfigModule } from '@nestjs/config';
|
|
||||||
import config from './config';
|
|
||||||
import { EmailService } from './util/email.service';
|
|
||||||
import { ErrorMessageService } from 'src/error-message/error-message.service';
|
|
||||||
import { TuyaService } from './integrations/tuya/services/tuya.service';
|
import { TuyaService } from './integrations/tuya/services/tuya.service';
|
||||||
import { SceneDeviceRepository } from './modules/scene-device/repositories';
|
import { SceneDeviceRepository } from './modules/scene-device/repositories';
|
||||||
import { SpaceRepository } from './modules/space';
|
import { SpaceRepository } from './modules/space';
|
||||||
@ -15,6 +14,7 @@ import {
|
|||||||
SubspaceModelRepository,
|
SubspaceModelRepository,
|
||||||
} from './modules/space-model';
|
} from './modules/space-model';
|
||||||
import { SubspaceRepository } from './modules/space/repositories/subspace.repository';
|
import { SubspaceRepository } from './modules/space/repositories/subspace.repository';
|
||||||
|
import { EmailService } from './util/email/email.service';
|
||||||
@Module({
|
@Module({
|
||||||
providers: [
|
providers: [
|
||||||
CommonService,
|
CommonService,
|
||||||
|
@ -10,6 +10,8 @@ export default registerAs(
|
|||||||
SMTP_USER: process.env.SMTP_USER,
|
SMTP_USER: process.env.SMTP_USER,
|
||||||
SMTP_SENDER: process.env.SMTP_SENDER,
|
SMTP_SENDER: process.env.SMTP_SENDER,
|
||||||
SMTP_PASSWORD: process.env.SMTP_PASSWORD,
|
SMTP_PASSWORD: process.env.SMTP_PASSWORD,
|
||||||
|
BATCH_EMAIL_API_URL: process.env.BATCH_EMAIL_API_URL,
|
||||||
|
SEND_EMAIL_API_URL: process.env.SEND_EMAIL_API_URL,
|
||||||
MAILTRAP_API_TOKEN: process.env.MAILTRAP_API_TOKEN,
|
MAILTRAP_API_TOKEN: process.env.MAILTRAP_API_TOKEN,
|
||||||
MAILTRAP_INVITATION_TEMPLATE_UUID:
|
MAILTRAP_INVITATION_TEMPLATE_UUID:
|
||||||
process.env.MAILTRAP_INVITATION_TEMPLATE_UUID,
|
process.env.MAILTRAP_INVITATION_TEMPLATE_UUID,
|
||||||
@ -21,5 +23,9 @@ export default registerAs(
|
|||||||
process.env.MAILTRAP_EDIT_USER_TEMPLATE_UUID,
|
process.env.MAILTRAP_EDIT_USER_TEMPLATE_UUID,
|
||||||
MAILTRAP_SEND_OTP_TEMPLATE_UUID:
|
MAILTRAP_SEND_OTP_TEMPLATE_UUID:
|
||||||
process.env.MAILTRAP_SEND_OTP_TEMPLATE_UUID,
|
process.env.MAILTRAP_SEND_OTP_TEMPLATE_UUID,
|
||||||
|
MAILTRAP_SEND_BOOKING_AVAILABILITY_UPDATE_TEMPLATE_UUID:
|
||||||
|
process.env.MAILTRAP_SEND_BOOKING_AVAILABILITY_UPDATE_TEMPLATE_UUID,
|
||||||
|
MAILTRAP_SEND_BOOKING_TIMING_UPDATE_TEMPLATE_UUID:
|
||||||
|
process.env.MAILTRAP_SEND_BOOKING_TIMING_UPDATE_TEMPLATE_UUID,
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
@ -69,7 +69,47 @@ export class ControllerRoute {
|
|||||||
'Retrieve the list of all regions registered in Syncrow.';
|
'Retrieve the list of all regions registered in Syncrow.';
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
static BOOKABLE_SPACES = class {
|
||||||
|
public static readonly ROUTE = 'bookable-spaces';
|
||||||
|
static ACTIONS = class {
|
||||||
|
public static readonly ADD_BOOKABLE_SPACES_SUMMARY =
|
||||||
|
'Add new bookable spaces';
|
||||||
|
|
||||||
|
public static readonly ADD_BOOKABLE_SPACES_DESCRIPTION =
|
||||||
|
'This endpoint allows you to add new bookable spaces by providing the required details.';
|
||||||
|
|
||||||
|
public static readonly GET_ALL_BOOKABLE_SPACES_SUMMARY =
|
||||||
|
'Get all bookable spaces';
|
||||||
|
|
||||||
|
public static readonly GET_ALL_BOOKABLE_SPACES_DESCRIPTION =
|
||||||
|
'This endpoint retrieves all bookable spaces.';
|
||||||
|
|
||||||
|
public static readonly UPDATE_BOOKABLE_SPACES_SUMMARY =
|
||||||
|
'Update existing bookable spaces';
|
||||||
|
|
||||||
|
public static readonly UPDATE_BOOKABLE_SPACES_DESCRIPTION =
|
||||||
|
'This endpoint allows you to update existing bookable spaces by providing the required details.';
|
||||||
|
};
|
||||||
|
};
|
||||||
|
static BOOKING = class {
|
||||||
|
public static readonly ROUTE = 'bookings';
|
||||||
|
static ACTIONS = class {
|
||||||
|
public static readonly ADD_BOOKING_SUMMARY = 'Add new booking';
|
||||||
|
|
||||||
|
public static readonly ADD_BOOKING_DESCRIPTION =
|
||||||
|
'This endpoint allows you to add new booking by providing the required details.';
|
||||||
|
|
||||||
|
public static readonly GET_ALL_BOOKINGS_SUMMARY = 'Get all bookings';
|
||||||
|
|
||||||
|
public static readonly GET_ALL_BOOKINGS_DESCRIPTION =
|
||||||
|
'This endpoint retrieves all bookings.';
|
||||||
|
|
||||||
|
public static readonly GET_MY_BOOKINGS_SUMMARY = 'Get my bookings';
|
||||||
|
|
||||||
|
public static readonly GET_MY_BOOKINGS_DESCRIPTION =
|
||||||
|
'This endpoint retrieves all bookings for the authenticated user.';
|
||||||
|
};
|
||||||
|
};
|
||||||
static COMMUNITY = class {
|
static COMMUNITY = class {
|
||||||
public static readonly ROUTE = '/projects/:projectUuid/communities';
|
public static readonly ROUTE = '/projects/:projectUuid/communities';
|
||||||
static ACTIONS = class {
|
static ACTIONS = class {
|
||||||
@ -183,6 +223,10 @@ export class ControllerRoute {
|
|||||||
public static readonly CREATE_SPACE_DESCRIPTION =
|
public static readonly CREATE_SPACE_DESCRIPTION =
|
||||||
'This endpoint allows you to create a space in a specified community. Optionally, you can specify a parent space to nest the new space under it.';
|
'This endpoint allows you to create a space in a specified community. Optionally, you can specify a parent space to nest the new space under it.';
|
||||||
|
|
||||||
|
public static readonly DUPLICATE_SPACE_SUMMARY = 'Duplicate a space';
|
||||||
|
public static readonly DUPLICATE_SPACE_DESCRIPTION =
|
||||||
|
'This endpoint allows you to create a copy of an existing space in a specified community.';
|
||||||
|
|
||||||
public static readonly LIST_SPACE_SUMMARY = 'List spaces in community';
|
public static readonly LIST_SPACE_SUMMARY = 'List spaces in community';
|
||||||
public static readonly LIST_SPACE_DESCRIPTION =
|
public static readonly LIST_SPACE_DESCRIPTION =
|
||||||
'List spaces in specified community by community id';
|
'List spaces in specified community by community id';
|
||||||
@ -199,6 +243,11 @@ export class ControllerRoute {
|
|||||||
public static readonly UPDATE_SPACE_DESCRIPTION =
|
public static readonly UPDATE_SPACE_DESCRIPTION =
|
||||||
'Updates a space by its UUID and community ID. You can update the name, parent space, and other properties. If a parent space is provided and not already a parent, its `isParent` flag will be set to true.';
|
'Updates a space by its UUID and community ID. You can update the name, parent space, and other properties. If a parent space is provided and not already a parent, its `isParent` flag will be set to true.';
|
||||||
|
|
||||||
|
public static readonly UPDATE_CHILDREN_SPACES_ORDER_OF_A_SPACE_SUMMARY =
|
||||||
|
'Update the order of child spaces under a specific parent space';
|
||||||
|
public static readonly UPDATE_CHILDREN_SPACES_ORDER_OF_A_SPACE_DESCRIPTION =
|
||||||
|
'Updates the order of child spaces under a specific parent space. You can provide a new order for the child spaces.';
|
||||||
|
|
||||||
public static readonly GET_HEIRARCHY_SUMMARY = 'Get space hierarchy';
|
public static readonly GET_HEIRARCHY_SUMMARY = 'Get space hierarchy';
|
||||||
public static readonly GET_HEIRARCHY_DESCRIPTION =
|
public static readonly GET_HEIRARCHY_DESCRIPTION =
|
||||||
'This endpoint retrieves the hierarchical structure of spaces under a given space ID. It returns all the child spaces nested within the specified space, organized by their parent-child relationships. ';
|
'This endpoint retrieves the hierarchical structure of spaces under a given space ID. It returns all the child spaces nested within the specified space, organized by their parent-child relationships. ';
|
||||||
@ -632,6 +681,11 @@ export class ControllerRoute {
|
|||||||
'Delete scenes by device uuid and switch name';
|
'Delete scenes by device uuid and switch name';
|
||||||
public static readonly DELETE_SCENES_BY_SWITCH_NAME_DESCRIPTION =
|
public static readonly DELETE_SCENES_BY_SWITCH_NAME_DESCRIPTION =
|
||||||
'This endpoint deletes all scenes associated with a specific switch device.';
|
'This endpoint deletes all scenes associated with a specific switch device.';
|
||||||
|
|
||||||
|
public static readonly POPULATE_TUYA_CONST_UUID_SUMMARY =
|
||||||
|
'Populate Tuya const UUID';
|
||||||
|
public static readonly POPULATE_TUYA_CONST_UUID_DESCRIPTION =
|
||||||
|
'This endpoint populates the Tuya const UUID for all devices.';
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
static DEVICE_COMMISSION = class {
|
static DEVICE_COMMISSION = class {
|
||||||
|
@ -1,3 +0,0 @@
|
|||||||
export const SEND_EMAIL_API_URL_PROD = 'https://send.api.mailtrap.io/api/send/';
|
|
||||||
export const SEND_EMAIL_API_URL_DEV =
|
|
||||||
'https://sandbox.api.mailtrap.io/api/send/2634012';
|
|
3
libs/common/src/constants/timer-job-type.enum.ts
Normal file
3
libs/common/src/constants/timer-job-type.enum.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export enum TimerJobTypeEnum {
|
||||||
|
INVITE_USER_EMAIL = 'INVITE_USER_EMAIL',
|
||||||
|
}
|
@ -14,6 +14,8 @@ import { createLogger } from 'winston';
|
|||||||
import { winstonLoggerOptions } from '../logger/services/winston.logger';
|
import { winstonLoggerOptions } from '../logger/services/winston.logger';
|
||||||
import { AqiSpaceDailyPollutantStatsEntity } from '../modules/aqi/entities';
|
import { AqiSpaceDailyPollutantStatsEntity } from '../modules/aqi/entities';
|
||||||
import { AutomationEntity } from '../modules/automation/entities';
|
import { AutomationEntity } from '../modules/automation/entities';
|
||||||
|
import { BookableSpaceEntity } from '../modules/booking/entities/bookable-space.entity';
|
||||||
|
import { BookingEntity } from '../modules/booking/entities/booking.entity';
|
||||||
import { ClientEntity } from '../modules/client/entities';
|
import { ClientEntity } from '../modules/client/entities';
|
||||||
import { CommunityEntity } from '../modules/community/entities';
|
import { CommunityEntity } from '../modules/community/entities';
|
||||||
import { DeviceStatusLogEntity } from '../modules/device-status-log/entities';
|
import { DeviceStatusLogEntity } from '../modules/device-status-log/entities';
|
||||||
@ -25,6 +27,7 @@ import {
|
|||||||
InviteUserEntity,
|
InviteUserEntity,
|
||||||
InviteUserSpaceEntity,
|
InviteUserSpaceEntity,
|
||||||
} from '../modules/Invite-user/entities';
|
} from '../modules/Invite-user/entities';
|
||||||
|
import { SpaceDailyOccupancyDurationEntity } from '../modules/occupancy/entities';
|
||||||
import {
|
import {
|
||||||
PowerClampDailyEntity,
|
PowerClampDailyEntity,
|
||||||
PowerClampHourlyEntity,
|
PowerClampHourlyEntity,
|
||||||
@ -46,19 +49,18 @@ import {
|
|||||||
SubspaceModelProductAllocationEntity,
|
SubspaceModelProductAllocationEntity,
|
||||||
} from '../modules/space-model/entities';
|
} from '../modules/space-model/entities';
|
||||||
import { InviteSpaceEntity } from '../modules/space/entities/invite-space.entity';
|
import { InviteSpaceEntity } from '../modules/space/entities/invite-space.entity';
|
||||||
import { SpaceLinkEntity } from '../modules/space/entities/space-link.entity';
|
|
||||||
import { SpaceProductAllocationEntity } from '../modules/space/entities/space-product-allocation.entity';
|
import { SpaceProductAllocationEntity } from '../modules/space/entities/space-product-allocation.entity';
|
||||||
import { SpaceEntity } from '../modules/space/entities/space.entity';
|
import { SpaceEntity } from '../modules/space/entities/space.entity';
|
||||||
import { SubspaceProductAllocationEntity } from '../modules/space/entities/subspace/subspace-product-allocation.entity';
|
import { SubspaceProductAllocationEntity } from '../modules/space/entities/subspace/subspace-product-allocation.entity';
|
||||||
import { SubspaceEntity } from '../modules/space/entities/subspace/subspace.entity';
|
import { SubspaceEntity } from '../modules/space/entities/subspace/subspace.entity';
|
||||||
import { NewTagEntity } from '../modules/tag/entities/tag.entity';
|
import { NewTagEntity } from '../modules/tag/entities/tag.entity';
|
||||||
|
import { TimerEntity } from '../modules/timer/entities/timer.entity';
|
||||||
import { TimeZoneEntity } from '../modules/timezone/entities';
|
import { TimeZoneEntity } from '../modules/timezone/entities';
|
||||||
import {
|
import {
|
||||||
UserNotificationEntity,
|
UserNotificationEntity,
|
||||||
UserSpaceEntity,
|
UserSpaceEntity,
|
||||||
} from '../modules/user/entities';
|
} from '../modules/user/entities';
|
||||||
import { VisitorPasswordEntity } from '../modules/visitor-password/entities';
|
import { VisitorPasswordEntity } from '../modules/visitor-password/entities';
|
||||||
import { SpaceDailyOccupancyDurationEntity } from '../modules/occupancy/entities';
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
TypeOrmModule.forRootAsync({
|
TypeOrmModule.forRootAsync({
|
||||||
@ -87,7 +89,6 @@ import { SpaceDailyOccupancyDurationEntity } from '../modules/occupancy/entities
|
|||||||
PermissionTypeEntity,
|
PermissionTypeEntity,
|
||||||
CommunityEntity,
|
CommunityEntity,
|
||||||
SpaceEntity,
|
SpaceEntity,
|
||||||
SpaceLinkEntity,
|
|
||||||
SubspaceEntity,
|
SubspaceEntity,
|
||||||
UserSpaceEntity,
|
UserSpaceEntity,
|
||||||
DeviceUserPermissionEntity,
|
DeviceUserPermissionEntity,
|
||||||
@ -119,6 +120,9 @@ import { SpaceDailyOccupancyDurationEntity } from '../modules/occupancy/entities
|
|||||||
PresenceSensorDailySpaceEntity,
|
PresenceSensorDailySpaceEntity,
|
||||||
AqiSpaceDailyPollutantStatsEntity,
|
AqiSpaceDailyPollutantStatsEntity,
|
||||||
SpaceDailyOccupancyDurationEntity,
|
SpaceDailyOccupancyDurationEntity,
|
||||||
|
BookableSpaceEntity,
|
||||||
|
BookingEntity,
|
||||||
|
TimerEntity,
|
||||||
],
|
],
|
||||||
namingStrategy: new SnakeNamingStrategy(),
|
namingStrategy: new SnakeNamingStrategy(),
|
||||||
synchronize: Boolean(JSON.parse(configService.get('DB_SYNC'))),
|
synchronize: Boolean(JSON.parse(configService.get('DB_SYNC'))),
|
||||||
@ -127,8 +131,8 @@ import { SpaceDailyOccupancyDurationEntity } from '../modules/occupancy/entities
|
|||||||
logger: typeOrmLogger,
|
logger: typeOrmLogger,
|
||||||
extra: {
|
extra: {
|
||||||
charset: 'utf8mb4',
|
charset: 'utf8mb4',
|
||||||
max: 20, // set pool max size
|
max: 100, // set pool max size
|
||||||
idleTimeoutMillis: 5000, // close idle clients after 5 second
|
idleTimeoutMillis: 3000, // close idle clients after 5 second
|
||||||
connectionTimeoutMillis: 12_000, // return an error after 11 second if connection could not be established
|
connectionTimeoutMillis: 12_000, // return an error after 11 second if connection could not be established
|
||||||
maxUses: 7500, // close (and replace) a connection after it has been used 7500 times (see below for discussion)
|
maxUses: 7500, // close (and replace) a connection after it has been used 7500 times (see below for discussion)
|
||||||
},
|
},
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
import { IsBoolean, IsDate, IsOptional } from 'class-validator';
|
|
||||||
import { IsPageRequestParam } from '../validators/is-page-request-param.validator';
|
|
||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
import { IsSizeRequestParam } from '../validators/is-size-request-param.validator';
|
|
||||||
import { Transform } from 'class-transformer';
|
import { Transform } from 'class-transformer';
|
||||||
import { parseToDate } from '../util/parseToDate';
|
import { IsBoolean, IsOptional } from 'class-validator';
|
||||||
import { BooleanValues } from '../constants/boolean-values.enum';
|
import { BooleanValues } from '../constants/boolean-values.enum';
|
||||||
|
import { IsPageRequestParam } from '../validators/is-page-request-param.validator';
|
||||||
|
import { IsSizeRequestParam } from '../validators/is-size-request-param.validator';
|
||||||
|
|
||||||
export class PaginationRequestGetListDto {
|
export class PaginationRequestGetListDto {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
@ -19,6 +18,7 @@ export class PaginationRequestGetListDto {
|
|||||||
return value.obj.includeSpaces === BooleanValues.TRUE;
|
return value.obj.includeSpaces === BooleanValues.TRUE;
|
||||||
})
|
})
|
||||||
public includeSpaces?: boolean = false;
|
public includeSpaces?: boolean = false;
|
||||||
|
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@IsPageRequestParam({
|
@IsPageRequestParam({
|
||||||
message: 'Page must be bigger than 0',
|
message: 'Page must be bigger than 0',
|
||||||
@ -40,40 +40,4 @@ export class PaginationRequestGetListDto {
|
|||||||
description: 'Size request',
|
description: 'Size request',
|
||||||
})
|
})
|
||||||
size?: number;
|
size?: number;
|
||||||
|
|
||||||
@IsOptional()
|
|
||||||
@ApiProperty({
|
|
||||||
name: 'name',
|
|
||||||
required: false,
|
|
||||||
description: 'Name to be filtered',
|
|
||||||
})
|
|
||||||
name?: string;
|
|
||||||
|
|
||||||
@ApiProperty({
|
|
||||||
name: 'from',
|
|
||||||
required: false,
|
|
||||||
type: Number,
|
|
||||||
description: `Start time in UNIX timestamp format to filter`,
|
|
||||||
example: 1674172800000,
|
|
||||||
})
|
|
||||||
@IsOptional()
|
|
||||||
@Transform(({ value }) => parseToDate(value))
|
|
||||||
@IsDate({
|
|
||||||
message: `From must be in UNIX timestamp format in order to parse to Date instance`,
|
|
||||||
})
|
|
||||||
from?: Date;
|
|
||||||
|
|
||||||
@ApiProperty({
|
|
||||||
name: 'to',
|
|
||||||
required: false,
|
|
||||||
type: Number,
|
|
||||||
description: `End time in UNIX timestamp format to filter`,
|
|
||||||
example: 1674259200000,
|
|
||||||
})
|
|
||||||
@IsOptional()
|
|
||||||
@Transform(({ value }) => parseToDate(value))
|
|
||||||
@IsDate({
|
|
||||||
message: `To must be in UNIX timestamp format in order to parse to Date instance`,
|
|
||||||
})
|
|
||||||
to?: Date;
|
|
||||||
}
|
}
|
||||||
|
@ -3,28 +3,12 @@ import { DeviceStatusFirebaseController } from './controllers/devices-status.con
|
|||||||
import { DeviceStatusFirebaseService } from './services/devices-status.service';
|
import { DeviceStatusFirebaseService } from './services/devices-status.service';
|
||||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||||
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories/device-status.repository';
|
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories/device-status.repository';
|
||||||
import { PowerClampService } from '@app/common/helper/services/power.clamp.service';
|
|
||||||
import {
|
|
||||||
PowerClampHourlyRepository,
|
|
||||||
PowerClampDailyRepository,
|
|
||||||
PowerClampMonthlyRepository,
|
|
||||||
} from '@app/common/modules/power-clamp/repositories';
|
|
||||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
|
||||||
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
|
||||||
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
providers: [
|
providers: [
|
||||||
DeviceStatusFirebaseService,
|
DeviceStatusFirebaseService,
|
||||||
DeviceRepository,
|
DeviceRepository,
|
||||||
DeviceStatusLogRepository,
|
DeviceStatusLogRepository,
|
||||||
PowerClampService,
|
|
||||||
PowerClampHourlyRepository,
|
|
||||||
PowerClampDailyRepository,
|
|
||||||
PowerClampMonthlyRepository,
|
|
||||||
SqlLoaderService,
|
|
||||||
OccupancyService,
|
|
||||||
AqiDataService,
|
|
||||||
],
|
],
|
||||||
controllers: [DeviceStatusFirebaseController],
|
controllers: [DeviceStatusFirebaseController],
|
||||||
exports: [DeviceStatusFirebaseService, DeviceStatusLogRepository],
|
exports: [DeviceStatusFirebaseService, DeviceStatusLogRepository],
|
||||||
|
@ -1,15 +1,13 @@
|
|||||||
|
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
|
||||||
|
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||||
import {
|
import {
|
||||||
HttpException,
|
HttpException,
|
||||||
HttpStatus,
|
HttpStatus,
|
||||||
Injectable,
|
Injectable,
|
||||||
NotFoundException,
|
NotFoundException,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { AddDeviceStatusDto } from '../dtos/add.devices-status.dto';
|
|
||||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
|
||||||
import { GetDeviceDetailsFunctionsStatusInterface } from 'src/device/interfaces/get.device.interface';
|
|
||||||
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
|
||||||
import { ConfigService } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
import { firebaseDataBase } from '../../firebase.config';
|
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||||
import {
|
import {
|
||||||
Database,
|
Database,
|
||||||
DataSnapshot,
|
DataSnapshot,
|
||||||
@ -17,13 +15,9 @@ import {
|
|||||||
ref,
|
ref,
|
||||||
runTransaction,
|
runTransaction,
|
||||||
} from 'firebase/database';
|
} from 'firebase/database';
|
||||||
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
|
import { GetDeviceDetailsFunctionsStatusInterface } from 'src/device/interfaces/get.device.interface';
|
||||||
import { ProductType } from '@app/common/constants/product-type.enum';
|
import { firebaseDataBase } from '../../firebase.config';
|
||||||
import { PowerClampService } from '@app/common/helper/services/power.clamp.service';
|
import { AddDeviceStatusDto } from '../dtos/add.devices-status.dto';
|
||||||
import { PowerClampEnergyEnum } from '@app/common/constants/power.clamp.enargy.enum';
|
|
||||||
import { PresenceSensorEnum } from '@app/common/constants/presence.sensor.enum';
|
|
||||||
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
|
||||||
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class DeviceStatusFirebaseService {
|
export class DeviceStatusFirebaseService {
|
||||||
private tuya: TuyaContext;
|
private tuya: TuyaContext;
|
||||||
@ -33,9 +27,6 @@ export class DeviceStatusFirebaseService {
|
|||||||
constructor(
|
constructor(
|
||||||
private readonly configService: ConfigService,
|
private readonly configService: ConfigService,
|
||||||
private readonly deviceRepository: DeviceRepository,
|
private readonly deviceRepository: DeviceRepository,
|
||||||
private readonly powerClampService: PowerClampService,
|
|
||||||
private readonly occupancyService: OccupancyService,
|
|
||||||
private readonly aqiDataService: AqiDataService,
|
|
||||||
private deviceStatusLogRepository: DeviceStatusLogRepository,
|
private deviceStatusLogRepository: DeviceStatusLogRepository,
|
||||||
) {
|
) {
|
||||||
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
||||||
@ -48,12 +39,7 @@ export class DeviceStatusFirebaseService {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Initialize firebaseDb using firebaseDataBase function
|
// Initialize firebaseDb using firebaseDataBase function
|
||||||
try {
|
this.firebaseDb = firebaseDataBase(this.configService);
|
||||||
this.firebaseDb = firebaseDataBase(this.configService);
|
|
||||||
} catch (error) {
|
|
||||||
console.warn('Firebase initialization failed, continuing without Firebase:', error.message);
|
|
||||||
this.firebaseDb = null;
|
|
||||||
}
|
|
||||||
this.isDevEnv =
|
this.isDevEnv =
|
||||||
this.configService.get<string>('NODE_ENV') === 'development';
|
this.configService.get<string>('NODE_ENV') === 'development';
|
||||||
}
|
}
|
||||||
@ -85,25 +71,107 @@ export class DeviceStatusFirebaseService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async addDeviceStatusToFirebase(
|
async addBatchDeviceStatusToOurDb(
|
||||||
addDeviceStatusDto: AddDeviceStatusDto,
|
batch: {
|
||||||
): Promise<AddDeviceStatusDto | null> {
|
deviceTuyaUuid: string;
|
||||||
try {
|
status: any;
|
||||||
const device = await this.getDeviceByDeviceTuyaUuid(
|
log: any;
|
||||||
addDeviceStatusDto.deviceTuyaUuid,
|
device: any;
|
||||||
|
}[],
|
||||||
|
): Promise<void> {
|
||||||
|
console.log(`🔁 Preparing logs from batch of ${batch.length} items...`);
|
||||||
|
|
||||||
|
const allLogs = [];
|
||||||
|
|
||||||
|
for (const item of batch) {
|
||||||
|
const device = item.device;
|
||||||
|
|
||||||
|
if (!device?.uuid) {
|
||||||
|
console.log(`⛔ Skipped unknown device: ${item.deviceTuyaUuid}`);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Determine properties based on environment
|
||||||
|
const properties =
|
||||||
|
this.isDevEnv && Array.isArray(item.log?.properties)
|
||||||
|
? item.log.properties
|
||||||
|
: Array.isArray(item.status)
|
||||||
|
? item.status
|
||||||
|
: null;
|
||||||
|
|
||||||
|
if (!properties) {
|
||||||
|
console.log(
|
||||||
|
`⛔ Skipped invalid status/properties for device: ${item.deviceTuyaUuid}`,
|
||||||
|
);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const logs = properties.map((property) =>
|
||||||
|
this.deviceStatusLogRepository.create({
|
||||||
|
deviceId: device.uuid,
|
||||||
|
deviceTuyaId: item.deviceTuyaUuid,
|
||||||
|
productId: device.productDevice?.uuid,
|
||||||
|
log: item.log,
|
||||||
|
code: property.code,
|
||||||
|
value: property.value,
|
||||||
|
eventId: item.log?.dataId,
|
||||||
|
eventTime: new Date(
|
||||||
|
this.isDevEnv ? property.time : property.t,
|
||||||
|
).toISOString(),
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
allLogs.push(...logs);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`📝 Total logs to insert: ${allLogs.length}`);
|
||||||
|
|
||||||
|
const chunkSize = 300;
|
||||||
|
let insertedCount = 0;
|
||||||
|
|
||||||
|
for (let i = 0; i < allLogs.length; i += chunkSize) {
|
||||||
|
const chunk = allLogs.slice(i, i + chunkSize);
|
||||||
|
try {
|
||||||
|
const result = await this.deviceStatusLogRepository
|
||||||
|
.createQueryBuilder()
|
||||||
|
.insert()
|
||||||
|
.into('device-status-log')
|
||||||
|
.values(chunk)
|
||||||
|
.orIgnore()
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
insertedCount += result.identifiers.length;
|
||||||
|
console.log(
|
||||||
|
`✅ Inserted ${result.identifiers.length} / ${chunk.length} logs (chunk)`,
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('❌ Insert error (skipped chunk):', error.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`✅ Total logs inserted: ${insertedCount} / ${allLogs.length}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
async addDeviceStatusToFirebase(
|
||||||
|
addDeviceStatusDto: AddDeviceStatusDto & { device?: any },
|
||||||
|
): Promise<AddDeviceStatusDto | null> {
|
||||||
|
try {
|
||||||
|
let device = addDeviceStatusDto.device;
|
||||||
|
if (!device) {
|
||||||
|
device = await this.getDeviceByDeviceTuyaUuid(
|
||||||
|
addDeviceStatusDto.deviceTuyaUuid,
|
||||||
|
);
|
||||||
|
}
|
||||||
if (device?.uuid) {
|
if (device?.uuid) {
|
||||||
return await this.createDeviceStatusFirebase({
|
return await this.createDeviceStatusFirebase({
|
||||||
deviceUuid: device.uuid,
|
deviceUuid: device.uuid,
|
||||||
...addDeviceStatusDto,
|
...addDeviceStatusDto,
|
||||||
productType: device.productDevice.prodType,
|
productType: device.productDevice?.prodType,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
// Return null if device not found or no UUID
|
// Return null if device not found or no UUID
|
||||||
return null;
|
return null;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// Handle the error silently, perhaps log it internally or ignore it
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -117,6 +185,15 @@ export class DeviceStatusFirebaseService {
|
|||||||
relations: ['productDevice'],
|
relations: ['productDevice'],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
async getAllDevices() {
|
||||||
|
return await this.deviceRepository.find({
|
||||||
|
where: {
|
||||||
|
isActive: true,
|
||||||
|
},
|
||||||
|
relations: ['productDevice'],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
async getDevicesInstructionStatus(deviceUuid: string) {
|
async getDevicesInstructionStatus(deviceUuid: string) {
|
||||||
try {
|
try {
|
||||||
const deviceDetails = await this.getDeviceByDeviceUuid(deviceUuid);
|
const deviceDetails = await this.getDeviceByDeviceUuid(deviceUuid);
|
||||||
@ -175,14 +252,6 @@ export class DeviceStatusFirebaseService {
|
|||||||
async createDeviceStatusFirebase(
|
async createDeviceStatusFirebase(
|
||||||
addDeviceStatusDto: AddDeviceStatusDto,
|
addDeviceStatusDto: AddDeviceStatusDto,
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
// Check if Firebase is available
|
|
||||||
if (!this.firebaseDb) {
|
|
||||||
console.warn('Firebase not available, skipping Firebase operations');
|
|
||||||
// Still process the database logs but skip Firebase operations
|
|
||||||
await this.processDeviceStatusLogs(addDeviceStatusDto);
|
|
||||||
return { message: 'Device status processed without Firebase' };
|
|
||||||
}
|
|
||||||
|
|
||||||
const dataRef = ref(
|
const dataRef = ref(
|
||||||
this.firebaseDb,
|
this.firebaseDb,
|
||||||
`device-status/${addDeviceStatusDto.deviceUuid}`,
|
`device-status/${addDeviceStatusDto.deviceUuid}`,
|
||||||
@ -228,251 +297,8 @@ export class DeviceStatusFirebaseService {
|
|||||||
return existingData;
|
return existingData;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (this.isDevEnv) {
|
|
||||||
// Save logs to your repository
|
|
||||||
const newLogs = addDeviceStatusDto.log.properties.map((property) => {
|
|
||||||
return this.deviceStatusLogRepository.create({
|
|
||||||
deviceId: addDeviceStatusDto.deviceUuid,
|
|
||||||
deviceTuyaId: addDeviceStatusDto.deviceTuyaUuid,
|
|
||||||
productId: addDeviceStatusDto.log.productId,
|
|
||||||
log: addDeviceStatusDto.log,
|
|
||||||
code: property.code,
|
|
||||||
value: property.value,
|
|
||||||
eventId: addDeviceStatusDto.log.dataId,
|
|
||||||
eventTime: new Date(property.time).toISOString(),
|
|
||||||
});
|
|
||||||
});
|
|
||||||
await this.deviceStatusLogRepository.save(newLogs);
|
|
||||||
|
|
||||||
if (addDeviceStatusDto.productType === ProductType.PC) {
|
|
||||||
const energyCodes = new Set([
|
|
||||||
PowerClampEnergyEnum.ENERGY_CONSUMED,
|
|
||||||
PowerClampEnergyEnum.ENERGY_CONSUMED_A,
|
|
||||||
PowerClampEnergyEnum.ENERGY_CONSUMED_B,
|
|
||||||
PowerClampEnergyEnum.ENERGY_CONSUMED_C,
|
|
||||||
]);
|
|
||||||
|
|
||||||
const energyStatus = addDeviceStatusDto?.log?.properties?.find(
|
|
||||||
(status) => energyCodes.has(status.code),
|
|
||||||
);
|
|
||||||
|
|
||||||
if (energyStatus) {
|
|
||||||
await this.powerClampService.updateEnergyConsumedHistoricalData(
|
|
||||||
addDeviceStatusDto.deviceUuid,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
addDeviceStatusDto.productType === ProductType.CPS ||
|
|
||||||
addDeviceStatusDto.productType === ProductType.WPS
|
|
||||||
) {
|
|
||||||
const occupancyCodes = new Set([PresenceSensorEnum.PRESENCE_STATE]);
|
|
||||||
|
|
||||||
const occupancyStatus = addDeviceStatusDto?.log?.properties?.find(
|
|
||||||
(status) => occupancyCodes.has(status.code),
|
|
||||||
);
|
|
||||||
|
|
||||||
if (occupancyStatus) {
|
|
||||||
await this.occupancyService.updateOccupancySensorHistoricalData(
|
|
||||||
addDeviceStatusDto.deviceUuid,
|
|
||||||
);
|
|
||||||
await this.occupancyService.updateOccupancySensorHistoricalDurationData(
|
|
||||||
addDeviceStatusDto.deviceUuid,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (addDeviceStatusDto.productType === ProductType.AQI) {
|
|
||||||
await this.aqiDataService.updateAQISensorHistoricalData(
|
|
||||||
addDeviceStatusDto.deviceUuid,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Save logs to your repository
|
|
||||||
const newLogs = addDeviceStatusDto?.status.map((property) => {
|
|
||||||
return this.deviceStatusLogRepository.create({
|
|
||||||
deviceId: addDeviceStatusDto.deviceUuid,
|
|
||||||
deviceTuyaId: addDeviceStatusDto.deviceTuyaUuid,
|
|
||||||
productId: addDeviceStatusDto.log.productKey,
|
|
||||||
log: addDeviceStatusDto.log,
|
|
||||||
code: property.code,
|
|
||||||
value: property.value,
|
|
||||||
eventId: addDeviceStatusDto.log.dataId,
|
|
||||||
eventTime: new Date(property.t).toISOString(),
|
|
||||||
});
|
|
||||||
});
|
|
||||||
await this.deviceStatusLogRepository.save(newLogs);
|
|
||||||
|
|
||||||
if (addDeviceStatusDto.productType === ProductType.PC) {
|
|
||||||
const energyCodes = new Set([
|
|
||||||
PowerClampEnergyEnum.ENERGY_CONSUMED,
|
|
||||||
PowerClampEnergyEnum.ENERGY_CONSUMED_A,
|
|
||||||
PowerClampEnergyEnum.ENERGY_CONSUMED_B,
|
|
||||||
PowerClampEnergyEnum.ENERGY_CONSUMED_C,
|
|
||||||
]);
|
|
||||||
|
|
||||||
const energyStatus = addDeviceStatusDto?.status?.find((status) => {
|
|
||||||
return energyCodes.has(status.code as PowerClampEnergyEnum);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (energyStatus) {
|
|
||||||
await this.powerClampService.updateEnergyConsumedHistoricalData(
|
|
||||||
addDeviceStatusDto.deviceUuid,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
addDeviceStatusDto.productType === ProductType.CPS ||
|
|
||||||
addDeviceStatusDto.productType === ProductType.WPS
|
|
||||||
) {
|
|
||||||
const occupancyCodes = new Set([PresenceSensorEnum.PRESENCE_STATE]);
|
|
||||||
|
|
||||||
const occupancyStatus = addDeviceStatusDto?.status?.find((status) => {
|
|
||||||
return occupancyCodes.has(status.code as PresenceSensorEnum);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (occupancyStatus) {
|
|
||||||
await this.occupancyService.updateOccupancySensorHistoricalData(
|
|
||||||
addDeviceStatusDto.deviceUuid,
|
|
||||||
);
|
|
||||||
await this.occupancyService.updateOccupancySensorHistoricalDurationData(
|
|
||||||
addDeviceStatusDto.deviceUuid,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (addDeviceStatusDto.productType === ProductType.AQI) {
|
|
||||||
await this.aqiDataService.updateAQISensorHistoricalData(
|
|
||||||
addDeviceStatusDto.deviceUuid,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Return the updated data
|
// Return the updated data
|
||||||
const snapshot: DataSnapshot = await get(dataRef);
|
const snapshot: DataSnapshot = await get(dataRef);
|
||||||
return snapshot.val();
|
return snapshot.val();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async processDeviceStatusLogs(addDeviceStatusDto: AddDeviceStatusDto): Promise<void> {
|
|
||||||
if (this.isDevEnv) {
|
|
||||||
// Save logs to your repository
|
|
||||||
const newLogs = addDeviceStatusDto.log.properties.map((property) => {
|
|
||||||
return this.deviceStatusLogRepository.create({
|
|
||||||
deviceId: addDeviceStatusDto.deviceUuid,
|
|
||||||
deviceTuyaId: addDeviceStatusDto.deviceTuyaUuid,
|
|
||||||
productId: addDeviceStatusDto.log.productId,
|
|
||||||
log: addDeviceStatusDto.log,
|
|
||||||
code: property.code,
|
|
||||||
value: property.value,
|
|
||||||
eventId: addDeviceStatusDto.log.dataId,
|
|
||||||
eventTime: new Date(property.time).toISOString(),
|
|
||||||
});
|
|
||||||
});
|
|
||||||
await this.deviceStatusLogRepository.save(newLogs);
|
|
||||||
|
|
||||||
if (addDeviceStatusDto.productType === ProductType.PC) {
|
|
||||||
const energyCodes = new Set([
|
|
||||||
PowerClampEnergyEnum.ENERGY_CONSUMED,
|
|
||||||
PowerClampEnergyEnum.ENERGY_CONSUMED_A,
|
|
||||||
PowerClampEnergyEnum.ENERGY_CONSUMED_B,
|
|
||||||
PowerClampEnergyEnum.ENERGY_CONSUMED_C,
|
|
||||||
]);
|
|
||||||
|
|
||||||
const energyStatus = addDeviceStatusDto?.log?.properties?.find(
|
|
||||||
(status) => energyCodes.has(status.code),
|
|
||||||
);
|
|
||||||
|
|
||||||
if (energyStatus) {
|
|
||||||
await this.powerClampService.updateEnergyConsumedHistoricalData(
|
|
||||||
addDeviceStatusDto.deviceUuid,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
addDeviceStatusDto.productType === ProductType.CPS ||
|
|
||||||
addDeviceStatusDto.productType === ProductType.WPS
|
|
||||||
) {
|
|
||||||
const occupancyCodes = new Set([PresenceSensorEnum.PRESENCE_STATE]);
|
|
||||||
|
|
||||||
const occupancyStatus = addDeviceStatusDto?.log?.properties?.find(
|
|
||||||
(status) => occupancyCodes.has(status.code),
|
|
||||||
);
|
|
||||||
|
|
||||||
if (occupancyStatus) {
|
|
||||||
await this.occupancyService.updateOccupancySensorHistoricalData(
|
|
||||||
addDeviceStatusDto.deviceUuid,
|
|
||||||
);
|
|
||||||
await this.occupancyService.updateOccupancySensorHistoricalDurationData(
|
|
||||||
addDeviceStatusDto.deviceUuid,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (addDeviceStatusDto.productType === ProductType.AQI) {
|
|
||||||
await this.aqiDataService.updateAQISensorHistoricalData(
|
|
||||||
addDeviceStatusDto.deviceUuid,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Save logs to your repository
|
|
||||||
const newLogs = addDeviceStatusDto?.status.map((property) => {
|
|
||||||
return this.deviceStatusLogRepository.create({
|
|
||||||
deviceId: addDeviceStatusDto.deviceUuid,
|
|
||||||
deviceTuyaId: addDeviceStatusDto.deviceTuyaUuid,
|
|
||||||
productId: addDeviceStatusDto.log.productKey,
|
|
||||||
log: addDeviceStatusDto.log,
|
|
||||||
code: property.code,
|
|
||||||
value: property.value,
|
|
||||||
eventId: addDeviceStatusDto.log.dataId,
|
|
||||||
eventTime: new Date(property.t).toISOString(),
|
|
||||||
});
|
|
||||||
});
|
|
||||||
await this.deviceStatusLogRepository.save(newLogs);
|
|
||||||
|
|
||||||
if (addDeviceStatusDto.productType === ProductType.PC) {
|
|
||||||
const energyCodes = new Set([
|
|
||||||
PowerClampEnergyEnum.ENERGY_CONSUMED,
|
|
||||||
PowerClampEnergyEnum.ENERGY_CONSUMED_A,
|
|
||||||
PowerClampEnergyEnum.ENERGY_CONSUMED_B,
|
|
||||||
PowerClampEnergyEnum.ENERGY_CONSUMED_C,
|
|
||||||
]);
|
|
||||||
|
|
||||||
const energyStatus = addDeviceStatusDto?.status?.find((status) => {
|
|
||||||
return energyCodes.has(status.code as PowerClampEnergyEnum);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (energyStatus) {
|
|
||||||
await this.powerClampService.updateEnergyConsumedHistoricalData(
|
|
||||||
addDeviceStatusDto.deviceUuid,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
addDeviceStatusDto.productType === ProductType.CPS ||
|
|
||||||
addDeviceStatusDto.productType === ProductType.WPS
|
|
||||||
) {
|
|
||||||
const occupancyCodes = new Set([PresenceSensorEnum.PRESENCE_STATE]);
|
|
||||||
|
|
||||||
const occupancyStatus = addDeviceStatusDto?.status?.find((status) => {
|
|
||||||
return occupancyCodes.has(status.code as PresenceSensorEnum);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (occupancyStatus) {
|
|
||||||
await this.occupancyService.updateOccupancySensorHistoricalData(
|
|
||||||
addDeviceStatusDto.deviceUuid,
|
|
||||||
);
|
|
||||||
await this.occupancyService.updateOccupancySensorHistoricalDurationData(
|
|
||||||
addDeviceStatusDto.deviceUuid,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (addDeviceStatusDto.productType === ProductType.AQI) {
|
|
||||||
await this.aqiDataService.updateAQISensorHistoricalData(
|
|
||||||
addDeviceStatusDto.deviceUuid,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -3,32 +3,21 @@ import { getDatabase } from 'firebase/database';
|
|||||||
import { ConfigService } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
|
|
||||||
export const initializeFirebaseApp = (configService: ConfigService) => {
|
export const initializeFirebaseApp = (configService: ConfigService) => {
|
||||||
try {
|
const firebaseConfig = {
|
||||||
const firebaseConfig = {
|
apiKey: configService.get<string>('FIREBASE_API_KEY'),
|
||||||
apiKey: configService.get<string>('FIREBASE_API_KEY'),
|
authDomain: configService.get<string>('FIREBASE_AUTH_DOMAIN'),
|
||||||
authDomain: configService.get<string>('FIREBASE_AUTH_DOMAIN'),
|
projectId: configService.get<string>('FIREBASE_PROJECT_ID'),
|
||||||
projectId: configService.get<string>('FIREBASE_PROJECT_ID'),
|
storageBucket: configService.get<string>('FIREBASE_STORAGE_BUCKET'),
|
||||||
storageBucket: configService.get<string>('FIREBASE_STORAGE_BUCKET'),
|
messagingSenderId: configService.get<string>(
|
||||||
messagingSenderId: configService.get<string>(
|
'FIREBASE_MESSAGING_SENDER_ID',
|
||||||
'FIREBASE_MESSAGING_SENDER_ID',
|
),
|
||||||
),
|
appId: configService.get<string>('FIREBASE_APP_ID'),
|
||||||
appId: configService.get<string>('FIREBASE_APP_ID'),
|
measurementId: configService.get<string>('FIREBASE_MEASUREMENT_ID'),
|
||||||
measurementId: configService.get<string>('FIREBASE_MEASUREMENT_ID'),
|
databaseURL: configService.get<string>('FIREBASE_DATABASE_URL'),
|
||||||
databaseURL: configService.get<string>('FIREBASE_DATABASE_URL'),
|
};
|
||||||
};
|
|
||||||
|
|
||||||
// Check if required Firebase config is available
|
const app = initializeApp(firebaseConfig);
|
||||||
if (!firebaseConfig.projectId || firebaseConfig.projectId === 'placeholder-project') {
|
return getDatabase(app);
|
||||||
console.warn('Firebase configuration not available, Firebase features will be disabled');
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const app = initializeApp(firebaseConfig);
|
|
||||||
return getDatabase(app);
|
|
||||||
} catch (error) {
|
|
||||||
console.warn('Firebase initialization failed, Firebase features will be disabled:', error.message);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const firebaseDataBase = (configService: ConfigService) =>
|
export const firebaseDataBase = (configService: ConfigService) =>
|
||||||
|
@ -8,7 +8,10 @@ import { TuyaWebSocketService } from './services/tuya.web.socket.service';
|
|||||||
import { OneSignalService } from './services/onesignal.service';
|
import { OneSignalService } from './services/onesignal.service';
|
||||||
import { DeviceMessagesService } from './services/device.messages.service';
|
import { DeviceMessagesService } from './services/device.messages.service';
|
||||||
import { DeviceRepositoryModule } from '../modules/device/device.repository.module';
|
import { DeviceRepositoryModule } from '../modules/device/device.repository.module';
|
||||||
import { DeviceNotificationRepository } from '../modules/device/repositories';
|
import {
|
||||||
|
DeviceNotificationRepository,
|
||||||
|
DeviceRepository,
|
||||||
|
} from '../modules/device/repositories';
|
||||||
import { DeviceStatusFirebaseModule } from '../firebase/devices-status/devices-status.module';
|
import { DeviceStatusFirebaseModule } from '../firebase/devices-status/devices-status.module';
|
||||||
import { CommunityPermissionService } from './services/community.permission.service';
|
import { CommunityPermissionService } from './services/community.permission.service';
|
||||||
import { CommunityRepository } from '../modules/community/repositories';
|
import { CommunityRepository } from '../modules/community/repositories';
|
||||||
@ -27,6 +30,7 @@ import { SosHandlerService } from './services/sos.handler.service';
|
|||||||
DeviceNotificationRepository,
|
DeviceNotificationRepository,
|
||||||
CommunityRepository,
|
CommunityRepository,
|
||||||
SosHandlerService,
|
SosHandlerService,
|
||||||
|
DeviceRepository,
|
||||||
],
|
],
|
||||||
exports: [
|
exports: [
|
||||||
HelperHashService,
|
HelperHashService,
|
||||||
|
@ -1,44 +1,63 @@
|
|||||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
|
||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { SqlLoaderService } from './sql-loader.service';
|
|
||||||
import { DataSource } from 'typeorm';
|
import { DataSource } from 'typeorm';
|
||||||
import { SQL_PROCEDURES_PATH } from '@app/common/constants/sql-query-path';
|
import { SQL_PROCEDURES_PATH } from '@app/common/constants/sql-query-path';
|
||||||
|
import { SqlLoaderService } from './sql-loader.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AqiDataService {
|
export class AqiDataService {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly sqlLoader: SqlLoaderService,
|
private readonly sqlLoader: SqlLoaderService,
|
||||||
private readonly dataSource: DataSource,
|
private readonly dataSource: DataSource,
|
||||||
private readonly deviceRepository: DeviceRepository,
|
|
||||||
) {}
|
) {}
|
||||||
async updateAQISensorHistoricalData(deviceUuid: string): Promise<void> {
|
|
||||||
try {
|
|
||||||
const now = new Date();
|
|
||||||
const dateStr = now.toLocaleDateString('en-CA'); // YYYY-MM-DD
|
|
||||||
const device = await this.deviceRepository.findOne({
|
|
||||||
where: { uuid: deviceUuid },
|
|
||||||
relations: ['spaceDevice'],
|
|
||||||
});
|
|
||||||
|
|
||||||
await this.executeProcedure(
|
async updateAQISensorHistoricalData(): Promise<void> {
|
||||||
'fact_daily_space_aqi',
|
try {
|
||||||
'proceduce_update_daily_space_aqi',
|
const { dateStr } = this.getFormattedDates();
|
||||||
[dateStr, device.spaceDevice?.uuid],
|
|
||||||
);
|
// Execute all procedures in parallel
|
||||||
|
await Promise.all([
|
||||||
|
this.executeProcedureWithRetry(
|
||||||
|
'proceduce_update_daily_space_aqi',
|
||||||
|
[dateStr],
|
||||||
|
'fact_daily_space_aqi',
|
||||||
|
),
|
||||||
|
]);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Failed to insert or update aqi data:', err);
|
console.error('Failed to update AQI sensor historical data:', err);
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
private getFormattedDates(): { dateStr: string } {
|
||||||
private async executeProcedure(
|
const now = new Date();
|
||||||
procedureFolderName: string,
|
return {
|
||||||
|
dateStr: now.toLocaleDateString('en-CA'), // YYYY-MM-DD
|
||||||
|
};
|
||||||
|
}
|
||||||
|
private async executeProcedureWithRetry(
|
||||||
procedureFileName: string,
|
procedureFileName: string,
|
||||||
params: (string | number | null)[],
|
params: (string | number | null)[],
|
||||||
|
folderName: string,
|
||||||
|
retries = 3,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const query = this.loadQuery(procedureFolderName, procedureFileName);
|
try {
|
||||||
await this.dataSource.query(query, params);
|
const query = this.loadQuery(folderName, procedureFileName);
|
||||||
console.log(`Procedure ${procedureFileName} executed successfully.`);
|
await this.dataSource.query(query, params);
|
||||||
|
console.log(`Procedure ${procedureFileName} executed successfully.`);
|
||||||
|
} catch (err) {
|
||||||
|
if (retries > 0) {
|
||||||
|
const delayMs = 1000 * (4 - retries); // Exponential backoff
|
||||||
|
console.warn(`Retrying ${procedureFileName} (${retries} retries left)`);
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
||||||
|
return this.executeProcedureWithRetry(
|
||||||
|
procedureFileName,
|
||||||
|
params,
|
||||||
|
folderName,
|
||||||
|
retries - 1,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
console.error(`Failed to execute ${procedureFileName}:`, err);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private loadQuery(folderName: string, fileName: string): string {
|
private loadQuery(folderName: string, fileName: string): string {
|
||||||
|
@ -1,65 +1,68 @@
|
|||||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
|
||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { SqlLoaderService } from './sql-loader.service';
|
|
||||||
import { DataSource } from 'typeorm';
|
import { DataSource } from 'typeorm';
|
||||||
import { SQL_PROCEDURES_PATH } from '@app/common/constants/sql-query-path';
|
import { SQL_PROCEDURES_PATH } from '@app/common/constants/sql-query-path';
|
||||||
|
import { SqlLoaderService } from './sql-loader.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class OccupancyService {
|
export class OccupancyService {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly sqlLoader: SqlLoaderService,
|
private readonly sqlLoader: SqlLoaderService,
|
||||||
private readonly dataSource: DataSource,
|
private readonly dataSource: DataSource,
|
||||||
private readonly deviceRepository: DeviceRepository,
|
|
||||||
) {}
|
) {}
|
||||||
async updateOccupancySensorHistoricalDurationData(
|
|
||||||
deviceUuid: string,
|
|
||||||
): Promise<void> {
|
|
||||||
try {
|
|
||||||
const now = new Date();
|
|
||||||
const dateStr = now.toLocaleDateString('en-CA'); // YYYY-MM-DD
|
|
||||||
const device = await this.deviceRepository.findOne({
|
|
||||||
where: { uuid: deviceUuid },
|
|
||||||
relations: ['spaceDevice'],
|
|
||||||
});
|
|
||||||
|
|
||||||
await this.executeProcedure(
|
async updateOccupancyDataProcedures(): Promise<void> {
|
||||||
'fact_daily_space_occupancy_duration',
|
try {
|
||||||
'procedure_update_daily_space_occupancy_duration',
|
const { dateStr } = this.getFormattedDates();
|
||||||
[dateStr, device.spaceDevice?.uuid],
|
|
||||||
);
|
// Execute all procedures in parallel
|
||||||
|
await Promise.all([
|
||||||
|
this.executeProcedureWithRetry(
|
||||||
|
'procedure_update_fact_space_occupancy',
|
||||||
|
[dateStr],
|
||||||
|
'fact_space_occupancy_count',
|
||||||
|
),
|
||||||
|
this.executeProcedureWithRetry(
|
||||||
|
'procedure_update_daily_space_occupancy_duration',
|
||||||
|
[dateStr],
|
||||||
|
'fact_daily_space_occupancy_duration',
|
||||||
|
),
|
||||||
|
]);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Failed to insert or update occupancy duration data:', err);
|
console.error('Failed to update occupancy data:', err);
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async updateOccupancySensorHistoricalData(deviceUuid: string): Promise<void> {
|
private getFormattedDates(): { dateStr: string } {
|
||||||
try {
|
const now = new Date();
|
||||||
const now = new Date();
|
return {
|
||||||
const dateStr = now.toLocaleDateString('en-CA'); // YYYY-MM-DD
|
dateStr: now.toLocaleDateString('en-CA'), // YYYY-MM-DD
|
||||||
const device = await this.deviceRepository.findOne({
|
};
|
||||||
where: { uuid: deviceUuid },
|
|
||||||
relations: ['spaceDevice'],
|
|
||||||
});
|
|
||||||
|
|
||||||
await this.executeProcedure(
|
|
||||||
'fact_space_occupancy_count',
|
|
||||||
'procedure_update_fact_space_occupancy',
|
|
||||||
[dateStr, device.spaceDevice?.uuid],
|
|
||||||
);
|
|
||||||
} catch (err) {
|
|
||||||
console.error('Failed to insert or update occupancy data:', err);
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
private async executeProcedureWithRetry(
|
||||||
private async executeProcedure(
|
|
||||||
procedureFolderName: string,
|
|
||||||
procedureFileName: string,
|
procedureFileName: string,
|
||||||
params: (string | number | null)[],
|
params: (string | number | null)[],
|
||||||
|
folderName: string,
|
||||||
|
retries = 3,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const query = this.loadQuery(procedureFolderName, procedureFileName);
|
try {
|
||||||
await this.dataSource.query(query, params);
|
const query = this.loadQuery(folderName, procedureFileName);
|
||||||
console.log(`Procedure ${procedureFileName} executed successfully.`);
|
await this.dataSource.query(query, params);
|
||||||
|
console.log(`Procedure ${procedureFileName} executed successfully.`);
|
||||||
|
} catch (err) {
|
||||||
|
if (retries > 0) {
|
||||||
|
const delayMs = 1000 * (4 - retries); // Exponential backoff
|
||||||
|
console.warn(`Retrying ${procedureFileName} (${retries} retries left)`);
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
||||||
|
return this.executeProcedureWithRetry(
|
||||||
|
procedureFileName,
|
||||||
|
params,
|
||||||
|
folderName,
|
||||||
|
retries - 1,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
console.error(`Failed to execute ${procedureFileName}:`, err);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private loadQuery(folderName: string, fileName: string): string {
|
private loadQuery(folderName: string, fileName: string): string {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { SqlLoaderService } from './sql-loader.service';
|
|
||||||
import { DataSource } from 'typeorm';
|
import { DataSource } from 'typeorm';
|
||||||
import { SQL_PROCEDURES_PATH } from '@app/common/constants/sql-query-path';
|
import { SQL_PROCEDURES_PATH } from '@app/common/constants/sql-query-path';
|
||||||
|
import { SqlLoaderService } from './sql-loader.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class PowerClampService {
|
export class PowerClampService {
|
||||||
@ -10,48 +10,72 @@ export class PowerClampService {
|
|||||||
private readonly dataSource: DataSource,
|
private readonly dataSource: DataSource,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async updateEnergyConsumedHistoricalData(deviceUuid: string): Promise<void> {
|
async updateEnergyConsumedHistoricalData(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const now = new Date();
|
const { dateStr, monthYear } = this.getFormattedDates();
|
||||||
const dateStr = now.toLocaleDateString('en-CA'); // YYYY-MM-DD
|
|
||||||
const hour = now.getHours();
|
|
||||||
const monthYear = now
|
|
||||||
.toLocaleDateString('en-US', {
|
|
||||||
month: '2-digit',
|
|
||||||
year: 'numeric',
|
|
||||||
})
|
|
||||||
.replace('/', '-'); // MM-YYYY
|
|
||||||
|
|
||||||
await this.executeProcedure(
|
// Execute all procedures in parallel
|
||||||
'fact_hourly_device_energy_consumed_procedure',
|
await Promise.all([
|
||||||
[deviceUuid, dateStr, hour],
|
this.executeProcedureWithRetry(
|
||||||
);
|
'fact_hourly_device_energy_consumed_procedure',
|
||||||
|
[dateStr],
|
||||||
await this.executeProcedure(
|
'fact_device_energy_consumed',
|
||||||
'fact_daily_device_energy_consumed_procedure',
|
),
|
||||||
[deviceUuid, dateStr],
|
this.executeProcedureWithRetry(
|
||||||
);
|
'fact_daily_device_energy_consumed_procedure',
|
||||||
|
[dateStr],
|
||||||
await this.executeProcedure(
|
'fact_device_energy_consumed',
|
||||||
'fact_monthly_device_energy_consumed_procedure',
|
),
|
||||||
[deviceUuid, monthYear],
|
this.executeProcedureWithRetry(
|
||||||
);
|
'fact_monthly_device_energy_consumed_procedure',
|
||||||
|
[monthYear],
|
||||||
|
'fact_device_energy_consumed',
|
||||||
|
),
|
||||||
|
]);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Failed to insert or update energy data:', err);
|
console.error('Failed to update energy consumption data:', err);
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async executeProcedure(
|
private getFormattedDates(): { dateStr: string; monthYear: string } {
|
||||||
|
const now = new Date();
|
||||||
|
return {
|
||||||
|
dateStr: now.toLocaleDateString('en-CA'), // YYYY-MM-DD
|
||||||
|
monthYear: now
|
||||||
|
.toLocaleDateString('en-US', {
|
||||||
|
month: '2-digit',
|
||||||
|
year: 'numeric',
|
||||||
|
})
|
||||||
|
.replace('/', '-'), // MM-YYYY
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private async executeProcedureWithRetry(
|
||||||
procedureFileName: string,
|
procedureFileName: string,
|
||||||
params: (string | number | null)[],
|
params: (string | number | null)[],
|
||||||
|
folderName: string,
|
||||||
|
retries = 3,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const query = this.loadQuery(
|
try {
|
||||||
'fact_device_energy_consumed',
|
const query = this.loadQuery(folderName, procedureFileName);
|
||||||
procedureFileName,
|
await this.dataSource.query(query, params);
|
||||||
);
|
console.log(`Procedure ${procedureFileName} executed successfully.`);
|
||||||
await this.dataSource.query(query, params);
|
} catch (err) {
|
||||||
console.log(`Procedure ${procedureFileName} executed successfully.`);
|
if (retries > 0) {
|
||||||
|
const delayMs = 1000 * (4 - retries); // Exponential backoff
|
||||||
|
console.warn(`Retrying ${procedureFileName} (${retries} retries left)`);
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
||||||
|
return this.executeProcedureWithRetry(
|
||||||
|
procedureFileName,
|
||||||
|
params,
|
||||||
|
folderName,
|
||||||
|
retries - 1,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
console.error(`Failed to execute ${procedureFileName}:`, err);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private loadQuery(folderName: string, fileName: string): string {
|
private loadQuery(folderName: string, fileName: string): string {
|
||||||
|
@ -16,21 +16,46 @@ export class SosHandlerService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async handleSosEvent(devId: string, logData: any): Promise<void> {
|
async handleSosEventFirebase(device: any, logData: any): Promise<void> {
|
||||||
|
const sosTrueStatus = [{ code: 'sos', value: true }];
|
||||||
|
const sosFalseStatus = [{ code: 'sos', value: false }];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// ✅ Send true status
|
||||||
await this.deviceStatusFirebaseService.addDeviceStatusToFirebase({
|
await this.deviceStatusFirebaseService.addDeviceStatusToFirebase({
|
||||||
deviceTuyaUuid: devId,
|
deviceTuyaUuid: device.deviceTuyaUuid,
|
||||||
status: [{ code: 'sos', value: true }],
|
status: sosTrueStatus,
|
||||||
log: logData,
|
log: logData,
|
||||||
|
device,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await this.deviceStatusFirebaseService.addBatchDeviceStatusToOurDb([
|
||||||
|
{
|
||||||
|
deviceTuyaUuid: device.deviceTuyaUuid,
|
||||||
|
status: sosTrueStatus,
|
||||||
|
log: logData,
|
||||||
|
device,
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
// ✅ Schedule false status
|
||||||
setTimeout(async () => {
|
setTimeout(async () => {
|
||||||
try {
|
try {
|
||||||
await this.deviceStatusFirebaseService.addDeviceStatusToFirebase({
|
await this.deviceStatusFirebaseService.addDeviceStatusToFirebase({
|
||||||
deviceTuyaUuid: devId,
|
deviceTuyaUuid: device.deviceTuyaUuid,
|
||||||
status: [{ code: 'sos', value: false }],
|
status: sosFalseStatus,
|
||||||
log: logData,
|
log: logData,
|
||||||
|
device,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await this.deviceStatusFirebaseService.addBatchDeviceStatusToOurDb([
|
||||||
|
{
|
||||||
|
deviceTuyaUuid: device.deviceTuyaUuid,
|
||||||
|
status: sosFalseStatus,
|
||||||
|
log: logData,
|
||||||
|
device,
|
||||||
|
},
|
||||||
|
]);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.logger.error('Failed to send SOS false value', err);
|
this.logger.error('Failed to send SOS false value', err);
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,24 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
|
||||||
import TuyaWebsocket from '../../config/tuya-web-socket-config';
|
|
||||||
import { ConfigService } from '@nestjs/config';
|
|
||||||
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
|
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
|
||||||
|
import { Injectable, OnModuleInit } from '@nestjs/common';
|
||||||
|
import { ConfigService } from '@nestjs/config';
|
||||||
|
import * as NodeCache from 'node-cache';
|
||||||
|
import TuyaWebsocket from '../../config/tuya-web-socket-config';
|
||||||
import { SosHandlerService } from './sos.handler.service';
|
import { SosHandlerService } from './sos.handler.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class TuyaWebSocketService {
|
export class TuyaWebSocketService implements OnModuleInit {
|
||||||
private client: any;
|
private client: any;
|
||||||
private readonly isDevEnv: boolean;
|
private readonly isDevEnv: boolean;
|
||||||
|
private readonly deviceCache = new NodeCache({ stdTTL: 7200 }); // TTL = 2 hour
|
||||||
|
|
||||||
|
private messageQueue: {
|
||||||
|
devId: string;
|
||||||
|
status: any;
|
||||||
|
logData: any;
|
||||||
|
device: any;
|
||||||
|
}[] = [];
|
||||||
|
|
||||||
|
private isProcessing = false;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly configService: ConfigService,
|
private readonly configService: ConfigService,
|
||||||
@ -26,16 +37,36 @@ export class TuyaWebSocketService {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (this.configService.get<string>('tuya-config.TRUN_ON_TUYA_SOCKET')) {
|
if (this.configService.get<string>('tuya-config.TRUN_ON_TUYA_SOCKET')) {
|
||||||
// Set up event handlers
|
|
||||||
this.setupEventHandlers();
|
this.setupEventHandlers();
|
||||||
|
|
||||||
// Start receiving messages
|
|
||||||
this.client.start();
|
this.client.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Run the queue processor every 15 seconds
|
||||||
|
setInterval(() => this.processQueue(), 15000);
|
||||||
|
|
||||||
|
// Refresh the cache every 1 hour
|
||||||
|
setInterval(() => this.initializeDeviceCache(), 30 * 60 * 1000); // 30 minutes
|
||||||
|
}
|
||||||
|
|
||||||
|
async onModuleInit() {
|
||||||
|
await this.initializeDeviceCache();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async initializeDeviceCache() {
|
||||||
|
try {
|
||||||
|
const allDevices = await this.deviceStatusFirebaseService.getAllDevices();
|
||||||
|
allDevices.forEach((device) => {
|
||||||
|
if (device.deviceTuyaUuid) {
|
||||||
|
this.deviceCache.set(device.deviceTuyaUuid, device);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
console.log(`✅ Refreshed cache with ${allDevices.length} devices.`);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('❌ Failed to initialize device cache:', error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private setupEventHandlers() {
|
private setupEventHandlers() {
|
||||||
// Event handlers
|
|
||||||
this.client.open(() => {
|
this.client.open(() => {
|
||||||
console.log('open');
|
console.log('open');
|
||||||
});
|
});
|
||||||
@ -43,23 +74,43 @@ export class TuyaWebSocketService {
|
|||||||
this.client.message(async (ws: WebSocket, message: any) => {
|
this.client.message(async (ws: WebSocket, message: any) => {
|
||||||
try {
|
try {
|
||||||
const { devId, status, logData } = this.extractMessageData(message);
|
const { devId, status, logData } = this.extractMessageData(message);
|
||||||
|
// console.log(
|
||||||
|
// `📬 Received message for device: ${devId}, status:`,
|
||||||
|
// status,
|
||||||
|
// logData,
|
||||||
|
// );
|
||||||
|
if (!Array.isArray(status)) {
|
||||||
|
this.client.ackMessage(message.messageId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const device = this.deviceCache.get(devId);
|
||||||
|
if (!device) {
|
||||||
|
// console.log(⛔ Unknown device: ${devId}, message ignored.);
|
||||||
|
this.client.ackMessage(message.messageId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (this.sosHandlerService.isSosTriggered(status)) {
|
if (this.sosHandlerService.isSosTriggered(status)) {
|
||||||
await this.sosHandlerService.handleSosEvent(devId, logData);
|
await this.sosHandlerService.handleSosEventFirebase(devId, logData);
|
||||||
} else {
|
} else {
|
||||||
await this.deviceStatusFirebaseService.addDeviceStatusToFirebase({
|
await this.deviceStatusFirebaseService.addDeviceStatusToFirebase({
|
||||||
deviceTuyaUuid: devId,
|
deviceTuyaUuid: devId,
|
||||||
status: status,
|
status,
|
||||||
log: logData,
|
log: logData,
|
||||||
|
device,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Push to internal queue
|
||||||
|
this.messageQueue.push({ devId, status, logData, device });
|
||||||
|
|
||||||
|
// Acknowledge the message
|
||||||
this.client.ackMessage(message.messageId);
|
this.client.ackMessage(message.messageId);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error processing message:', error);
|
console.error('❌ Error receiving message:', error);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
this.client.reconnect(() => {
|
this.client.reconnect(() => {
|
||||||
console.log('reconnect');
|
console.log('reconnect');
|
||||||
});
|
});
|
||||||
@ -80,11 +131,44 @@ export class TuyaWebSocketService {
|
|||||||
console.error('WebSocket error:', error);
|
console.error('WebSocket error:', error);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
private async processQueue() {
|
||||||
|
if (this.isProcessing) {
|
||||||
|
console.log('⏳ Skipping: still processing previous batch');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.messageQueue.length === 0) return;
|
||||||
|
|
||||||
|
this.isProcessing = true;
|
||||||
|
const batch = [...this.messageQueue];
|
||||||
|
this.messageQueue = [];
|
||||||
|
|
||||||
|
console.log(`🔁 Processing batch of size: ${batch.length}`);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await this.deviceStatusFirebaseService.addBatchDeviceStatusToOurDb(
|
||||||
|
batch.map((item) => ({
|
||||||
|
deviceTuyaUuid: item.devId,
|
||||||
|
status: item.status,
|
||||||
|
log: item.logData,
|
||||||
|
device: item.device,
|
||||||
|
})),
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('❌ Error processing batch:', error);
|
||||||
|
this.messageQueue.unshift(...batch); // retry
|
||||||
|
} finally {
|
||||||
|
this.isProcessing = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private extractMessageData(message: any): {
|
private extractMessageData(message: any): {
|
||||||
devId: string;
|
devId: string;
|
||||||
status: any;
|
status: any;
|
||||||
logData: any;
|
logData: any;
|
||||||
} {
|
} {
|
||||||
|
// console.log('Received message:', message);
|
||||||
|
|
||||||
const payloadData = message.payload.data;
|
const payloadData = message.payload.data;
|
||||||
|
|
||||||
if (this.isDevEnv) {
|
if (this.isDevEnv) {
|
||||||
|
5
libs/common/src/helper/timeToMinutes.ts
Normal file
5
libs/common/src/helper/timeToMinutes.ts
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
// Convert time string (HH:mm) to minutes
|
||||||
|
export function timeToMinutes(time: string): number {
|
||||||
|
const [hours, minutes] = time.split(':').map(Number);
|
||||||
|
return hours * 60 + minutes;
|
||||||
|
}
|
@ -49,12 +49,12 @@ export class TuyaService {
|
|||||||
path,
|
path,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.success) {
|
// if (!response.success) {
|
||||||
throw new HttpException(
|
// throw new HttpException(
|
||||||
`Error fetching device details: ${response.msg}`,
|
// `Error fetching device details: ${response.msg}`,
|
||||||
HttpStatus.BAD_REQUEST,
|
// HttpStatus.BAD_REQUEST,
|
||||||
);
|
// );
|
||||||
}
|
// }
|
||||||
|
|
||||||
return response.result;
|
return response.result;
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { RoleType } from '@app/common/constants/role.type.enum';
|
import { RoleType } from '@app/common/constants/role.type.enum';
|
||||||
import { UserStatusEnum } from '@app/common/constants/user-status.enum';
|
import { UserStatusEnum } from '@app/common/constants/user-status.enum';
|
||||||
import { IsEnum, IsNotEmpty, IsString } from 'class-validator';
|
import { IsEnum, IsNotEmpty, IsOptional, IsString } from 'class-validator';
|
||||||
|
|
||||||
export class InviteUserDto {
|
export class InviteUserDto {
|
||||||
@IsString()
|
@IsString()
|
||||||
@ -12,8 +12,12 @@ export class InviteUserDto {
|
|||||||
public email: string;
|
public email: string;
|
||||||
|
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsNotEmpty()
|
@IsOptional()
|
||||||
public jobTitle: string;
|
public jobTitle?: string;
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
@IsOptional()
|
||||||
|
public companyName?: string;
|
||||||
|
|
||||||
@IsEnum(UserStatusEnum)
|
@IsEnum(UserStatusEnum)
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
|
@ -15,17 +15,16 @@ import { ProjectEntity } from '../../project/entities';
|
|||||||
import { RoleTypeEntity } from '../../role-type/entities';
|
import { RoleTypeEntity } from '../../role-type/entities';
|
||||||
import { SpaceEntity } from '../../space/entities/space.entity';
|
import { SpaceEntity } from '../../space/entities/space.entity';
|
||||||
import { UserEntity } from '../../user/entities';
|
import { UserEntity } from '../../user/entities';
|
||||||
import { InviteUserDto, InviteUserSpaceDto } from '../dtos';
|
|
||||||
|
|
||||||
@Entity({ name: 'invite-user' })
|
@Entity({ name: 'invite-user' })
|
||||||
@Unique(['email', 'project'])
|
@Unique(['email', 'project'])
|
||||||
export class InviteUserEntity extends AbstractEntity<InviteUserDto> {
|
export class InviteUserEntity extends AbstractEntity {
|
||||||
@Column({
|
@Column({
|
||||||
type: 'uuid',
|
type: 'uuid',
|
||||||
default: () => 'gen_random_uuid()',
|
default: () => 'gen_random_uuid()',
|
||||||
nullable: false,
|
nullable: false,
|
||||||
})
|
})
|
||||||
public uuid: string;
|
uuid: string;
|
||||||
|
|
||||||
@Column({
|
@Column({
|
||||||
nullable: false,
|
nullable: false,
|
||||||
@ -37,6 +36,11 @@ export class InviteUserEntity extends AbstractEntity<InviteUserDto> {
|
|||||||
})
|
})
|
||||||
jobTitle: string;
|
jobTitle: string;
|
||||||
|
|
||||||
|
@Column({
|
||||||
|
nullable: true,
|
||||||
|
})
|
||||||
|
companyName: string;
|
||||||
|
|
||||||
@Column({
|
@Column({
|
||||||
nullable: false,
|
nullable: false,
|
||||||
enum: Object.values(UserStatusEnum),
|
enum: Object.values(UserStatusEnum),
|
||||||
@ -44,50 +48,67 @@ export class InviteUserEntity extends AbstractEntity<InviteUserDto> {
|
|||||||
status: string;
|
status: string;
|
||||||
|
|
||||||
@Column()
|
@Column()
|
||||||
public firstName: string;
|
firstName: string;
|
||||||
|
|
||||||
@Column({
|
@Column({
|
||||||
nullable: false,
|
nullable: false,
|
||||||
})
|
})
|
||||||
public lastName: string;
|
lastName: string;
|
||||||
|
|
||||||
@Column({
|
@Column({
|
||||||
nullable: true,
|
nullable: true,
|
||||||
})
|
})
|
||||||
public phoneNumber: string;
|
phoneNumber: string;
|
||||||
|
|
||||||
@Column({
|
@Column({
|
||||||
nullable: false,
|
nullable: false,
|
||||||
default: true,
|
default: true,
|
||||||
})
|
})
|
||||||
public isActive: boolean;
|
isActive: boolean;
|
||||||
|
|
||||||
@Column({
|
@Column({
|
||||||
nullable: false,
|
nullable: false,
|
||||||
default: true,
|
default: true,
|
||||||
})
|
})
|
||||||
public isEnabled: boolean;
|
isEnabled: boolean;
|
||||||
|
|
||||||
@Column({
|
@Column({
|
||||||
nullable: false,
|
nullable: false,
|
||||||
unique: true,
|
unique: true,
|
||||||
})
|
})
|
||||||
public invitationCode: string;
|
invitationCode: string;
|
||||||
|
|
||||||
|
@Column({
|
||||||
|
default: new Date(),
|
||||||
|
type: 'date',
|
||||||
|
})
|
||||||
|
accessStartDate: Date;
|
||||||
|
|
||||||
|
@Column({
|
||||||
|
type: 'date',
|
||||||
|
nullable: true,
|
||||||
|
})
|
||||||
|
accessEndDate?: Date;
|
||||||
|
|
||||||
@Column({
|
@Column({
|
||||||
nullable: false,
|
nullable: false,
|
||||||
enum: Object.values(RoleType),
|
enum: Object.values(RoleType),
|
||||||
})
|
})
|
||||||
public invitedBy: string;
|
invitedBy: string;
|
||||||
|
|
||||||
@ManyToOne(() => RoleTypeEntity, (roleType) => roleType.invitedUsers, {
|
@ManyToOne(() => RoleTypeEntity, (roleType) => roleType.invitedUsers, {
|
||||||
nullable: false,
|
nullable: false,
|
||||||
onDelete: 'CASCADE',
|
onDelete: 'CASCADE',
|
||||||
})
|
})
|
||||||
public roleType: RoleTypeEntity;
|
roleType: RoleTypeEntity;
|
||||||
|
|
||||||
@OneToOne(() => UserEntity, (user) => user.inviteUser, {
|
@OneToOne(() => UserEntity, (user) => user.inviteUser, {
|
||||||
nullable: true,
|
nullable: true,
|
||||||
onDelete: 'CASCADE',
|
onDelete: 'CASCADE',
|
||||||
})
|
})
|
||||||
@JoinColumn({ name: 'user_uuid' })
|
@JoinColumn({ name: 'user_uuid' })
|
||||||
user: UserEntity;
|
user: UserEntity;
|
||||||
|
|
||||||
@OneToMany(
|
@OneToMany(
|
||||||
() => InviteUserSpaceEntity,
|
() => InviteUserSpaceEntity,
|
||||||
(inviteUserSpace) => inviteUserSpace.inviteUser,
|
(inviteUserSpace) => inviteUserSpace.inviteUser,
|
||||||
@ -98,32 +119,34 @@ export class InviteUserEntity extends AbstractEntity<InviteUserDto> {
|
|||||||
nullable: true,
|
nullable: true,
|
||||||
})
|
})
|
||||||
@JoinColumn({ name: 'project_uuid' })
|
@JoinColumn({ name: 'project_uuid' })
|
||||||
public project: ProjectEntity;
|
project: ProjectEntity;
|
||||||
|
|
||||||
constructor(partial: Partial<InviteUserEntity>) {
|
constructor(partial: Partial<InviteUserEntity>) {
|
||||||
super();
|
super();
|
||||||
Object.assign(this, partial);
|
Object.assign(this, partial);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Entity({ name: 'invite-user-space' })
|
@Entity({ name: 'invite-user-space' })
|
||||||
@Unique(['inviteUser', 'space'])
|
@Unique(['inviteUser', 'space'])
|
||||||
export class InviteUserSpaceEntity extends AbstractEntity<InviteUserSpaceDto> {
|
export class InviteUserSpaceEntity extends AbstractEntity {
|
||||||
@Column({
|
@Column({
|
||||||
type: 'uuid',
|
type: 'uuid',
|
||||||
default: () => 'gen_random_uuid()',
|
default: () => 'gen_random_uuid()',
|
||||||
nullable: false,
|
nullable: false,
|
||||||
})
|
})
|
||||||
public uuid: string;
|
uuid: string;
|
||||||
|
|
||||||
@ManyToOne(() => InviteUserEntity, (inviteUser) => inviteUser.spaces, {
|
@ManyToOne(() => InviteUserEntity, (inviteUser) => inviteUser.spaces, {
|
||||||
onDelete: 'CASCADE',
|
onDelete: 'CASCADE',
|
||||||
})
|
})
|
||||||
@JoinColumn({ name: 'invite_user_uuid' })
|
@JoinColumn({ name: 'invite_user_uuid' })
|
||||||
public inviteUser: InviteUserEntity;
|
inviteUser: InviteUserEntity;
|
||||||
|
|
||||||
@ManyToOne(() => SpaceEntity, (space) => space.invitedUsers)
|
@ManyToOne(() => SpaceEntity, (space) => space.invitedUsers)
|
||||||
@JoinColumn({ name: 'space_uuid' })
|
@JoinColumn({ name: 'space_uuid' })
|
||||||
public space: SpaceEntity;
|
space: SpaceEntity;
|
||||||
|
|
||||||
constructor(partial: Partial<InviteUserSpaceEntity>) {
|
constructor(partial: Partial<InviteUserSpaceEntity>) {
|
||||||
super();
|
super();
|
||||||
Object.assign(this, partial);
|
Object.assign(this, partial);
|
||||||
|
12
libs/common/src/modules/booking/booking.repository.module.ts
Normal file
12
libs/common/src/modules/booking/booking.repository.module.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { Module } from '@nestjs/common';
|
||||||
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||||
|
import { BookableSpaceEntity } from './entities/bookable-space.entity';
|
||||||
|
import { BookingEntity } from './entities/booking.entity';
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
providers: [],
|
||||||
|
exports: [],
|
||||||
|
controllers: [],
|
||||||
|
imports: [TypeOrmModule.forFeature([BookableSpaceEntity, BookingEntity])],
|
||||||
|
})
|
||||||
|
export class BookingRepositoryModule {}
|
@ -0,0 +1,51 @@
|
|||||||
|
import { DaysEnum } from '@app/common/constants/days.enum';
|
||||||
|
import {
|
||||||
|
Column,
|
||||||
|
CreateDateColumn,
|
||||||
|
Entity,
|
||||||
|
JoinColumn,
|
||||||
|
OneToOne,
|
||||||
|
UpdateDateColumn,
|
||||||
|
} from 'typeorm';
|
||||||
|
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||||
|
import { SpaceEntity } from '../../space/entities/space.entity';
|
||||||
|
|
||||||
|
@Entity('bookable-space')
|
||||||
|
export class BookableSpaceEntity extends AbstractEntity {
|
||||||
|
@Column({
|
||||||
|
type: 'uuid',
|
||||||
|
default: () => 'gen_random_uuid()',
|
||||||
|
nullable: false,
|
||||||
|
})
|
||||||
|
public uuid: string;
|
||||||
|
|
||||||
|
@OneToOne(() => SpaceEntity, (space) => space.bookableConfig)
|
||||||
|
@JoinColumn({ name: 'space_uuid' })
|
||||||
|
space: SpaceEntity;
|
||||||
|
|
||||||
|
@Column({
|
||||||
|
type: 'enum',
|
||||||
|
enum: DaysEnum,
|
||||||
|
array: true,
|
||||||
|
nullable: false,
|
||||||
|
})
|
||||||
|
daysAvailable: DaysEnum[];
|
||||||
|
|
||||||
|
@Column({ type: 'time' })
|
||||||
|
startTime: string;
|
||||||
|
|
||||||
|
@Column({ type: 'time' })
|
||||||
|
endTime: string;
|
||||||
|
|
||||||
|
@Column({ type: Boolean, default: true })
|
||||||
|
active: boolean;
|
||||||
|
|
||||||
|
@Column({ type: 'int', default: null })
|
||||||
|
points?: number;
|
||||||
|
|
||||||
|
@CreateDateColumn()
|
||||||
|
createdAt: Date;
|
||||||
|
|
||||||
|
@UpdateDateColumn()
|
||||||
|
updatedAt: Date;
|
||||||
|
}
|
44
libs/common/src/modules/booking/entities/booking.entity.ts
Normal file
44
libs/common/src/modules/booking/entities/booking.entity.ts
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
import {
|
||||||
|
Column,
|
||||||
|
CreateDateColumn,
|
||||||
|
Entity,
|
||||||
|
ManyToOne,
|
||||||
|
UpdateDateColumn,
|
||||||
|
} from 'typeorm';
|
||||||
|
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||||
|
import { SpaceEntity } from '../../space/entities/space.entity';
|
||||||
|
import { UserEntity } from '../../user/entities';
|
||||||
|
|
||||||
|
@Entity('booking')
|
||||||
|
export class BookingEntity extends AbstractEntity {
|
||||||
|
@Column({
|
||||||
|
type: 'uuid',
|
||||||
|
default: () => 'gen_random_uuid()',
|
||||||
|
nullable: false,
|
||||||
|
})
|
||||||
|
public uuid: string;
|
||||||
|
|
||||||
|
@ManyToOne(() => SpaceEntity, (space) => space.bookableConfig)
|
||||||
|
space: SpaceEntity;
|
||||||
|
|
||||||
|
@ManyToOne(() => UserEntity, (user) => user.bookings)
|
||||||
|
user: UserEntity;
|
||||||
|
|
||||||
|
@Column({ type: Date, nullable: false })
|
||||||
|
date: Date;
|
||||||
|
|
||||||
|
@Column({ type: 'time' })
|
||||||
|
startTime: string;
|
||||||
|
|
||||||
|
@Column({ type: 'time' })
|
||||||
|
endTime: string;
|
||||||
|
|
||||||
|
@Column({ type: 'int', default: null })
|
||||||
|
cost?: number;
|
||||||
|
|
||||||
|
@CreateDateColumn()
|
||||||
|
createdAt: Date;
|
||||||
|
|
||||||
|
@UpdateDateColumn()
|
||||||
|
updatedAt: Date;
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
import { DataSource, Repository } from 'typeorm';
|
||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { BookableSpaceEntity } from '../entities/bookable-space.entity';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class BookableSpaceEntityRepository extends Repository<BookableSpaceEntity> {
|
||||||
|
constructor(private dataSource: DataSource) {
|
||||||
|
super(BookableSpaceEntity, dataSource.createEntityManager());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { DataSource, Repository } from 'typeorm';
|
||||||
|
import { BookingEntity } from '../entities/booking.entity';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class BookingEntityRepository extends Repository<BookingEntity> {
|
||||||
|
constructor(private dataSource: DataSource) {
|
||||||
|
super(BookingEntity, dataSource.createEntityManager());
|
||||||
|
}
|
||||||
|
}
|
@ -28,6 +28,11 @@ export class DeviceEntity extends AbstractEntity<DeviceDto> {
|
|||||||
})
|
})
|
||||||
deviceTuyaUuid: string;
|
deviceTuyaUuid: string;
|
||||||
|
|
||||||
|
@Column({
|
||||||
|
nullable: true,
|
||||||
|
})
|
||||||
|
deviceTuyaConstUuid: string;
|
||||||
|
|
||||||
@Column({
|
@Column({
|
||||||
nullable: true,
|
nullable: true,
|
||||||
default: true,
|
default: true,
|
||||||
|
@ -1,32 +0,0 @@
|
|||||||
import { Column, Entity, JoinColumn, ManyToOne } from 'typeorm';
|
|
||||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
|
||||||
import { SpaceEntity } from './space.entity';
|
|
||||||
import { Direction } from '@app/common/constants/direction.enum';
|
|
||||||
|
|
||||||
@Entity({ name: 'space-link' })
|
|
||||||
export class SpaceLinkEntity extends AbstractEntity {
|
|
||||||
@ManyToOne(() => SpaceEntity, { nullable: false, onDelete: 'CASCADE' })
|
|
||||||
@JoinColumn({ name: 'start_space_id' })
|
|
||||||
public startSpace: SpaceEntity;
|
|
||||||
|
|
||||||
@ManyToOne(() => SpaceEntity, { nullable: false, onDelete: 'CASCADE' })
|
|
||||||
@JoinColumn({ name: 'end_space_id' })
|
|
||||||
public endSpace: SpaceEntity;
|
|
||||||
|
|
||||||
@Column({
|
|
||||||
nullable: false,
|
|
||||||
default: false,
|
|
||||||
})
|
|
||||||
public disabled: boolean;
|
|
||||||
|
|
||||||
@Column({
|
|
||||||
nullable: false,
|
|
||||||
enum: Object.values(Direction),
|
|
||||||
})
|
|
||||||
direction: string;
|
|
||||||
|
|
||||||
constructor(partial: Partial<SpaceLinkEntity>) {
|
|
||||||
super();
|
|
||||||
Object.assign(this, partial);
|
|
||||||
}
|
|
||||||
}
|
|
@ -22,6 +22,11 @@ export class SpaceProductAllocationEntity extends AbstractEntity<SpaceProductAll
|
|||||||
})
|
})
|
||||||
public space: SpaceEntity;
|
public space: SpaceEntity;
|
||||||
|
|
||||||
|
@Column({
|
||||||
|
name: 'space_uuid',
|
||||||
|
})
|
||||||
|
public spaceUuid: string;
|
||||||
|
|
||||||
@ManyToOne(() => SpaceModelProductAllocationEntity, {
|
@ManyToOne(() => SpaceModelProductAllocationEntity, {
|
||||||
nullable: true,
|
nullable: true,
|
||||||
onDelete: 'SET NULL',
|
onDelete: 'SET NULL',
|
||||||
@ -31,9 +36,19 @@ export class SpaceProductAllocationEntity extends AbstractEntity<SpaceProductAll
|
|||||||
@ManyToOne(() => ProductEntity, { nullable: false, onDelete: 'CASCADE' })
|
@ManyToOne(() => ProductEntity, { nullable: false, onDelete: 'CASCADE' })
|
||||||
public product: ProductEntity;
|
public product: ProductEntity;
|
||||||
|
|
||||||
|
@Column({
|
||||||
|
name: 'product_uuid',
|
||||||
|
})
|
||||||
|
public productUuid: string;
|
||||||
|
|
||||||
@ManyToOne(() => NewTagEntity, { nullable: true, onDelete: 'CASCADE' })
|
@ManyToOne(() => NewTagEntity, { nullable: true, onDelete: 'CASCADE' })
|
||||||
public tag: NewTagEntity;
|
public tag: NewTagEntity;
|
||||||
|
|
||||||
|
@Column({
|
||||||
|
name: 'tag_uuid',
|
||||||
|
})
|
||||||
|
public tagUuid: string;
|
||||||
|
|
||||||
constructor(partial: Partial<SpaceProductAllocationEntity>) {
|
constructor(partial: Partial<SpaceProductAllocationEntity>) {
|
||||||
super();
|
super();
|
||||||
Object.assign(this, partial);
|
Object.assign(this, partial);
|
||||||
|
@ -1,18 +1,26 @@
|
|||||||
import { Column, Entity, JoinColumn, ManyToOne, OneToMany } from 'typeorm';
|
import {
|
||||||
import { SpaceDto } from '../dtos';
|
Column,
|
||||||
|
Entity,
|
||||||
|
JoinColumn,
|
||||||
|
ManyToOne,
|
||||||
|
OneToMany,
|
||||||
|
OneToOne,
|
||||||
|
} from 'typeorm';
|
||||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||||
import { UserSpaceEntity } from '../../user/entities';
|
import { AqiSpaceDailyPollutantStatsEntity } from '../../aqi/entities';
|
||||||
import { DeviceEntity } from '../../device/entities';
|
import { BookableSpaceEntity } from '../../booking/entities/bookable-space.entity';
|
||||||
|
import { BookingEntity } from '../../booking/entities/booking.entity';
|
||||||
import { CommunityEntity } from '../../community/entities';
|
import { CommunityEntity } from '../../community/entities';
|
||||||
import { SpaceLinkEntity } from './space-link.entity';
|
import { DeviceEntity } from '../../device/entities';
|
||||||
|
import { InviteUserSpaceEntity } from '../../Invite-user/entities';
|
||||||
|
import { SpaceDailyOccupancyDurationEntity } from '../../occupancy/entities';
|
||||||
|
import { PresenceSensorDailySpaceEntity } from '../../presence-sensor/entities';
|
||||||
import { SceneEntity } from '../../scene/entities';
|
import { SceneEntity } from '../../scene/entities';
|
||||||
import { SpaceModelEntity } from '../../space-model';
|
import { SpaceModelEntity } from '../../space-model';
|
||||||
import { InviteUserSpaceEntity } from '../../Invite-user/entities';
|
import { UserSpaceEntity } from '../../user/entities';
|
||||||
|
import { SpaceDto } from '../dtos';
|
||||||
import { SpaceProductAllocationEntity } from './space-product-allocation.entity';
|
import { SpaceProductAllocationEntity } from './space-product-allocation.entity';
|
||||||
import { SubspaceEntity } from './subspace/subspace.entity';
|
import { SubspaceEntity } from './subspace/subspace.entity';
|
||||||
import { PresenceSensorDailySpaceEntity } from '../../presence-sensor/entities';
|
|
||||||
import { AqiSpaceDailyPollutantStatsEntity } from '../../aqi/entities';
|
|
||||||
import { SpaceDailyOccupancyDurationEntity } from '../../occupancy/entities';
|
|
||||||
|
|
||||||
@Entity({ name: 'space' })
|
@Entity({ name: 'space' })
|
||||||
export class SpaceEntity extends AbstractEntity<SpaceDto> {
|
export class SpaceEntity extends AbstractEntity<SpaceDto> {
|
||||||
@ -39,12 +47,26 @@ export class SpaceEntity extends AbstractEntity<SpaceDto> {
|
|||||||
@JoinColumn({ name: 'community_id' })
|
@JoinColumn({ name: 'community_id' })
|
||||||
community: CommunityEntity;
|
community: CommunityEntity;
|
||||||
|
|
||||||
@ManyToOne(() => SpaceEntity, (space) => space.children, { nullable: true })
|
@Column({
|
||||||
|
name: 'community_id',
|
||||||
|
})
|
||||||
|
communityId: string;
|
||||||
|
|
||||||
|
@ManyToOne(() => SpaceEntity, (space) => space.children, {
|
||||||
|
nullable: true,
|
||||||
|
})
|
||||||
parent: SpaceEntity;
|
parent: SpaceEntity;
|
||||||
|
|
||||||
|
@Column({
|
||||||
|
name: 'parent_uuid',
|
||||||
|
nullable: true,
|
||||||
|
})
|
||||||
|
public parentUuid: string;
|
||||||
|
|
||||||
@OneToMany(() => SpaceEntity, (space) => space.parent, {
|
@OneToMany(() => SpaceEntity, (space) => space.parent, {
|
||||||
nullable: false,
|
nullable: false,
|
||||||
onDelete: 'CASCADE',
|
onDelete: 'CASCADE',
|
||||||
|
cascade: true,
|
||||||
})
|
})
|
||||||
children: SpaceEntity[];
|
children: SpaceEntity[];
|
||||||
|
|
||||||
@ -57,34 +79,24 @@ export class SpaceEntity extends AbstractEntity<SpaceDto> {
|
|||||||
})
|
})
|
||||||
public disabled: boolean;
|
public disabled: boolean;
|
||||||
|
|
||||||
|
@Column({
|
||||||
|
nullable: true,
|
||||||
|
type: Number,
|
||||||
|
})
|
||||||
|
public order?: number;
|
||||||
|
|
||||||
@OneToMany(() => SubspaceEntity, (subspace) => subspace.space, {
|
@OneToMany(() => SubspaceEntity, (subspace) => subspace.space, {
|
||||||
nullable: true,
|
nullable: true,
|
||||||
|
cascade: true,
|
||||||
})
|
})
|
||||||
subspaces?: SubspaceEntity[];
|
subspaces?: SubspaceEntity[];
|
||||||
|
|
||||||
// Position columns
|
|
||||||
@Column({ type: 'float', nullable: false, default: 0 })
|
|
||||||
public x: number; // X coordinate for position
|
|
||||||
|
|
||||||
@Column({ type: 'float', nullable: false, default: 0 })
|
|
||||||
public y: number; // Y coordinate for position
|
|
||||||
|
|
||||||
@OneToMany(
|
@OneToMany(
|
||||||
() => DeviceEntity,
|
() => DeviceEntity,
|
||||||
(devicesSpaceEntity) => devicesSpaceEntity.spaceDevice,
|
(devicesSpaceEntity) => devicesSpaceEntity.spaceDevice,
|
||||||
)
|
)
|
||||||
devices: DeviceEntity[];
|
devices: DeviceEntity[];
|
||||||
|
|
||||||
@OneToMany(() => SpaceLinkEntity, (connection) => connection.startSpace, {
|
|
||||||
nullable: true,
|
|
||||||
})
|
|
||||||
public outgoingConnections: SpaceLinkEntity[];
|
|
||||||
|
|
||||||
@OneToMany(() => SpaceLinkEntity, (connection) => connection.endSpace, {
|
|
||||||
nullable: true,
|
|
||||||
})
|
|
||||||
public incomingConnections: SpaceLinkEntity[];
|
|
||||||
|
|
||||||
@Column({
|
@Column({
|
||||||
nullable: true,
|
nullable: true,
|
||||||
type: 'text',
|
type: 'text',
|
||||||
@ -126,6 +138,12 @@ export class SpaceEntity extends AbstractEntity<SpaceDto> {
|
|||||||
)
|
)
|
||||||
occupancyDaily: SpaceDailyOccupancyDurationEntity[];
|
occupancyDaily: SpaceDailyOccupancyDurationEntity[];
|
||||||
|
|
||||||
|
@OneToOne(() => BookableSpaceEntity, (bookable) => bookable.space)
|
||||||
|
bookableConfig: BookableSpaceEntity;
|
||||||
|
|
||||||
|
@OneToMany(() => BookingEntity, (booking) => booking.space)
|
||||||
|
bookings: BookingEntity[];
|
||||||
|
|
||||||
constructor(partial: Partial<SpaceEntity>) {
|
constructor(partial: Partial<SpaceEntity>) {
|
||||||
super();
|
super();
|
||||||
Object.assign(this, partial);
|
Object.assign(this, partial);
|
||||||
|
@ -22,6 +22,11 @@ export class SubspaceProductAllocationEntity extends AbstractEntity<SubspaceProd
|
|||||||
})
|
})
|
||||||
public subspace: SubspaceEntity;
|
public subspace: SubspaceEntity;
|
||||||
|
|
||||||
|
@Column({
|
||||||
|
name: 'subspace_uuid',
|
||||||
|
})
|
||||||
|
public subspaceUuid: string;
|
||||||
|
|
||||||
@ManyToOne(() => SubspaceModelProductAllocationEntity, {
|
@ManyToOne(() => SubspaceModelProductAllocationEntity, {
|
||||||
nullable: true,
|
nullable: true,
|
||||||
onDelete: 'SET NULL',
|
onDelete: 'SET NULL',
|
||||||
@ -31,9 +36,19 @@ export class SubspaceProductAllocationEntity extends AbstractEntity<SubspaceProd
|
|||||||
@ManyToOne(() => ProductEntity, { nullable: false, onDelete: 'CASCADE' })
|
@ManyToOne(() => ProductEntity, { nullable: false, onDelete: 'CASCADE' })
|
||||||
public product: ProductEntity;
|
public product: ProductEntity;
|
||||||
|
|
||||||
|
@Column({
|
||||||
|
name: 'product_uuid',
|
||||||
|
})
|
||||||
|
public productUuid: string;
|
||||||
|
|
||||||
@ManyToOne(() => NewTagEntity, { nullable: true, onDelete: 'CASCADE' })
|
@ManyToOne(() => NewTagEntity, { nullable: true, onDelete: 'CASCADE' })
|
||||||
public tag: NewTagEntity;
|
public tag: NewTagEntity;
|
||||||
|
|
||||||
|
@Column({
|
||||||
|
name: 'tag_uuid',
|
||||||
|
})
|
||||||
|
public tagUuid: string;
|
||||||
|
|
||||||
constructor(partial: Partial<SubspaceProductAllocationEntity>) {
|
constructor(partial: Partial<SubspaceProductAllocationEntity>) {
|
||||||
super();
|
super();
|
||||||
Object.assign(this, partial);
|
Object.assign(this, partial);
|
||||||
|
@ -26,6 +26,11 @@ export class SubspaceEntity extends AbstractEntity<SubspaceDto> {
|
|||||||
@JoinColumn({ name: 'space_uuid' })
|
@JoinColumn({ name: 'space_uuid' })
|
||||||
space: SpaceEntity;
|
space: SpaceEntity;
|
||||||
|
|
||||||
|
@Column({
|
||||||
|
name: 'space_uuid',
|
||||||
|
})
|
||||||
|
public spaceUuid: string;
|
||||||
|
|
||||||
@Column({
|
@Column({
|
||||||
nullable: false,
|
nullable: false,
|
||||||
default: false,
|
default: false,
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { DataSource, Repository } from 'typeorm';
|
import { DataSource, Repository } from 'typeorm';
|
||||||
import { InviteSpaceEntity } from '../entities/invite-space.entity';
|
import { InviteSpaceEntity } from '../entities/invite-space.entity';
|
||||||
import { SpaceLinkEntity } from '../entities/space-link.entity';
|
|
||||||
import { SpaceProductAllocationEntity } from '../entities/space-product-allocation.entity';
|
import { SpaceProductAllocationEntity } from '../entities/space-product-allocation.entity';
|
||||||
import { SpaceEntity } from '../entities/space.entity';
|
import { SpaceEntity } from '../entities/space.entity';
|
||||||
|
|
||||||
@ -12,13 +11,6 @@ export class SpaceRepository extends Repository<SpaceEntity> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Injectable()
|
|
||||||
export class SpaceLinkRepository extends Repository<SpaceLinkEntity> {
|
|
||||||
constructor(private dataSource: DataSource) {
|
|
||||||
super(SpaceLinkEntity, dataSource.createEntityManager());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class InviteSpaceRepository extends Repository<InviteSpaceEntity> {
|
export class InviteSpaceRepository extends Repository<InviteSpaceEntity> {
|
||||||
constructor(private dataSource: DataSource) {
|
constructor(private dataSource: DataSource) {
|
||||||
|
37
libs/common/src/modules/timer/entities/timer.entity.ts
Normal file
37
libs/common/src/modules/timer/entities/timer.entity.ts
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
import { TimerJobTypeEnum } from '@app/common/constants/timer-job-type.enum';
|
||||||
|
import { Column, Entity } from 'typeorm';
|
||||||
|
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||||
|
|
||||||
|
@Entity({ name: 'timer' })
|
||||||
|
export class TimerEntity extends AbstractEntity {
|
||||||
|
@Column({
|
||||||
|
type: 'uuid',
|
||||||
|
default: () => 'gen_random_uuid()',
|
||||||
|
nullable: false,
|
||||||
|
})
|
||||||
|
uuid: string;
|
||||||
|
|
||||||
|
@Column({
|
||||||
|
nullable: false,
|
||||||
|
enum: Object.values(TimerJobTypeEnum),
|
||||||
|
type: String,
|
||||||
|
})
|
||||||
|
type: TimerJobTypeEnum;
|
||||||
|
|
||||||
|
@Column({
|
||||||
|
nullable: false,
|
||||||
|
type: 'date',
|
||||||
|
})
|
||||||
|
triggerDate: Date;
|
||||||
|
|
||||||
|
@Column({
|
||||||
|
type: 'jsonb',
|
||||||
|
nullable: true,
|
||||||
|
})
|
||||||
|
metadata?: Record<string, any>;
|
||||||
|
|
||||||
|
constructor(partial: Partial<TimerEntity>) {
|
||||||
|
super();
|
||||||
|
Object.assign(this, partial);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
import { DataSource, Repository } from 'typeorm';
|
||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { TimerEntity } from '../entities/timer.entity';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class TimerRepository extends Repository<TimerEntity> {
|
||||||
|
constructor(private dataSource: DataSource) {
|
||||||
|
super(TimerEntity, dataSource.createEntityManager());
|
||||||
|
}
|
||||||
|
}
|
12
libs/common/src/modules/timer/timer.repository.module.ts
Normal file
12
libs/common/src/modules/timer/timer.repository.module.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { Global, Module } from '@nestjs/common';
|
||||||
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||||
|
import { TimerEntity } from './entities/timer.entity';
|
||||||
|
import { TimerRepository } from './repositories/timer.repository';
|
||||||
|
|
||||||
|
@Global()
|
||||||
|
@Module({
|
||||||
|
imports: [TypeOrmModule.forFeature([TimerEntity])],
|
||||||
|
providers: [TimerRepository],
|
||||||
|
exports: [TimerRepository],
|
||||||
|
})
|
||||||
|
export class TimerRepositoryModule {}
|
@ -11,6 +11,7 @@ import {
|
|||||||
} from 'typeorm';
|
} from 'typeorm';
|
||||||
import { OtpType } from '../../../../src/constants/otp-type.enum';
|
import { OtpType } from '../../../../src/constants/otp-type.enum';
|
||||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||||
|
import { BookingEntity } from '../../booking/entities/booking.entity';
|
||||||
import { ClientEntity } from '../../client/entities';
|
import { ClientEntity } from '../../client/entities';
|
||||||
import {
|
import {
|
||||||
DeviceNotificationEntity,
|
DeviceNotificationEntity,
|
||||||
@ -82,6 +83,12 @@ export class UserEntity extends AbstractEntity<UserDto> {
|
|||||||
})
|
})
|
||||||
public isActive: boolean;
|
public isActive: boolean;
|
||||||
|
|
||||||
|
@Column({
|
||||||
|
nullable: true,
|
||||||
|
type: Number,
|
||||||
|
})
|
||||||
|
public bookingPoints?: number;
|
||||||
|
|
||||||
@Column({ default: false })
|
@Column({ default: false })
|
||||||
hasAcceptedWebAgreement: boolean;
|
hasAcceptedWebAgreement: boolean;
|
||||||
|
|
||||||
@ -94,6 +101,9 @@ export class UserEntity extends AbstractEntity<UserDto> {
|
|||||||
@Column({ type: 'timestamp', nullable: true })
|
@Column({ type: 'timestamp', nullable: true })
|
||||||
appAgreementAcceptedAt: Date;
|
appAgreementAcceptedAt: Date;
|
||||||
|
|
||||||
|
@Column({ type: Boolean, default: false })
|
||||||
|
bookingEnabled: boolean;
|
||||||
|
|
||||||
@OneToMany(() => UserSpaceEntity, (userSpace) => userSpace.user, {
|
@OneToMany(() => UserSpaceEntity, (userSpace) => userSpace.user, {
|
||||||
onDelete: 'CASCADE',
|
onDelete: 'CASCADE',
|
||||||
})
|
})
|
||||||
@ -115,6 +125,9 @@ export class UserEntity extends AbstractEntity<UserDto> {
|
|||||||
)
|
)
|
||||||
deviceUserNotification: DeviceNotificationEntity[];
|
deviceUserNotification: DeviceNotificationEntity[];
|
||||||
|
|
||||||
|
@OneToMany(() => BookingEntity, (booking) => booking.user)
|
||||||
|
bookings: BookingEntity[];
|
||||||
|
|
||||||
@ManyToOne(() => RegionEntity, (region) => region.users, { nullable: true })
|
@ManyToOne(() => RegionEntity, (region) => region.users, { nullable: true })
|
||||||
region: RegionEntity;
|
region: RegionEntity;
|
||||||
@ManyToOne(() => TimeZoneEntity, (timezone) => timezone.users, {
|
@ManyToOne(() => TimeZoneEntity, (timezone) => timezone.users, {
|
||||||
|
@ -61,10 +61,6 @@ export class SuperAdminSeeder {
|
|||||||
lastName: 'Admin',
|
lastName: 'Admin',
|
||||||
isUserVerified: true,
|
isUserVerified: true,
|
||||||
isActive: true,
|
isActive: true,
|
||||||
hasAcceptedAppAgreement: true,
|
|
||||||
hasAcceptedWebAgreement: true,
|
|
||||||
appAgreementAcceptedAt: new Date(),
|
|
||||||
webAgreementAcceptedAt: new Date(),
|
|
||||||
roleType: { uuid: defaultUserRoleUuid },
|
roleType: { uuid: defaultUserRoleUuid },
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
WITH params AS (
|
WITH params AS (
|
||||||
SELECT
|
SELECT
|
||||||
TO_DATE(NULLIF($1, ''), 'YYYY-MM-DD') AS event_date,
|
TO_DATE(NULLIF($1, ''), 'YYYY-MM-DD') AS event_date
|
||||||
$2::uuid AS space_id
|
|
||||||
),
|
),
|
||||||
|
|
||||||
-- Query Pipeline Starts Here
|
-- Query Pipeline Starts Here
|
||||||
@ -277,7 +276,10 @@ SELECT
|
|||||||
a.daily_avg_ch2o,a.daily_max_ch2o, a.daily_min_ch2o
|
a.daily_avg_ch2o,a.daily_max_ch2o, a.daily_min_ch2o
|
||||||
FROM daily_percentages p
|
FROM daily_percentages p
|
||||||
LEFT JOIN daily_averages a
|
LEFT JOIN daily_averages a
|
||||||
ON p.space_id = a.space_id AND p.event_date = a.event_date
|
ON p.space_id = a.space_id
|
||||||
|
AND p.event_date = a.event_date
|
||||||
|
JOIN params
|
||||||
|
ON params.event_date = a.event_date
|
||||||
ORDER BY p.space_id, p.event_date)
|
ORDER BY p.space_id, p.event_date)
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
WITH params AS (
|
WITH params AS (
|
||||||
SELECT
|
SELECT
|
||||||
TO_DATE(NULLIF($1, ''), 'YYYY-MM-DD') AS event_date,
|
TO_DATE(NULLIF($1, ''), 'YYYY-MM-DD') AS event_date
|
||||||
$2::uuid AS space_id
|
|
||||||
),
|
),
|
||||||
|
|
||||||
presence_logs AS (
|
presence_logs AS (
|
||||||
@ -86,8 +85,7 @@ final_data AS (
|
|||||||
ROUND(LEAST(raw_occupied_seconds, 86400) / 86400.0 * 100, 2) AS occupancy_percentage
|
ROUND(LEAST(raw_occupied_seconds, 86400) / 86400.0 * 100, 2) AS occupancy_percentage
|
||||||
FROM summed_intervals s
|
FROM summed_intervals s
|
||||||
JOIN params p
|
JOIN params p
|
||||||
ON p.space_id = s.space_id
|
ON p.event_date = s.event_date
|
||||||
AND p.event_date = s.event_date
|
|
||||||
)
|
)
|
||||||
|
|
||||||
INSERT INTO public."space-daily-occupancy-duration" (
|
INSERT INTO public."space-daily-occupancy-duration" (
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
WITH params AS (
|
WITH params AS (
|
||||||
SELECT
|
SELECT
|
||||||
$1::uuid AS device_id,
|
$1::date AS target_date
|
||||||
$2::date AS target_date
|
|
||||||
),
|
),
|
||||||
total_energy AS (
|
total_energy AS (
|
||||||
SELECT
|
SELECT
|
||||||
@ -14,8 +13,7 @@ total_energy AS (
|
|||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer AS max_value
|
||||||
FROM "device-status-log" log, params
|
FROM "device-status-log" log, params
|
||||||
WHERE log.code = 'EnergyConsumed'
|
WHERE log.code = 'EnergyConsumed'
|
||||||
AND log.device_id = params.device_id
|
AND log.event_time::date = params.target_date
|
||||||
AND log.event_time::date = params.target_date
|
|
||||||
GROUP BY 1,2,3,4,5
|
GROUP BY 1,2,3,4,5
|
||||||
),
|
),
|
||||||
energy_phase_A AS (
|
energy_phase_A AS (
|
||||||
@ -29,8 +27,7 @@ energy_phase_A AS (
|
|||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer AS max_value
|
||||||
FROM "device-status-log" log, params
|
FROM "device-status-log" log, params
|
||||||
WHERE log.code = 'EnergyConsumedA'
|
WHERE log.code = 'EnergyConsumedA'
|
||||||
AND log.device_id = params.device_id
|
AND log.event_time::date = params.target_date
|
||||||
AND log.event_time::date = params.target_date
|
|
||||||
GROUP BY 1,2,3,4,5
|
GROUP BY 1,2,3,4,5
|
||||||
),
|
),
|
||||||
energy_phase_B AS (
|
energy_phase_B AS (
|
||||||
@ -44,8 +41,7 @@ energy_phase_B AS (
|
|||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer AS max_value
|
||||||
FROM "device-status-log" log, params
|
FROM "device-status-log" log, params
|
||||||
WHERE log.code = 'EnergyConsumedB'
|
WHERE log.code = 'EnergyConsumedB'
|
||||||
AND log.device_id = params.device_id
|
AND log.event_time::date = params.target_date
|
||||||
AND log.event_time::date = params.target_date
|
|
||||||
GROUP BY 1,2,3,4,5
|
GROUP BY 1,2,3,4,5
|
||||||
),
|
),
|
||||||
energy_phase_C AS (
|
energy_phase_C AS (
|
||||||
@ -59,8 +55,7 @@ energy_phase_C AS (
|
|||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer AS max_value
|
||||||
FROM "device-status-log" log, params
|
FROM "device-status-log" log, params
|
||||||
WHERE log.code = 'EnergyConsumedC'
|
WHERE log.code = 'EnergyConsumedC'
|
||||||
AND log.device_id = params.device_id
|
AND log.event_time::date = params.target_date
|
||||||
AND log.event_time::date = params.target_date
|
|
||||||
GROUP BY 1,2,3,4,5
|
GROUP BY 1,2,3,4,5
|
||||||
),
|
),
|
||||||
final_data AS (
|
final_data AS (
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
WITH params AS (
|
WITH params AS (
|
||||||
SELECT
|
SELECT
|
||||||
$1::uuid AS device_id,
|
$1::date AS target_date
|
||||||
$2::date AS target_date,
|
|
||||||
$3::text AS target_hour
|
|
||||||
),
|
),
|
||||||
total_energy AS (
|
total_energy AS (
|
||||||
SELECT
|
SELECT
|
||||||
@ -15,9 +13,7 @@ total_energy AS (
|
|||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer AS max_value
|
||||||
FROM "device-status-log" log, params
|
FROM "device-status-log" log, params
|
||||||
WHERE log.code = 'EnergyConsumed'
|
WHERE log.code = 'EnergyConsumed'
|
||||||
AND log.device_id = params.device_id
|
|
||||||
AND log.event_time::date = params.target_date
|
AND log.event_time::date = params.target_date
|
||||||
AND EXTRACT(HOUR FROM log.event_time)::text = params.target_hour
|
|
||||||
GROUP BY 1,2,3,4,5
|
GROUP BY 1,2,3,4,5
|
||||||
),
|
),
|
||||||
energy_phase_A AS (
|
energy_phase_A AS (
|
||||||
@ -31,9 +27,7 @@ energy_phase_A AS (
|
|||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer AS max_value
|
||||||
FROM "device-status-log" log, params
|
FROM "device-status-log" log, params
|
||||||
WHERE log.code = 'EnergyConsumedA'
|
WHERE log.code = 'EnergyConsumedA'
|
||||||
AND log.device_id = params.device_id
|
|
||||||
AND log.event_time::date = params.target_date
|
AND log.event_time::date = params.target_date
|
||||||
AND EXTRACT(HOUR FROM log.event_time)::text = params.target_hour
|
|
||||||
GROUP BY 1,2,3,4,5
|
GROUP BY 1,2,3,4,5
|
||||||
),
|
),
|
||||||
energy_phase_B AS (
|
energy_phase_B AS (
|
||||||
@ -47,9 +41,7 @@ energy_phase_B AS (
|
|||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer AS max_value
|
||||||
FROM "device-status-log" log, params
|
FROM "device-status-log" log, params
|
||||||
WHERE log.code = 'EnergyConsumedB'
|
WHERE log.code = 'EnergyConsumedB'
|
||||||
AND log.device_id = params.device_id
|
|
||||||
AND log.event_time::date = params.target_date
|
AND log.event_time::date = params.target_date
|
||||||
AND EXTRACT(HOUR FROM log.event_time)::text = params.target_hour
|
|
||||||
GROUP BY 1,2,3,4,5
|
GROUP BY 1,2,3,4,5
|
||||||
),
|
),
|
||||||
energy_phase_C AS (
|
energy_phase_C AS (
|
||||||
@ -63,9 +55,7 @@ energy_phase_C AS (
|
|||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer AS max_value
|
||||||
FROM "device-status-log" log, params
|
FROM "device-status-log" log, params
|
||||||
WHERE log.code = 'EnergyConsumedC'
|
WHERE log.code = 'EnergyConsumedC'
|
||||||
AND log.device_id = params.device_id
|
|
||||||
AND log.event_time::date = params.target_date
|
AND log.event_time::date = params.target_date
|
||||||
AND EXTRACT(HOUR FROM log.event_time)::text = params.target_hour
|
|
||||||
GROUP BY 1,2,3,4,5
|
GROUP BY 1,2,3,4,5
|
||||||
),
|
),
|
||||||
final_data AS (
|
final_data AS (
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
WITH params AS (
|
WITH params AS (
|
||||||
SELECT
|
SELECT
|
||||||
$1::uuid AS device_id,
|
$1::text AS target_month -- Format should match 'MM-YYYY'
|
||||||
$2::text AS target_month -- Format should match 'MM-YYYY'
|
|
||||||
),
|
),
|
||||||
total_energy AS (
|
total_energy AS (
|
||||||
SELECT
|
SELECT
|
||||||
@ -14,7 +13,6 @@ total_energy AS (
|
|||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer AS max_value
|
||||||
FROM "device-status-log" log, params
|
FROM "device-status-log" log, params
|
||||||
WHERE log.code = 'EnergyConsumed'
|
WHERE log.code = 'EnergyConsumed'
|
||||||
AND log.device_id = params.device_id
|
|
||||||
AND TO_CHAR(log.event_time, 'MM-YYYY') = params.target_month
|
AND TO_CHAR(log.event_time, 'MM-YYYY') = params.target_month
|
||||||
GROUP BY 1,2,3,4,5
|
GROUP BY 1,2,3,4,5
|
||||||
),
|
),
|
||||||
@ -29,7 +27,6 @@ energy_phase_A AS (
|
|||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer AS max_value
|
||||||
FROM "device-status-log" log, params
|
FROM "device-status-log" log, params
|
||||||
WHERE log.code = 'EnergyConsumedA'
|
WHERE log.code = 'EnergyConsumedA'
|
||||||
AND log.device_id = params.device_id
|
|
||||||
AND TO_CHAR(log.event_time, 'MM-YYYY') = params.target_month
|
AND TO_CHAR(log.event_time, 'MM-YYYY') = params.target_month
|
||||||
GROUP BY 1,2,3,4,5
|
GROUP BY 1,2,3,4,5
|
||||||
),
|
),
|
||||||
@ -44,7 +41,6 @@ energy_phase_B AS (
|
|||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer AS max_value
|
||||||
FROM "device-status-log" log, params
|
FROM "device-status-log" log, params
|
||||||
WHERE log.code = 'EnergyConsumedB'
|
WHERE log.code = 'EnergyConsumedB'
|
||||||
AND log.device_id = params.device_id
|
|
||||||
AND TO_CHAR(log.event_time, 'MM-YYYY') = params.target_month
|
AND TO_CHAR(log.event_time, 'MM-YYYY') = params.target_month
|
||||||
GROUP BY 1,2,3,4,5
|
GROUP BY 1,2,3,4,5
|
||||||
),
|
),
|
||||||
@ -59,7 +55,6 @@ energy_phase_C AS (
|
|||||||
MAX(log.value)::integer AS max_value
|
MAX(log.value)::integer AS max_value
|
||||||
FROM "device-status-log" log, params
|
FROM "device-status-log" log, params
|
||||||
WHERE log.code = 'EnergyConsumedC'
|
WHERE log.code = 'EnergyConsumedC'
|
||||||
AND log.device_id = params.device_id
|
|
||||||
AND TO_CHAR(log.event_time, 'MM-YYYY') = params.target_month
|
AND TO_CHAR(log.event_time, 'MM-YYYY') = params.target_month
|
||||||
GROUP BY 1,2,3,4,5
|
GROUP BY 1,2,3,4,5
|
||||||
),
|
),
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
WITH params AS (
|
WITH params AS (
|
||||||
SELECT
|
SELECT
|
||||||
TO_DATE(NULLIF($1, ''), 'YYYY-MM-DD') AS event_date,
|
TO_DATE(NULLIF($1, ''), 'YYYY-MM-DD') AS event_date
|
||||||
$2::uuid AS space_id
|
|
||||||
),
|
),
|
||||||
|
|
||||||
device_logs AS (
|
device_logs AS (
|
||||||
@ -87,8 +86,7 @@ SELECT summary.space_id,
|
|||||||
count_total_presence_detected
|
count_total_presence_detected
|
||||||
FROM summary
|
FROM summary
|
||||||
JOIN params P ON true
|
JOIN params P ON true
|
||||||
where summary.space_id = P.space_id
|
where (P.event_date IS NULL or summary.event_date::date = P.event_date)
|
||||||
and (P.event_date IS NULL or summary.event_date::date = P.event_date)
|
|
||||||
ORDER BY space_id, event_date)
|
ORDER BY space_id, event_date)
|
||||||
|
|
||||||
|
|
||||||
|
8
libs/common/src/util/email/batch-email.interface.ts
Normal file
8
libs/common/src/util/email/batch-email.interface.ts
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
export interface BatchEmailData {
|
||||||
|
base: { from: { email: string }; template_uuid: string };
|
||||||
|
requests: Array<{
|
||||||
|
to: { email: string }[];
|
||||||
|
template_variables: Record<string, any>;
|
||||||
|
}>;
|
||||||
|
isBatch: true;
|
||||||
|
}
|
@ -2,14 +2,16 @@ import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
|||||||
import { ConfigService } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import * as nodemailer from 'nodemailer';
|
import * as nodemailer from 'nodemailer';
|
||||||
import {
|
import Mail from 'nodemailer/lib/mailer';
|
||||||
SEND_EMAIL_API_URL_DEV,
|
import { BatchEmailData } from './batch-email.interface';
|
||||||
SEND_EMAIL_API_URL_PROD,
|
import { SingleEmailData } from './single-email.interface';
|
||||||
} from '../constants/mail-trap';
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class EmailService {
|
export class EmailService {
|
||||||
private smtpConfig: any;
|
private smtpConfig: any;
|
||||||
|
private API_TOKEN: string;
|
||||||
|
private SEND_EMAIL_API_URL: string;
|
||||||
|
private BATCH_EMAIL_API_URL: string;
|
||||||
|
|
||||||
constructor(private readonly configService: ConfigService) {
|
constructor(private readonly configService: ConfigService) {
|
||||||
this.smtpConfig = {
|
this.smtpConfig = {
|
||||||
@ -22,6 +24,15 @@ export class EmailService {
|
|||||||
pass: this.configService.get<string>('email-config.SMTP_PASSWORD'),
|
pass: this.configService.get<string>('email-config.SMTP_PASSWORD'),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
this.API_TOKEN = this.configService.get<string>(
|
||||||
|
'email-config.MAILTRAP_API_TOKEN',
|
||||||
|
);
|
||||||
|
this.SEND_EMAIL_API_URL = this.configService.get<string>(
|
||||||
|
'email-config.SEND_EMAIL_API_URL',
|
||||||
|
);
|
||||||
|
this.BATCH_EMAIL_API_URL = this.configService.get<string>(
|
||||||
|
'email-config.BATCH_EMAIL_API_URL',
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async sendEmail(
|
async sendEmail(
|
||||||
@ -31,7 +42,7 @@ export class EmailService {
|
|||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const transporter = nodemailer.createTransport(this.smtpConfig);
|
const transporter = nodemailer.createTransport(this.smtpConfig);
|
||||||
|
|
||||||
const mailOptions = {
|
const mailOptions: Mail.Options = {
|
||||||
from: this.smtpConfig.sender,
|
from: this.smtpConfig.sender,
|
||||||
to: email,
|
to: email,
|
||||||
subject,
|
subject,
|
||||||
@ -44,13 +55,6 @@ export class EmailService {
|
|||||||
email: string,
|
email: string,
|
||||||
emailInvitationData: any,
|
emailInvitationData: any,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const isProduction = process.env.NODE_ENV === 'production';
|
|
||||||
const API_TOKEN = this.configService.get<string>(
|
|
||||||
'email-config.MAILTRAP_API_TOKEN',
|
|
||||||
);
|
|
||||||
const API_URL = isProduction
|
|
||||||
? SEND_EMAIL_API_URL_PROD
|
|
||||||
: SEND_EMAIL_API_URL_DEV;
|
|
||||||
const TEMPLATE_UUID = this.configService.get<string>(
|
const TEMPLATE_UUID = this.configService.get<string>(
|
||||||
'email-config.MAILTRAP_INVITATION_TEMPLATE_UUID',
|
'email-config.MAILTRAP_INVITATION_TEMPLATE_UUID',
|
||||||
);
|
);
|
||||||
@ -68,21 +72,12 @@ export class EmailService {
|
|||||||
template_variables: emailInvitationData,
|
template_variables: emailInvitationData,
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
return this.sendEmailWithTemplateV2({
|
||||||
await axios.post(API_URL, emailData, {
|
...emailData,
|
||||||
headers: {
|
isBatch: false,
|
||||||
Authorization: `Bearer ${API_TOKEN}`,
|
});
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
throw new HttpException(
|
|
||||||
error.response?.data?.message ||
|
|
||||||
'Error sending email using Mailtrap template',
|
|
||||||
error.response?.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async sendEmailWithTemplate({
|
async sendEmailWithTemplate({
|
||||||
email,
|
email,
|
||||||
name,
|
name,
|
||||||
@ -94,14 +89,6 @@ export class EmailService {
|
|||||||
isEnable: boolean;
|
isEnable: boolean;
|
||||||
isDelete: boolean;
|
isDelete: boolean;
|
||||||
}): Promise<void> {
|
}): Promise<void> {
|
||||||
const isProduction = process.env.NODE_ENV === 'production';
|
|
||||||
const API_TOKEN = this.configService.get<string>(
|
|
||||||
'email-config.MAILTRAP_API_TOKEN',
|
|
||||||
);
|
|
||||||
const API_URL = isProduction
|
|
||||||
? SEND_EMAIL_API_URL_PROD
|
|
||||||
: SEND_EMAIL_API_URL_DEV;
|
|
||||||
|
|
||||||
// Determine the template UUID based on the arguments
|
// Determine the template UUID based on the arguments
|
||||||
const templateUuid = isDelete
|
const templateUuid = isDelete
|
||||||
? this.configService.get<string>(
|
? this.configService.get<string>(
|
||||||
@ -128,32 +115,16 @@ export class EmailService {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
return this.sendEmailWithTemplateV2({
|
||||||
await axios.post(API_URL, emailData, {
|
...emailData,
|
||||||
headers: {
|
isBatch: false,
|
||||||
Authorization: `Bearer ${API_TOKEN}`,
|
});
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
throw new HttpException(
|
|
||||||
error.response?.data?.message ||
|
|
||||||
'Error sending email using Mailtrap template',
|
|
||||||
error.response?.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async sendEditUserEmailWithTemplate(
|
async sendEditUserEmailWithTemplate(
|
||||||
email: string,
|
email: string,
|
||||||
emailEditData: any,
|
emailEditData: any,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const isProduction = process.env.NODE_ENV === 'production';
|
|
||||||
const API_TOKEN = this.configService.get<string>(
|
|
||||||
'email-config.MAILTRAP_API_TOKEN',
|
|
||||||
);
|
|
||||||
const API_URL = isProduction
|
|
||||||
? SEND_EMAIL_API_URL_PROD
|
|
||||||
: SEND_EMAIL_API_URL_DEV;
|
|
||||||
const TEMPLATE_UUID = this.configService.get<string>(
|
const TEMPLATE_UUID = this.configService.get<string>(
|
||||||
'email-config.MAILTRAP_EDIT_USER_TEMPLATE_UUID',
|
'email-config.MAILTRAP_EDIT_USER_TEMPLATE_UUID',
|
||||||
);
|
);
|
||||||
@ -171,32 +142,15 @@ export class EmailService {
|
|||||||
template_variables: emailEditData,
|
template_variables: emailEditData,
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
return this.sendEmailWithTemplateV2({
|
||||||
await axios.post(API_URL, emailData, {
|
...emailData,
|
||||||
headers: {
|
isBatch: false,
|
||||||
Authorization: `Bearer ${API_TOKEN}`,
|
});
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
throw new HttpException(
|
|
||||||
error.response?.data?.message ||
|
|
||||||
'Error sending email using Mailtrap template',
|
|
||||||
error.response?.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
async sendOtpEmailWithTemplate(
|
async sendOtpEmailWithTemplate(
|
||||||
email: string,
|
email: string,
|
||||||
emailEditData: any,
|
emailEditData: any,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const isProduction = process.env.NODE_ENV === 'production';
|
|
||||||
const API_TOKEN = this.configService.get<string>(
|
|
||||||
'email-config.MAILTRAP_API_TOKEN',
|
|
||||||
);
|
|
||||||
const API_URL = isProduction
|
|
||||||
? SEND_EMAIL_API_URL_PROD
|
|
||||||
: SEND_EMAIL_API_URL_DEV;
|
|
||||||
const TEMPLATE_UUID = this.configService.get<string>(
|
const TEMPLATE_UUID = this.configService.get<string>(
|
||||||
'email-config.MAILTRAP_SEND_OTP_TEMPLATE_UUID',
|
'email-config.MAILTRAP_SEND_OTP_TEMPLATE_UUID',
|
||||||
);
|
);
|
||||||
@ -214,20 +168,84 @@ export class EmailService {
|
|||||||
template_variables: emailEditData,
|
template_variables: emailEditData,
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
return this.sendEmailWithTemplateV2({
|
||||||
await axios.post(API_URL, emailData, {
|
...emailData,
|
||||||
headers: {
|
isBatch: false,
|
||||||
Authorization: `Bearer ${API_TOKEN}`,
|
});
|
||||||
'Content-Type': 'application/json',
|
}
|
||||||
|
|
||||||
|
async sendUpdateBookingTimingEmailWithTemplate(
|
||||||
|
emails: {
|
||||||
|
email: string;
|
||||||
|
name: string;
|
||||||
|
bookings: {
|
||||||
|
date: string;
|
||||||
|
start_time: string;
|
||||||
|
end_time: string;
|
||||||
|
}[];
|
||||||
|
}[],
|
||||||
|
emailVariables: {
|
||||||
|
space_name: string;
|
||||||
|
days: string;
|
||||||
|
start_time: string;
|
||||||
|
end_time: string;
|
||||||
|
},
|
||||||
|
): Promise<void> {
|
||||||
|
const TEMPLATE_UUID = this.configService.get<string>(
|
||||||
|
'email-config.MAILTRAP_SEND_BOOKING_TIMING_UPDATE_TEMPLATE_UUID',
|
||||||
|
);
|
||||||
|
|
||||||
|
const emailData = {
|
||||||
|
base: {
|
||||||
|
from: {
|
||||||
|
email: this.smtpConfig.sender,
|
||||||
},
|
},
|
||||||
});
|
template_uuid: TEMPLATE_UUID,
|
||||||
} catch (error) {
|
},
|
||||||
throw new HttpException(
|
requests: emails.map(({ email, name, bookings }) => ({
|
||||||
error.response?.data?.message ||
|
to: [{ email }],
|
||||||
'Error sending email using Mailtrap template',
|
template_variables: { ...emailVariables, name, bookings },
|
||||||
error.response?.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
})),
|
||||||
);
|
};
|
||||||
}
|
|
||||||
|
return this.sendEmailWithTemplateV2({
|
||||||
|
...emailData,
|
||||||
|
isBatch: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async sendUpdateBookingAvailabilityEmailWithTemplate(
|
||||||
|
emails: { email: string; name: string }[],
|
||||||
|
emailVariables: {
|
||||||
|
space_name: string;
|
||||||
|
availability: string;
|
||||||
|
isAvailable: boolean;
|
||||||
|
},
|
||||||
|
): Promise<void> {
|
||||||
|
const TEMPLATE_UUID = this.configService.get<string>(
|
||||||
|
'email-config.MAILTRAP_SEND_BOOKING_AVAILABILITY_UPDATE_TEMPLATE_UUID',
|
||||||
|
);
|
||||||
|
|
||||||
|
const emailData = {
|
||||||
|
base: {
|
||||||
|
from: {
|
||||||
|
email: this.smtpConfig.sender,
|
||||||
|
},
|
||||||
|
template_uuid: TEMPLATE_UUID,
|
||||||
|
},
|
||||||
|
requests: emails.map(({ email, name }) => ({
|
||||||
|
to: [{ email }],
|
||||||
|
template_variables: {
|
||||||
|
...emailVariables,
|
||||||
|
name,
|
||||||
|
},
|
||||||
|
})),
|
||||||
|
};
|
||||||
|
|
||||||
|
return this.sendEmailWithTemplateV2({
|
||||||
|
...emailData,
|
||||||
|
isBatch: true,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
generateUserChangesEmailBody(
|
generateUserChangesEmailBody(
|
||||||
addedSpaceNames: string[],
|
addedSpaceNames: string[],
|
||||||
@ -264,4 +282,30 @@ export class EmailService {
|
|||||||
nameChanged,
|
nameChanged,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async sendEmailWithTemplateV2({
|
||||||
|
isBatch,
|
||||||
|
...emailData
|
||||||
|
}: BatchEmailData | SingleEmailData): Promise<void> {
|
||||||
|
try {
|
||||||
|
await axios.post(
|
||||||
|
isBatch ? this.BATCH_EMAIL_API_URL : this.SEND_EMAIL_API_URL,
|
||||||
|
{
|
||||||
|
...emailData,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${this.API_TOKEN}`,
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
error.response?.data?.message ||
|
||||||
|
'Error sending email using Mailtrap template',
|
||||||
|
error.response?.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
7
libs/common/src/util/email/single-email.interface.ts
Normal file
7
libs/common/src/util/email/single-email.interface.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
export interface SingleEmailData {
|
||||||
|
from: { email: string };
|
||||||
|
to: { email: string }[];
|
||||||
|
template_uuid: string;
|
||||||
|
template_variables?: Record<string, any>;
|
||||||
|
isBatch: false;
|
||||||
|
}
|
7
libs/common/src/util/time-to-12-hours-convetion.ts
Normal file
7
libs/common/src/util/time-to-12-hours-convetion.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import { format, parse } from 'date-fns';
|
||||||
|
|
||||||
|
export function to12HourFormat(timeString: string): string {
|
||||||
|
timeString = timeString.padEnd(8, ':00');
|
||||||
|
const parsedTime = parse(timeString, 'HH:mm:ss', new Date());
|
||||||
|
return format(parsedTime, 'hh:mm a');
|
||||||
|
}
|
4539
package-lock.json
generated
4539
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
34
package.json
34
package.json
@ -6,25 +6,19 @@
|
|||||||
"private": true,
|
"private": true,
|
||||||
"license": "UNLICENSED",
|
"license": "UNLICENSED",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "npx nest build",
|
"build": "npm run test && npx nest build",
|
||||||
"build:lambda": "npx nest build && cp package*.json dist/",
|
|
||||||
"format": "prettier --write \"apps/**/*.ts\" \"libs/**/*.ts\"",
|
"format": "prettier --write \"apps/**/*.ts\" \"libs/**/*.ts\"",
|
||||||
"start": "node dist/main",
|
"start": "npm run test && node dist/main",
|
||||||
"start:dev": "npx nest start --watch",
|
"start:dev": "npm run test && npx nest start --watch",
|
||||||
"dev": "npx nest start --watch",
|
"dev": "npx nest start --watch",
|
||||||
"start:debug": "npx nest start --debug --watch",
|
"start:debug": "npm run test && npx nest start --debug --watch",
|
||||||
"start:prod": "node dist/main",
|
"start:prod": "npm run test && node dist/main",
|
||||||
"start:lambda": "node dist/lambda",
|
|
||||||
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
|
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
|
||||||
"test": "jest --config jest.config.js",
|
"test": "jest --config jest.config.js",
|
||||||
"test:watch": "jest --watch --config jest.config.js",
|
"test:watch": "jest --watch --config jest.config.js",
|
||||||
"test:cov": "jest --coverage --config jest.config.js",
|
"test:cov": "jest --coverage --config jest.config.js",
|
||||||
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
|
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
|
||||||
"test:e2e": "jest --config ./apps/backend/test/jest-e2e.json",
|
"test:e2e": "jest --config ./apps/backend/test/jest-e2e.json"
|
||||||
"deploy": "./deploy.sh",
|
|
||||||
"infra:build": "bash build.sh",
|
|
||||||
"infra:deploy": "cdk deploy SyncrowBackendStack",
|
|
||||||
"infra:destroy": "cdk destroy SyncrowBackendStack"
|
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@fast-csv/format": "^5.0.2",
|
"@fast-csv/format": "^5.0.2",
|
||||||
@ -36,22 +30,21 @@
|
|||||||
"@nestjs/jwt": "^10.2.0",
|
"@nestjs/jwt": "^10.2.0",
|
||||||
"@nestjs/passport": "^10.0.3",
|
"@nestjs/passport": "^10.0.3",
|
||||||
"@nestjs/platform-express": "^10.0.0",
|
"@nestjs/platform-express": "^10.0.0",
|
||||||
|
"@nestjs/schedule": "^6.0.0",
|
||||||
"@nestjs/swagger": "^7.3.0",
|
"@nestjs/swagger": "^7.3.0",
|
||||||
"@nestjs/terminus": "^11.0.0",
|
"@nestjs/terminus": "^11.0.0",
|
||||||
"@nestjs/throttler": "^6.4.0",
|
"@nestjs/throttler": "^6.4.0",
|
||||||
"@nestjs/typeorm": "^10.0.2",
|
"@nestjs/typeorm": "^10.0.2",
|
||||||
"@nestjs/websockets": "^10.3.8",
|
"@nestjs/websockets": "^10.3.8",
|
||||||
"@tuya/tuya-connector-nodejs": "^2.1.2",
|
"@tuya/tuya-connector-nodejs": "^2.1.2",
|
||||||
"@types/aws-lambda": "^8.10.150",
|
|
||||||
"argon2": "^0.40.1",
|
"argon2": "^0.40.1",
|
||||||
"aws-serverless-express": "^3.4.0",
|
|
||||||
"axios": "^1.7.7",
|
"axios": "^1.7.7",
|
||||||
"bcryptjs": "^2.4.3",
|
"bcryptjs": "^2.4.3",
|
||||||
"class-transformer": "^0.5.1",
|
"class-transformer": "^0.5.1",
|
||||||
"class-validator": "^0.14.1",
|
"class-validator": "^0.14.1",
|
||||||
"crypto-js": "^4.2.0",
|
"crypto-js": "^4.2.0",
|
||||||
"csv-parser": "^3.2.0",
|
"csv-parser": "^3.2.0",
|
||||||
"dotenv": "^17.0.0",
|
"date-fns": "^4.1.0",
|
||||||
"express-rate-limit": "^7.1.5",
|
"express-rate-limit": "^7.1.5",
|
||||||
"firebase": "^10.12.5",
|
"firebase": "^10.12.5",
|
||||||
"google-auth-library": "^9.14.1",
|
"google-auth-library": "^9.14.1",
|
||||||
@ -59,15 +52,15 @@
|
|||||||
"ioredis": "^5.3.2",
|
"ioredis": "^5.3.2",
|
||||||
"morgan": "^1.10.0",
|
"morgan": "^1.10.0",
|
||||||
"nest-winston": "^1.10.2",
|
"nest-winston": "^1.10.2",
|
||||||
"nodemailer": "^6.9.10",
|
"node-cache": "^5.1.2",
|
||||||
|
"nodemailer": "^7.0.5",
|
||||||
"onesignal-node": "^3.4.0",
|
"onesignal-node": "^3.4.0",
|
||||||
"passport": "^0.7.0",
|
|
||||||
"passport-jwt": "^4.0.1",
|
"passport-jwt": "^4.0.1",
|
||||||
"pg": "^8.11.3",
|
"pg": "^8.11.3",
|
||||||
"reflect-metadata": "^0.2.0",
|
"reflect-metadata": "^0.2.2",
|
||||||
"rxjs": "^7.8.1",
|
"rxjs": "^7.8.1",
|
||||||
"typeorm": "^0.3.20",
|
"typeorm": "^0.3.20",
|
||||||
"webpack": "^5.99.9",
|
"uuid": "^11.1.0",
|
||||||
"winston": "^3.17.0",
|
"winston": "^3.17.0",
|
||||||
"ws": "^8.17.0"
|
"ws": "^8.17.0"
|
||||||
},
|
},
|
||||||
@ -81,12 +74,11 @@
|
|||||||
"@types/jest": "^29.5.2",
|
"@types/jest": "^29.5.2",
|
||||||
"@types/multer": "^1.4.12",
|
"@types/multer": "^1.4.12",
|
||||||
"@types/node": "^20.3.1",
|
"@types/node": "^20.3.1",
|
||||||
|
"@types/nodemailer": "^6.4.17",
|
||||||
"@types/supertest": "^6.0.0",
|
"@types/supertest": "^6.0.0",
|
||||||
"@typescript-eslint/eslint-plugin": "^6.0.0",
|
"@typescript-eslint/eslint-plugin": "^6.0.0",
|
||||||
"@typescript-eslint/parser": "^6.0.0",
|
"@typescript-eslint/parser": "^6.0.0",
|
||||||
"aws-cdk-lib": "^2.202.0",
|
|
||||||
"concurrently": "^8.2.2",
|
"concurrently": "^8.2.2",
|
||||||
"constructs": "^10.4.2",
|
|
||||||
"eslint": "^8.42.0",
|
"eslint": "^8.42.0",
|
||||||
"eslint-config-prettier": "^9.0.0",
|
"eslint-config-prettier": "^9.0.0",
|
||||||
"eslint-plugin-import": "^2.31.0",
|
"eslint-plugin-import": "^2.31.0",
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { SeederModule } from '@app/common/seed/seeder.module';
|
import { SeederModule } from '@app/common/seed/seeder.module';
|
||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { ConfigModule } from '@nestjs/config';
|
import { ConfigModule } from '@nestjs/config';
|
||||||
import { APP_INTERCEPTOR } from '@nestjs/core';
|
import { APP_GUARD, APP_INTERCEPTOR } from '@nestjs/core';
|
||||||
import { WinstonModule } from 'nest-winston';
|
import { WinstonModule } from 'nest-winston';
|
||||||
import { AuthenticationModule } from './auth/auth.module';
|
import { AuthenticationModule } from './auth/auth.module';
|
||||||
import { AutomationModule } from './automation/automation.module';
|
import { AutomationModule } from './automation/automation.module';
|
||||||
@ -35,19 +35,38 @@ import { UserNotificationModule } from './user-notification/user-notification.mo
|
|||||||
import { UserModule } from './users/user.module';
|
import { UserModule } from './users/user.module';
|
||||||
import { VisitorPasswordModule } from './vistor-password/visitor-password.module';
|
import { VisitorPasswordModule } from './vistor-password/visitor-password.module';
|
||||||
|
|
||||||
|
import { TimerRepositoryModule } from '@app/common/modules/timer/timer.repository.module';
|
||||||
|
import { ScheduleModule as NestScheduleModule } from '@nestjs/schedule';
|
||||||
|
import { ThrottlerGuard } from '@nestjs/throttler';
|
||||||
|
import { ThrottlerModule } from '@nestjs/throttler/dist/throttler.module';
|
||||||
|
import { isArray } from 'class-validator';
|
||||||
import { winstonLoggerOptions } from '../libs/common/src/logger/services/winston.logger';
|
import { winstonLoggerOptions } from '../libs/common/src/logger/services/winston.logger';
|
||||||
import { AqiModule } from './aqi/aqi.module';
|
import { AqiModule } from './aqi/aqi.module';
|
||||||
|
import { BookingModule } from './booking';
|
||||||
import { OccupancyModule } from './occupancy/occupancy.module';
|
import { OccupancyModule } from './occupancy/occupancy.module';
|
||||||
|
import { SchedulerModule } from './scheduler/scheduler.module';
|
||||||
|
import { TimerModule } from './timer/timer.module';
|
||||||
import { WeatherModule } from './weather/weather.module';
|
import { WeatherModule } from './weather/weather.module';
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
ConfigModule.forRoot({
|
ConfigModule.forRoot({
|
||||||
load: config,
|
load: config,
|
||||||
}),
|
}),
|
||||||
/* ThrottlerModule.forRoot({
|
ThrottlerModule.forRoot({
|
||||||
throttlers: [{ ttl: 100000, limit: 30 }],
|
throttlers: [{ ttl: 60000, limit: 100 }],
|
||||||
}), */
|
generateKey: (context) => {
|
||||||
|
const req = context.switchToHttp().getRequest();
|
||||||
|
console.log('Real IP:', req.headers['x-forwarded-for']);
|
||||||
|
return req.headers['x-forwarded-for']
|
||||||
|
? isArray(req.headers['x-forwarded-for'])
|
||||||
|
? req.headers['x-forwarded-for'][0].split(':')[0]
|
||||||
|
: req.headers['x-forwarded-for'].split(':')[0]
|
||||||
|
: req.ip;
|
||||||
|
},
|
||||||
|
}),
|
||||||
WinstonModule.forRoot(winstonLoggerOptions),
|
WinstonModule.forRoot(winstonLoggerOptions),
|
||||||
|
TimerModule,
|
||||||
|
TimerRepositoryModule,
|
||||||
ClientModule,
|
ClientModule,
|
||||||
AuthenticationModule,
|
AuthenticationModule,
|
||||||
UserModule,
|
UserModule,
|
||||||
@ -82,16 +101,19 @@ import { WeatherModule } from './weather/weather.module';
|
|||||||
OccupancyModule,
|
OccupancyModule,
|
||||||
WeatherModule,
|
WeatherModule,
|
||||||
AqiModule,
|
AqiModule,
|
||||||
|
SchedulerModule,
|
||||||
|
NestScheduleModule.forRoot(),
|
||||||
|
BookingModule,
|
||||||
],
|
],
|
||||||
providers: [
|
providers: [
|
||||||
{
|
{
|
||||||
provide: APP_INTERCEPTOR,
|
provide: APP_INTERCEPTOR,
|
||||||
useClass: LoggingInterceptor,
|
useClass: LoggingInterceptor,
|
||||||
},
|
},
|
||||||
/* {
|
{
|
||||||
provide: APP_GUARD,
|
provide: APP_GUARD,
|
||||||
useClass: ThrottlerGuard,
|
useClass: ThrottlerGuard,
|
||||||
}, */
|
},
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
export class AppModule {}
|
export class AppModule {}
|
||||||
|
@ -1,15 +1,17 @@
|
|||||||
|
import { AuthService } from '@app/common/auth/services/auth.service';
|
||||||
|
import { RoleTypeRepository } from '@app/common/modules/role-type/repositories';
|
||||||
|
import { UserSessionRepository } from '@app/common/modules/session/repositories/session.repository';
|
||||||
|
import {
|
||||||
|
UserOtpRepository,
|
||||||
|
UserRepository,
|
||||||
|
} from '@app/common/modules/user/repositories';
|
||||||
|
import { EmailService } from '@app/common/util/email/email.service';
|
||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { ConfigModule } from '@nestjs/config';
|
import { ConfigModule } from '@nestjs/config';
|
||||||
import { UserRepository } from '@app/common/modules/user/repositories';
|
import { JwtService } from '@nestjs/jwt';
|
||||||
import { UserSessionRepository } from '@app/common/modules/session/repositories/session.repository';
|
|
||||||
import { UserOtpRepository } from '@app/common/modules/user/repositories';
|
|
||||||
import { RoleTypeRepository } from '@app/common/modules/role-type/repositories';
|
|
||||||
import { RoleService } from 'src/role/services';
|
import { RoleService } from 'src/role/services';
|
||||||
import { UserAuthController } from './controllers';
|
import { UserAuthController } from './controllers';
|
||||||
import { UserAuthService } from './services';
|
import { UserAuthService } from './services';
|
||||||
import { AuthService } from '@app/common/auth/services/auth.service';
|
|
||||||
import { EmailService } from '@app/common/util/email.service';
|
|
||||||
import { JwtService } from '@nestjs/jwt';
|
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule],
|
imports: [ConfigModule],
|
||||||
|
@ -1,25 +1,25 @@
|
|||||||
import { UserRepository } from '../../../libs/common/src/modules/user/repositories';
|
import { RoleType } from '@app/common/constants/role.type.enum';
|
||||||
|
import { differenceInSeconds } from '@app/common/helper/differenceInSeconds';
|
||||||
import {
|
import {
|
||||||
BadRequestException,
|
BadRequestException,
|
||||||
ForbiddenException,
|
ForbiddenException,
|
||||||
Injectable,
|
Injectable,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { UserSignUpDto } from '../dtos/user-auth.dto';
|
|
||||||
import { HelperHashService } from '../../../libs/common/src/helper/services';
|
|
||||||
import { UserLoginDto } from '../dtos/user-login.dto';
|
|
||||||
import { AuthService } from '../../../libs/common/src/auth/services/auth.service';
|
|
||||||
import { UserSessionRepository } from '../../../libs/common/src/modules/session/repositories/session.repository';
|
|
||||||
import { UserOtpRepository } from '../../../libs/common/src/modules/user/repositories/user.repository';
|
|
||||||
import { ForgetPasswordDto, UserOtpDto, VerifyOtpDto } from '../dtos';
|
|
||||||
import { EmailService } from '../../../libs/common/src/util/email.service';
|
|
||||||
import { OtpType } from '../../../libs/common/src/constants/otp-type.enum';
|
|
||||||
import { UserEntity } from '../../../libs/common/src/modules/user/entities/user.entity';
|
|
||||||
import * as argon2 from 'argon2';
|
|
||||||
import { differenceInSeconds } from '@app/common/helper/differenceInSeconds';
|
|
||||||
import { LessThan, MoreThan } from 'typeorm';
|
|
||||||
import { ConfigService } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
|
import * as argon2 from 'argon2';
|
||||||
import { RoleService } from 'src/role/services';
|
import { RoleService } from 'src/role/services';
|
||||||
import { RoleType } from '@app/common/constants/role.type.enum';
|
import { LessThan, MoreThan } from 'typeorm';
|
||||||
|
import { AuthService } from '../../../libs/common/src/auth/services/auth.service';
|
||||||
|
import { OtpType } from '../../../libs/common/src/constants/otp-type.enum';
|
||||||
|
import { HelperHashService } from '../../../libs/common/src/helper/services';
|
||||||
|
import { UserSessionRepository } from '../../../libs/common/src/modules/session/repositories/session.repository';
|
||||||
|
import { UserEntity } from '../../../libs/common/src/modules/user/entities/user.entity';
|
||||||
|
import { UserRepository } from '../../../libs/common/src/modules/user/repositories';
|
||||||
|
import { UserOtpRepository } from '../../../libs/common/src/modules/user/repositories/user.repository';
|
||||||
|
import { EmailService } from '../../../libs/common/src/util/email/email.service';
|
||||||
|
import { ForgetPasswordDto, UserOtpDto, VerifyOtpDto } from '../dtos';
|
||||||
|
import { UserSignUpDto } from '../dtos/user-auth.dto';
|
||||||
|
import { UserLoginDto } from '../dtos/user-login.dto';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class UserAuthService {
|
export class UserAuthService {
|
||||||
@ -108,7 +108,7 @@ export class UserAuthService {
|
|||||||
|
|
||||||
async userLogin(data: UserLoginDto) {
|
async userLogin(data: UserLoginDto) {
|
||||||
try {
|
try {
|
||||||
let user;
|
let user: Omit<UserEntity, 'password'>;
|
||||||
if (data.googleCode) {
|
if (data.googleCode) {
|
||||||
const googleUserData = await this.authService.login({
|
const googleUserData = await this.authService.login({
|
||||||
googleCode: data.googleCode,
|
googleCode: data.googleCode,
|
||||||
@ -145,7 +145,7 @@ export class UserAuthService {
|
|||||||
}
|
}
|
||||||
const session = await Promise.all([
|
const session = await Promise.all([
|
||||||
await this.sessionRepository.update(
|
await this.sessionRepository.update(
|
||||||
{ userId: user.id },
|
{ userId: user?.['id'] },
|
||||||
{
|
{
|
||||||
isLoggedOut: true,
|
isLoggedOut: true,
|
||||||
},
|
},
|
||||||
@ -166,6 +166,7 @@ export class UserAuthService {
|
|||||||
hasAcceptedAppAgreement: user.hasAcceptedAppAgreement,
|
hasAcceptedAppAgreement: user.hasAcceptedAppAgreement,
|
||||||
project: user.project,
|
project: user.project,
|
||||||
sessionId: session[1].uuid,
|
sessionId: session[1].uuid,
|
||||||
|
bookingPoints: user.bookingPoints,
|
||||||
});
|
});
|
||||||
return res;
|
return res;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -347,6 +348,7 @@ export class UserAuthService {
|
|||||||
userId: user.uuid,
|
userId: user.uuid,
|
||||||
uuid: user.uuid,
|
uuid: user.uuid,
|
||||||
type,
|
type,
|
||||||
|
bookingPoints: user.bookingPoints,
|
||||||
sessionId,
|
sessionId,
|
||||||
});
|
});
|
||||||
await this.authService.updateRefreshToken(user.uuid, tokens.refreshToken);
|
await this.authService.updateRefreshToken(user.uuid, tokens.refreshToken);
|
||||||
|
29
src/booking/booking.module.ts
Normal file
29
src/booking/booking.module.ts
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
import { BookingRepositoryModule } from '@app/common/modules/booking/booking.repository.module';
|
||||||
|
import { BookableSpaceEntityRepository } from '@app/common/modules/booking/repositories/bookable-space.repository';
|
||||||
|
import { BookingEntityRepository } from '@app/common/modules/booking/repositories/booking.repository';
|
||||||
|
import { SpaceRepository } from '@app/common/modules/space';
|
||||||
|
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||||
|
import { EmailService } from '@app/common/util/email/email.service';
|
||||||
|
import { Global, Module } from '@nestjs/common';
|
||||||
|
import { BookableSpaceController } from './controllers/bookable-space.controller';
|
||||||
|
import { BookingController } from './controllers/booking.controller';
|
||||||
|
import { BookableSpaceService } from './services/bookable-space.service';
|
||||||
|
import { BookingService } from './services/booking.service';
|
||||||
|
|
||||||
|
@Global()
|
||||||
|
@Module({
|
||||||
|
imports: [BookingRepositoryModule],
|
||||||
|
controllers: [BookableSpaceController, BookingController],
|
||||||
|
providers: [
|
||||||
|
BookableSpaceService,
|
||||||
|
BookingService,
|
||||||
|
EmailService,
|
||||||
|
BookableSpaceEntityRepository,
|
||||||
|
BookingEntityRepository,
|
||||||
|
|
||||||
|
SpaceRepository,
|
||||||
|
UserRepository,
|
||||||
|
],
|
||||||
|
exports: [BookableSpaceService, BookingService],
|
||||||
|
})
|
||||||
|
export class BookingModule {}
|
107
src/booking/controllers/bookable-space.controller.ts
Normal file
107
src/booking/controllers/bookable-space.controller.ts
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
import { ControllerRoute } from '@app/common/constants/controller-route';
|
||||||
|
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
|
||||||
|
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||||
|
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||||
|
import {
|
||||||
|
Body,
|
||||||
|
Controller,
|
||||||
|
Get,
|
||||||
|
Param,
|
||||||
|
ParseUUIDPipe,
|
||||||
|
Post,
|
||||||
|
Put,
|
||||||
|
Query,
|
||||||
|
Req,
|
||||||
|
UseGuards,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||||
|
|
||||||
|
import { PageResponse } from '@app/common/dto/pagination.response.dto';
|
||||||
|
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||||
|
import { plainToInstance } from 'class-transformer';
|
||||||
|
import { BookableSpaceRequestDto } from '../dtos/bookable-space-request.dto';
|
||||||
|
import { BookableSpaceResponseDto } from '../dtos/bookable-space-response.dto';
|
||||||
|
import { CreateBookableSpaceDto } from '../dtos/create-bookable-space.dto';
|
||||||
|
import { UpdateBookableSpaceDto } from '../dtos/update-bookable-space.dto';
|
||||||
|
import { BookableSpaceService } from '../services/bookable-space.service';
|
||||||
|
|
||||||
|
@ApiTags('Booking Module')
|
||||||
|
@Controller({
|
||||||
|
version: EnableDisableStatusEnum.ENABLED,
|
||||||
|
path: ControllerRoute.BOOKABLE_SPACES.ROUTE,
|
||||||
|
})
|
||||||
|
export class BookableSpaceController {
|
||||||
|
constructor(private readonly bookableSpaceService: BookableSpaceService) {}
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Post()
|
||||||
|
@ApiOperation({
|
||||||
|
summary:
|
||||||
|
ControllerRoute.BOOKABLE_SPACES.ACTIONS.ADD_BOOKABLE_SPACES_SUMMARY,
|
||||||
|
description:
|
||||||
|
ControllerRoute.BOOKABLE_SPACES.ACTIONS.ADD_BOOKABLE_SPACES_DESCRIPTION,
|
||||||
|
})
|
||||||
|
async create(@Body() dto: CreateBookableSpaceDto): Promise<BaseResponseDto> {
|
||||||
|
const result = await this.bookableSpaceService.create(dto);
|
||||||
|
return new SuccessResponseDto({
|
||||||
|
data: result,
|
||||||
|
message: 'Successfully created bookable spaces',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Get()
|
||||||
|
@ApiOperation({
|
||||||
|
summary:
|
||||||
|
ControllerRoute.BOOKABLE_SPACES.ACTIONS.GET_ALL_BOOKABLE_SPACES_SUMMARY,
|
||||||
|
description:
|
||||||
|
ControllerRoute.BOOKABLE_SPACES.ACTIONS
|
||||||
|
.GET_ALL_BOOKABLE_SPACES_DESCRIPTION,
|
||||||
|
})
|
||||||
|
async findAll(
|
||||||
|
@Query() query: BookableSpaceRequestDto,
|
||||||
|
@Req() req: Request,
|
||||||
|
): Promise<PageResponse<BookableSpaceResponseDto>> {
|
||||||
|
const project = req['user']?.project?.uuid;
|
||||||
|
if (!project) {
|
||||||
|
throw new Error('Project UUID is required in the request');
|
||||||
|
}
|
||||||
|
const { data, pagination } = await this.bookableSpaceService.findAll(
|
||||||
|
query,
|
||||||
|
project,
|
||||||
|
);
|
||||||
|
return new PageResponse<BookableSpaceResponseDto>(
|
||||||
|
{
|
||||||
|
data: data.map((space) =>
|
||||||
|
plainToInstance(BookableSpaceResponseDto, space, {
|
||||||
|
excludeExtraneousValues: true,
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
message: 'Successfully fetched all bookable spaces',
|
||||||
|
},
|
||||||
|
pagination,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Put(':spaceUuid')
|
||||||
|
@ApiOperation({
|
||||||
|
summary:
|
||||||
|
ControllerRoute.BOOKABLE_SPACES.ACTIONS.UPDATE_BOOKABLE_SPACES_SUMMARY,
|
||||||
|
description:
|
||||||
|
ControllerRoute.BOOKABLE_SPACES.ACTIONS
|
||||||
|
.UPDATE_BOOKABLE_SPACES_DESCRIPTION,
|
||||||
|
})
|
||||||
|
async update(
|
||||||
|
@Param('spaceUuid', ParseUUIDPipe) spaceUuid: string,
|
||||||
|
@Body() dto: UpdateBookableSpaceDto,
|
||||||
|
): Promise<BaseResponseDto> {
|
||||||
|
const result = await this.bookableSpaceService.update(spaceUuid, dto);
|
||||||
|
return new SuccessResponseDto({
|
||||||
|
data: result,
|
||||||
|
message: 'Successfully updated bookable spaces',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
107
src/booking/controllers/booking.controller.ts
Normal file
107
src/booking/controllers/booking.controller.ts
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
import { ControllerRoute } from '@app/common/constants/controller-route';
|
||||||
|
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
|
||||||
|
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||||
|
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||||
|
import {
|
||||||
|
Body,
|
||||||
|
Controller,
|
||||||
|
Get,
|
||||||
|
Post,
|
||||||
|
Query,
|
||||||
|
Req,
|
||||||
|
UseGuards,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||||
|
|
||||||
|
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||||
|
import { plainToInstance } from 'class-transformer';
|
||||||
|
import { AdminRoleGuard } from 'src/guards/admin.role.guard';
|
||||||
|
import { BookingRequestDto } from '../dtos/booking-request.dto';
|
||||||
|
import { BookingResponseDto } from '../dtos/booking-response.dto';
|
||||||
|
import { CreateBookingDto } from '../dtos/create-booking.dto';
|
||||||
|
import { MyBookingRequestDto } from '../dtos/my-booking-request.dto';
|
||||||
|
import { BookingService } from '../services/booking.service';
|
||||||
|
|
||||||
|
@ApiTags('Booking Module')
|
||||||
|
@Controller({
|
||||||
|
version: EnableDisableStatusEnum.ENABLED,
|
||||||
|
path: ControllerRoute.BOOKING.ROUTE,
|
||||||
|
})
|
||||||
|
export class BookingController {
|
||||||
|
constructor(private readonly bookingService: BookingService) {}
|
||||||
|
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Post()
|
||||||
|
@ApiOperation({
|
||||||
|
summary: ControllerRoute.BOOKING.ACTIONS.ADD_BOOKING_SUMMARY,
|
||||||
|
description: ControllerRoute.BOOKING.ACTIONS.ADD_BOOKING_DESCRIPTION,
|
||||||
|
})
|
||||||
|
async create(
|
||||||
|
@Body() dto: CreateBookingDto,
|
||||||
|
@Req() req: Request,
|
||||||
|
): Promise<BaseResponseDto> {
|
||||||
|
const userUuid = req['user']?.uuid;
|
||||||
|
if (!userUuid) {
|
||||||
|
throw new Error('User UUID is required in the request');
|
||||||
|
}
|
||||||
|
const result = await this.bookingService.create(userUuid, dto);
|
||||||
|
return new SuccessResponseDto({
|
||||||
|
data: result,
|
||||||
|
message: 'Successfully created booking',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(AdminRoleGuard)
|
||||||
|
@Get()
|
||||||
|
@ApiOperation({
|
||||||
|
summary: ControllerRoute.BOOKING.ACTIONS.GET_ALL_BOOKINGS_SUMMARY,
|
||||||
|
description: ControllerRoute.BOOKING.ACTIONS.GET_ALL_BOOKINGS_DESCRIPTION,
|
||||||
|
})
|
||||||
|
async findAll(
|
||||||
|
@Query() query: BookingRequestDto,
|
||||||
|
@Req() req: Request,
|
||||||
|
): Promise<BaseResponseDto> {
|
||||||
|
const project = req['user']?.project?.uuid;
|
||||||
|
if (!project) {
|
||||||
|
throw new Error('Project UUID is required in the request');
|
||||||
|
}
|
||||||
|
const result = await this.bookingService.findAll(query, project);
|
||||||
|
return new SuccessResponseDto({
|
||||||
|
data: plainToInstance(BookingResponseDto, result, {
|
||||||
|
excludeExtraneousValues: true,
|
||||||
|
}),
|
||||||
|
message: 'Successfully fetched all bookings',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Get('my-bookings')
|
||||||
|
@ApiOperation({
|
||||||
|
summary: ControllerRoute.BOOKING.ACTIONS.GET_MY_BOOKINGS_SUMMARY,
|
||||||
|
description: ControllerRoute.BOOKING.ACTIONS.GET_MY_BOOKINGS_DESCRIPTION,
|
||||||
|
})
|
||||||
|
async findMyBookings(
|
||||||
|
@Query() query: MyBookingRequestDto,
|
||||||
|
@Req() req: Request,
|
||||||
|
): Promise<BaseResponseDto> {
|
||||||
|
const userUuid = req['user']?.uuid;
|
||||||
|
const project = req['user']?.project?.uuid;
|
||||||
|
if (!project) {
|
||||||
|
throw new Error('Project UUID is required in the request');
|
||||||
|
}
|
||||||
|
const result = await this.bookingService.findMyBookings(
|
||||||
|
query,
|
||||||
|
userUuid,
|
||||||
|
project,
|
||||||
|
);
|
||||||
|
return new SuccessResponseDto({
|
||||||
|
data: plainToInstance(BookingResponseDto, result, {
|
||||||
|
excludeExtraneousValues: true,
|
||||||
|
}),
|
||||||
|
message: 'Successfully fetched all bookings',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
31
src/booking/dtos/bookable-space-request.dto.ts
Normal file
31
src/booking/dtos/bookable-space-request.dto.ts
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
import { BooleanValues } from '@app/common/constants/boolean-values.enum';
|
||||||
|
import { PaginationRequestWithSearchGetListDto } from '@app/common/dto/pagination-with-search.request.dto';
|
||||||
|
import { ApiProperty, OmitType } from '@nestjs/swagger';
|
||||||
|
import { Transform } from 'class-transformer';
|
||||||
|
import { IsBoolean, IsNotEmpty, IsOptional } from 'class-validator';
|
||||||
|
|
||||||
|
export class BookableSpaceRequestDto extends OmitType(
|
||||||
|
PaginationRequestWithSearchGetListDto,
|
||||||
|
['includeSpaces'],
|
||||||
|
) {
|
||||||
|
@ApiProperty({
|
||||||
|
type: Boolean,
|
||||||
|
required: false,
|
||||||
|
})
|
||||||
|
@IsBoolean()
|
||||||
|
@IsOptional()
|
||||||
|
@Transform(({ obj }) => {
|
||||||
|
return obj.active === BooleanValues.TRUE;
|
||||||
|
})
|
||||||
|
active?: boolean;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
type: Boolean,
|
||||||
|
})
|
||||||
|
@IsBoolean()
|
||||||
|
@IsNotEmpty()
|
||||||
|
@Transform(({ obj }) => {
|
||||||
|
return obj.configured === BooleanValues.TRUE;
|
||||||
|
})
|
||||||
|
configured: boolean;
|
||||||
|
}
|
59
src/booking/dtos/bookable-space-response.dto.ts
Normal file
59
src/booking/dtos/bookable-space-response.dto.ts
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
|
import { Expose, Type } from 'class-transformer';
|
||||||
|
export class BookableSpaceConfigResponseDto {
|
||||||
|
@ApiProperty()
|
||||||
|
@Expose()
|
||||||
|
uuid: string;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
type: [String],
|
||||||
|
})
|
||||||
|
@Expose()
|
||||||
|
daysAvailable: string[];
|
||||||
|
|
||||||
|
@ApiProperty()
|
||||||
|
@Expose()
|
||||||
|
startTime: string;
|
||||||
|
|
||||||
|
@ApiProperty()
|
||||||
|
@Expose()
|
||||||
|
endTime: string;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
type: Boolean,
|
||||||
|
})
|
||||||
|
@Expose()
|
||||||
|
active: boolean;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
type: Number,
|
||||||
|
nullable: true,
|
||||||
|
})
|
||||||
|
@Expose()
|
||||||
|
points?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class BookableSpaceResponseDto {
|
||||||
|
@ApiProperty()
|
||||||
|
@Expose()
|
||||||
|
uuid: string;
|
||||||
|
|
||||||
|
@ApiProperty()
|
||||||
|
@Expose()
|
||||||
|
spaceUuid: string;
|
||||||
|
|
||||||
|
@ApiProperty()
|
||||||
|
@Expose()
|
||||||
|
spaceName: string;
|
||||||
|
|
||||||
|
@ApiProperty()
|
||||||
|
@Expose()
|
||||||
|
virtualLocation: string;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
type: BookableSpaceConfigResponseDto,
|
||||||
|
})
|
||||||
|
@Expose()
|
||||||
|
@Type(() => BookableSpaceConfigResponseDto)
|
||||||
|
bookableConfig: BookableSpaceConfigResponseDto;
|
||||||
|
}
|
23
src/booking/dtos/booking-request.dto.ts
Normal file
23
src/booking/dtos/booking-request.dto.ts
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
|
import { IsNotEmpty, IsOptional, IsUUID, Matches } from 'class-validator';
|
||||||
|
|
||||||
|
export class BookingRequestDto {
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'Month in MM-YYYY format',
|
||||||
|
example: '07-2025',
|
||||||
|
})
|
||||||
|
@IsNotEmpty()
|
||||||
|
@Matches(/^(0[1-9]|1[0-2])\-\d{4}$/, {
|
||||||
|
message: 'Date must be in MM/YYYY format',
|
||||||
|
})
|
||||||
|
month: string;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'Space UUID',
|
||||||
|
example: '550e8400-e29b-41d4-a716-446655440000',
|
||||||
|
required: false,
|
||||||
|
})
|
||||||
|
@IsOptional()
|
||||||
|
@IsUUID('4')
|
||||||
|
space?: string;
|
||||||
|
}
|
88
src/booking/dtos/booking-response.dto.ts
Normal file
88
src/booking/dtos/booking-response.dto.ts
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
|
import { Expose, Transform, Type } from 'class-transformer';
|
||||||
|
|
||||||
|
export class BookingUserResponseDto {
|
||||||
|
@ApiProperty()
|
||||||
|
@Expose()
|
||||||
|
uuid: string;
|
||||||
|
|
||||||
|
@ApiProperty()
|
||||||
|
@Expose()
|
||||||
|
firstName: string;
|
||||||
|
|
||||||
|
@ApiProperty()
|
||||||
|
@Expose()
|
||||||
|
lastName: string;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
type: String,
|
||||||
|
nullable: true,
|
||||||
|
})
|
||||||
|
@Expose()
|
||||||
|
email: string;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
type: String,
|
||||||
|
nullable: true,
|
||||||
|
})
|
||||||
|
@Expose()
|
||||||
|
@Transform(({ obj }) => obj.inviteUser?.companyName || null)
|
||||||
|
companyName: string;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
type: String,
|
||||||
|
nullable: true,
|
||||||
|
})
|
||||||
|
@Expose()
|
||||||
|
phoneNumber: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class BookingSpaceResponseDto {
|
||||||
|
@ApiProperty()
|
||||||
|
@Expose()
|
||||||
|
uuid: string;
|
||||||
|
|
||||||
|
@ApiProperty()
|
||||||
|
@Expose()
|
||||||
|
spaceName: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class BookingResponseDto {
|
||||||
|
@ApiProperty()
|
||||||
|
@Expose()
|
||||||
|
uuid: string;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
type: Date,
|
||||||
|
})
|
||||||
|
@Expose()
|
||||||
|
date: Date;
|
||||||
|
|
||||||
|
@ApiProperty()
|
||||||
|
@Expose()
|
||||||
|
startTime: string;
|
||||||
|
|
||||||
|
@ApiProperty()
|
||||||
|
@Expose()
|
||||||
|
endTime: string;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
type: Number,
|
||||||
|
})
|
||||||
|
@Expose()
|
||||||
|
cost: number;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
type: BookingUserResponseDto,
|
||||||
|
})
|
||||||
|
@Type(() => BookingUserResponseDto)
|
||||||
|
@Expose()
|
||||||
|
user: BookingUserResponseDto;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
type: BookingSpaceResponseDto,
|
||||||
|
})
|
||||||
|
@Type(() => BookingSpaceResponseDto)
|
||||||
|
@Expose()
|
||||||
|
space: BookingSpaceResponseDto;
|
||||||
|
}
|
63
src/booking/dtos/create-bookable-space.dto.ts
Normal file
63
src/booking/dtos/create-bookable-space.dto.ts
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
import { DaysEnum } from '@app/common/constants/days.enum';
|
||||||
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
|
import {
|
||||||
|
ArrayMinSize,
|
||||||
|
IsArray,
|
||||||
|
IsEnum,
|
||||||
|
IsInt,
|
||||||
|
IsNotEmpty,
|
||||||
|
IsOptional,
|
||||||
|
IsString,
|
||||||
|
IsUUID,
|
||||||
|
Matches,
|
||||||
|
Max,
|
||||||
|
Min,
|
||||||
|
} from 'class-validator';
|
||||||
|
|
||||||
|
export class CreateBookableSpaceDto {
|
||||||
|
@ApiProperty({
|
||||||
|
type: 'string',
|
||||||
|
isArray: true,
|
||||||
|
example: [
|
||||||
|
'3fa85f64-5717-4562-b3fc-2c963f66afa6',
|
||||||
|
'4fa85f64-5717-4562-b3fc-2c963f66afa7',
|
||||||
|
],
|
||||||
|
})
|
||||||
|
@IsArray()
|
||||||
|
@ArrayMinSize(1, { message: 'At least one space must be selected' })
|
||||||
|
@IsUUID('all', { each: true, message: 'Invalid space UUID provided' })
|
||||||
|
spaceUuids: string[];
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
enum: DaysEnum,
|
||||||
|
isArray: true,
|
||||||
|
example: [DaysEnum.MON, DaysEnum.WED, DaysEnum.FRI],
|
||||||
|
})
|
||||||
|
@IsArray()
|
||||||
|
@ArrayMinSize(1, { message: 'At least one day must be selected' })
|
||||||
|
@IsEnum(DaysEnum, { each: true, message: 'Invalid day provided' })
|
||||||
|
daysAvailable: DaysEnum[];
|
||||||
|
|
||||||
|
@ApiProperty({ example: '09:00' })
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty({ message: 'Start time cannot be empty' })
|
||||||
|
@Matches(/^([01]?[0-9]|2[0-3]):[0-5][0-9]$/, {
|
||||||
|
message: 'Start time must be in HH:mm format (24-hour)',
|
||||||
|
})
|
||||||
|
startTime: string;
|
||||||
|
|
||||||
|
@ApiProperty({ example: '17:00' })
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty({ message: 'End time cannot be empty' })
|
||||||
|
@Matches(/^([01]?[0-9]|2[0-3]):[0-5][0-9]$/, {
|
||||||
|
message: 'End time must be in HH:mm format (24-hour)',
|
||||||
|
})
|
||||||
|
endTime: string;
|
||||||
|
|
||||||
|
@ApiProperty({ example: 10, required: false })
|
||||||
|
@IsOptional()
|
||||||
|
@IsInt()
|
||||||
|
@Min(0, { message: 'Points cannot be negative' })
|
||||||
|
@Max(1000, { message: 'Points cannot exceed 1000' })
|
||||||
|
points?: number;
|
||||||
|
}
|
43
src/booking/dtos/create-booking.dto.ts
Normal file
43
src/booking/dtos/create-booking.dto.ts
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
|
import {
|
||||||
|
IsDate,
|
||||||
|
IsNotEmpty,
|
||||||
|
IsString,
|
||||||
|
IsUUID,
|
||||||
|
Matches,
|
||||||
|
MinDate,
|
||||||
|
} from 'class-validator';
|
||||||
|
|
||||||
|
export class CreateBookingDto {
|
||||||
|
@ApiProperty({
|
||||||
|
type: 'string',
|
||||||
|
example: '4fa85f64-5717-4562-b3fc-2c963f66afa7',
|
||||||
|
})
|
||||||
|
@IsNotEmpty()
|
||||||
|
@IsUUID('4', { message: 'Invalid space UUID provided' })
|
||||||
|
spaceUuid: string;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
type: Date,
|
||||||
|
})
|
||||||
|
@IsNotEmpty()
|
||||||
|
@IsDate()
|
||||||
|
@MinDate(new Date())
|
||||||
|
date: Date;
|
||||||
|
|
||||||
|
@ApiProperty({ example: '09:00' })
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty({ message: 'Start time cannot be empty' })
|
||||||
|
@Matches(/^([01]?[0-9]|2[0-3]):[0-5][0-9]$/, {
|
||||||
|
message: 'Start time must be in HH:mm format (24-hour)',
|
||||||
|
})
|
||||||
|
startTime: string;
|
||||||
|
|
||||||
|
@ApiProperty({ example: '17:00' })
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty({ message: 'End time cannot be empty' })
|
||||||
|
@Matches(/^([01]?[0-9]|2[0-3]):[0-5][0-9]$/, {
|
||||||
|
message: 'End time must be in HH:mm format (24-hour)',
|
||||||
|
})
|
||||||
|
endTime: string;
|
||||||
|
}
|
14
src/booking/dtos/my-booking-request.dto.ts
Normal file
14
src/booking/dtos/my-booking-request.dto.ts
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
|
import { IsIn, IsOptional } from 'class-validator';
|
||||||
|
|
||||||
|
export class MyBookingRequestDto {
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'Filter bookings by time period',
|
||||||
|
example: 'past',
|
||||||
|
enum: ['past', 'future'],
|
||||||
|
required: false,
|
||||||
|
})
|
||||||
|
@IsOptional()
|
||||||
|
@IsIn(['past', 'future'])
|
||||||
|
when?: 'past' | 'future';
|
||||||
|
}
|
12
src/booking/dtos/update-bookable-space.dto.ts
Normal file
12
src/booking/dtos/update-bookable-space.dto.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { ApiProperty, OmitType, PartialType } from '@nestjs/swagger';
|
||||||
|
import { IsBoolean, IsOptional } from 'class-validator';
|
||||||
|
import { CreateBookableSpaceDto } from './create-bookable-space.dto';
|
||||||
|
|
||||||
|
export class UpdateBookableSpaceDto extends PartialType(
|
||||||
|
OmitType(CreateBookableSpaceDto, ['spaceUuids']),
|
||||||
|
) {
|
||||||
|
@ApiProperty({ type: Boolean })
|
||||||
|
@IsOptional()
|
||||||
|
@IsBoolean()
|
||||||
|
active?: boolean;
|
||||||
|
}
|
1
src/booking/index.ts
Normal file
1
src/booking/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './booking.module';
|
371
src/booking/services/bookable-space.service.ts
Normal file
371
src/booking/services/bookable-space.service.ts
Normal file
@ -0,0 +1,371 @@
|
|||||||
|
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||||
|
import { PageResponseDto } from '@app/common/dto/pagination.response.dto';
|
||||||
|
import { timeToMinutes } from '@app/common/helper/timeToMinutes';
|
||||||
|
import { TypeORMCustomModel } from '@app/common/models/typeOrmCustom.model';
|
||||||
|
import { BookableSpaceEntityRepository } from '@app/common/modules/booking/repositories/bookable-space.repository';
|
||||||
|
import { BookingEntityRepository } from '@app/common/modules/booking/repositories/booking.repository';
|
||||||
|
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
|
||||||
|
import { SpaceRepository } from '@app/common/modules/space/repositories/space.repository';
|
||||||
|
import { EmailService } from '@app/common/util/email/email.service';
|
||||||
|
import { to12HourFormat } from '@app/common/util/time-to-12-hours-convetion';
|
||||||
|
import {
|
||||||
|
BadRequestException,
|
||||||
|
ConflictException,
|
||||||
|
Injectable,
|
||||||
|
NotFoundException,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
import { format } from 'date-fns';
|
||||||
|
import { Brackets, In } from 'typeorm';
|
||||||
|
import { BookableSpaceRequestDto } from '../dtos/bookable-space-request.dto';
|
||||||
|
import { CreateBookableSpaceDto } from '../dtos/create-bookable-space.dto';
|
||||||
|
import { UpdateBookableSpaceDto } from '../dtos/update-bookable-space.dto';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class BookableSpaceService {
|
||||||
|
constructor(
|
||||||
|
private readonly emailService: EmailService,
|
||||||
|
private readonly bookableSpaceEntityRepository: BookableSpaceEntityRepository,
|
||||||
|
private readonly bookingEntityRepository: BookingEntityRepository,
|
||||||
|
private readonly spaceRepository: SpaceRepository,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
async create(dto: CreateBookableSpaceDto) {
|
||||||
|
// Validate time slots first
|
||||||
|
this.validateTimeSlot(dto.startTime, dto.endTime);
|
||||||
|
|
||||||
|
// fetch spaces exist
|
||||||
|
const spaces = await this.getSpacesOrFindMissing(dto.spaceUuids);
|
||||||
|
|
||||||
|
// Validate no duplicate bookable configurations
|
||||||
|
await this.validateNoDuplicateBookableConfigs(dto.spaceUuids);
|
||||||
|
|
||||||
|
// Create and save bookable spaces
|
||||||
|
return this.createBookableSpaces(spaces, dto);
|
||||||
|
}
|
||||||
|
|
||||||
|
async findAll(
|
||||||
|
{ active, page, size, configured, search }: BookableSpaceRequestDto,
|
||||||
|
project: string,
|
||||||
|
): Promise<{
|
||||||
|
data: BaseResponseDto['data'];
|
||||||
|
pagination: PageResponseDto;
|
||||||
|
}> {
|
||||||
|
let qb = this.spaceRepository
|
||||||
|
.createQueryBuilder('space')
|
||||||
|
.leftJoinAndSelect('space.parent', 'parentSpace')
|
||||||
|
.leftJoinAndSelect('space.community', 'community')
|
||||||
|
.where('space.disabled = :disabled', { disabled: false })
|
||||||
|
.andWhere('community.project = :project', { project });
|
||||||
|
|
||||||
|
if (search) {
|
||||||
|
qb = qb.andWhere(
|
||||||
|
'(space.spaceName ILIKE :search OR community.name ILIKE :search OR parentSpace.spaceName ILIKE :search)',
|
||||||
|
{ search: `%${search}%` },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (configured) {
|
||||||
|
qb = qb
|
||||||
|
.leftJoinAndSelect('space.bookableConfig', 'bookableConfig')
|
||||||
|
.andWhere('bookableConfig.uuid IS NOT NULL');
|
||||||
|
if (active !== undefined) {
|
||||||
|
qb = qb.andWhere('bookableConfig.active = :active', { active });
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
qb = qb
|
||||||
|
.leftJoinAndSelect('space.bookableConfig', 'bookableConfig')
|
||||||
|
.andWhere('bookableConfig.uuid IS NULL');
|
||||||
|
}
|
||||||
|
const customModel = TypeORMCustomModel(this.spaceRepository);
|
||||||
|
|
||||||
|
const { baseResponseDto, paginationResponseDto } =
|
||||||
|
await customModel.findAll({ page, size, modelName: 'space' }, qb);
|
||||||
|
return {
|
||||||
|
data: baseResponseDto.data.map((space) => {
|
||||||
|
return {
|
||||||
|
...space,
|
||||||
|
virtualLocation: `${space.community?.name} - ${space.parent ? space.parent?.spaceName + ' - ' : ''}${space.spaceName}`,
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
pagination: paginationResponseDto,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update bookable space configuration
|
||||||
|
*/
|
||||||
|
async update(spaceUuid: string, dto: UpdateBookableSpaceDto) {
|
||||||
|
// fetch spaces exist
|
||||||
|
const space = (await this.getSpacesOrFindMissing([spaceUuid]))[0];
|
||||||
|
|
||||||
|
if (!space.bookableConfig) {
|
||||||
|
throw new NotFoundException(
|
||||||
|
`Bookable configuration not found for space: ${spaceUuid}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (dto.startTime || dto.endTime) {
|
||||||
|
// Validate time slots first
|
||||||
|
this.validateTimeSlot(
|
||||||
|
dto.startTime || space.bookableConfig.startTime,
|
||||||
|
dto.endTime || space.bookableConfig.endTime,
|
||||||
|
);
|
||||||
|
if (
|
||||||
|
dto.startTime != space.bookableConfig.startTime ||
|
||||||
|
dto.endTime != space.bookableConfig.endTime ||
|
||||||
|
dto.daysAvailable != space.bookableConfig.daysAvailable
|
||||||
|
) {
|
||||||
|
this.handleTimingUpdate(
|
||||||
|
{
|
||||||
|
daysAvailable:
|
||||||
|
dto.daysAvailable || space.bookableConfig.daysAvailable,
|
||||||
|
startTime: dto.startTime || space.bookableConfig.startTime,
|
||||||
|
endTime: dto.endTime || space.bookableConfig.endTime,
|
||||||
|
},
|
||||||
|
space,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
dto.active !== undefined &&
|
||||||
|
dto.active !== space.bookableConfig.active
|
||||||
|
) {
|
||||||
|
this.handleAvailabilityUpdate(dto.active, space);
|
||||||
|
}
|
||||||
|
|
||||||
|
Object.assign(space.bookableConfig, dto);
|
||||||
|
return this.bookableSpaceEntityRepository.save(space.bookableConfig);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async handleTimingUpdate(
|
||||||
|
dto: UpdateBookableSpaceDto,
|
||||||
|
space: SpaceEntity,
|
||||||
|
): Promise<void> {
|
||||||
|
const affectedUsers = await this.getAffectedBookings(space.uuid);
|
||||||
|
if (!affectedUsers.length) return;
|
||||||
|
|
||||||
|
const groupedParams = this.groupBookingsByUser(affectedUsers);
|
||||||
|
|
||||||
|
return this.emailService.sendUpdateBookingTimingEmailWithTemplate(
|
||||||
|
groupedParams,
|
||||||
|
{
|
||||||
|
space_name: space.spaceName,
|
||||||
|
start_time: to12HourFormat(dto.startTime),
|
||||||
|
end_time: to12HourFormat(dto.endTime),
|
||||||
|
days: dto.daysAvailable.join(', '),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async getAffectedBookings(spaceUuid: string) {
|
||||||
|
const today = new Date();
|
||||||
|
const nowTime = format(today, 'HH:mm');
|
||||||
|
|
||||||
|
const bookingWithDayCte = this.bookingEntityRepository
|
||||||
|
.createQueryBuilder('b')
|
||||||
|
.select('b.*')
|
||||||
|
.addSelect(
|
||||||
|
`
|
||||||
|
CASE EXTRACT(DOW FROM b.date)
|
||||||
|
WHEN 0 THEN 'Sun'
|
||||||
|
WHEN 1 THEN 'Mon'
|
||||||
|
WHEN 2 THEN 'Tue'
|
||||||
|
WHEN 3 THEN 'Wed'
|
||||||
|
WHEN 4 THEN 'Thu'
|
||||||
|
WHEN 5 THEN 'Fri'
|
||||||
|
WHEN 6 THEN 'Sat'
|
||||||
|
END::"bookable-space_days_available_enum"
|
||||||
|
`,
|
||||||
|
'booking_day',
|
||||||
|
)
|
||||||
|
.where(
|
||||||
|
`(DATE(b.date) > :today OR (DATE(b.date) = :today AND b.startTime >= :nowTime))`,
|
||||||
|
{ today, nowTime },
|
||||||
|
)
|
||||||
|
.andWhere('b.space_uuid = :spaceUuid', { spaceUuid });
|
||||||
|
|
||||||
|
const query = this.bookableSpaceEntityRepository
|
||||||
|
.createQueryBuilder('bs')
|
||||||
|
.distinct(true)
|
||||||
|
.addCommonTableExpression(bookingWithDayCte, 'booking_with_day')
|
||||||
|
.select('u.first_name', 'name')
|
||||||
|
.addSelect('u.email', 'email')
|
||||||
|
.addSelect('DATE(bwd.date)', 'date')
|
||||||
|
.addSelect('bwd.start_time', 'start_time')
|
||||||
|
.addSelect('bwd.end_time', 'end_time')
|
||||||
|
.from('booking_with_day', 'bwd')
|
||||||
|
.innerJoin('user', 'u', 'u.uuid = bwd.user_uuid')
|
||||||
|
.where('bs.space_uuid = :spaceUuid', { spaceUuid })
|
||||||
|
.andWhere(
|
||||||
|
new Brackets((qb) => {
|
||||||
|
qb.where('NOT (bwd.booking_day = ANY(bs.days_available))')
|
||||||
|
.orWhere('bwd.start_time < bs.start_time')
|
||||||
|
.orWhere('bwd.end_time > bs.end_time');
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
return query.getRawMany<{
|
||||||
|
name: string;
|
||||||
|
email: string;
|
||||||
|
date: string;
|
||||||
|
start_time: string;
|
||||||
|
end_time: string;
|
||||||
|
}>();
|
||||||
|
}
|
||||||
|
|
||||||
|
private groupBookingsByUser(
|
||||||
|
bookings: {
|
||||||
|
name: string;
|
||||||
|
email: string;
|
||||||
|
date: string;
|
||||||
|
start_time: string;
|
||||||
|
end_time: string;
|
||||||
|
}[],
|
||||||
|
): {
|
||||||
|
name: string;
|
||||||
|
email: string;
|
||||||
|
bookings: { date: string; start_time: string; end_time: string }[];
|
||||||
|
}[] {
|
||||||
|
const grouped: Record<
|
||||||
|
string,
|
||||||
|
{
|
||||||
|
name: string;
|
||||||
|
email: string;
|
||||||
|
bookings: { date: string; start_time: string; end_time: string }[];
|
||||||
|
}
|
||||||
|
> = {};
|
||||||
|
|
||||||
|
for (const { name, email, date, start_time, end_time } of bookings) {
|
||||||
|
const formattedDate = format(new Date(date), 'yyyy-MM-dd');
|
||||||
|
const formattedStartTime = to12HourFormat(start_time);
|
||||||
|
const formattedEndTime = to12HourFormat(end_time);
|
||||||
|
|
||||||
|
if (!grouped[email]) {
|
||||||
|
grouped[email] = {
|
||||||
|
name,
|
||||||
|
email,
|
||||||
|
bookings: [],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
grouped[email].bookings.push({
|
||||||
|
date: formattedDate,
|
||||||
|
start_time: formattedStartTime,
|
||||||
|
end_time: formattedEndTime,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return Object.values(grouped);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async handleAvailabilityUpdate(
|
||||||
|
active: boolean,
|
||||||
|
space: SpaceEntity,
|
||||||
|
): Promise<void> {
|
||||||
|
space = await this.spaceRepository.findOne({
|
||||||
|
where: { uuid: space.uuid },
|
||||||
|
relations: ['userSpaces', 'userSpaces.user'],
|
||||||
|
});
|
||||||
|
const emails = space.userSpaces.map((userSpace) => ({
|
||||||
|
email: userSpace.user.email,
|
||||||
|
name: userSpace.user.firstName,
|
||||||
|
}));
|
||||||
|
if (!emails.length) return Promise.resolve();
|
||||||
|
|
||||||
|
return this.emailService.sendUpdateBookingAvailabilityEmailWithTemplate(
|
||||||
|
emails,
|
||||||
|
{
|
||||||
|
availability: active ? 'Available' : 'Unavailable',
|
||||||
|
space_name: space.spaceName,
|
||||||
|
isAvailable: active,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch spaces by UUIDs and throw an error if any are missing
|
||||||
|
*/
|
||||||
|
private async getSpacesOrFindMissing(
|
||||||
|
spaceUuids: string[],
|
||||||
|
): Promise<SpaceEntity[]> {
|
||||||
|
const spaces = await this.spaceRepository.find({
|
||||||
|
where: { uuid: In(spaceUuids) },
|
||||||
|
relations: ['bookableConfig'],
|
||||||
|
});
|
||||||
|
|
||||||
|
if (spaces.length !== spaceUuids.length) {
|
||||||
|
const foundUuids = spaces.map((s) => s.uuid);
|
||||||
|
const missingUuids = spaceUuids.filter(
|
||||||
|
(uuid) => !foundUuids.includes(uuid),
|
||||||
|
);
|
||||||
|
throw new NotFoundException(
|
||||||
|
`Spaces not found: ${missingUuids.join(', ')}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return spaces;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validate there are no existing bookable configurations for these spaces
|
||||||
|
*/
|
||||||
|
private async validateNoDuplicateBookableConfigs(
|
||||||
|
spaceUuids: string[],
|
||||||
|
): Promise<void> {
|
||||||
|
const existingBookables = await this.bookableSpaceEntityRepository.find({
|
||||||
|
where: { space: { uuid: In(spaceUuids) } },
|
||||||
|
relations: ['space'],
|
||||||
|
});
|
||||||
|
|
||||||
|
if (existingBookables.length > 0) {
|
||||||
|
const existingUuids = [
|
||||||
|
...new Set(existingBookables.map((b) => b.space.uuid)),
|
||||||
|
];
|
||||||
|
throw new ConflictException(
|
||||||
|
`Bookable configuration already exists for spaces: ${existingUuids.join(', ')}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensure the slot start time is before the end time
|
||||||
|
*/
|
||||||
|
private validateTimeSlot(startTime: string, endTime: string): void {
|
||||||
|
const start = timeToMinutes(startTime);
|
||||||
|
const end = timeToMinutes(endTime);
|
||||||
|
|
||||||
|
if (start >= end) {
|
||||||
|
throw new BadRequestException(
|
||||||
|
`End time must be after start time for slot: ${startTime}-${endTime}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create bookable space entries after all validations pass
|
||||||
|
*/
|
||||||
|
private async createBookableSpaces(
|
||||||
|
spaces: SpaceEntity[],
|
||||||
|
dto: CreateBookableSpaceDto,
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
const entries = spaces.map((space) =>
|
||||||
|
this.bookableSpaceEntityRepository.create({
|
||||||
|
space,
|
||||||
|
daysAvailable: dto.daysAvailable,
|
||||||
|
startTime: dto.startTime,
|
||||||
|
endTime: dto.endTime,
|
||||||
|
points: dto.points,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
return this.bookableSpaceEntityRepository.save(entries);
|
||||||
|
} catch (error) {
|
||||||
|
if (error.code === '23505') {
|
||||||
|
throw new ConflictException(
|
||||||
|
'Duplicate bookable space configuration detected',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
218
src/booking/services/booking.service.ts
Normal file
218
src/booking/services/booking.service.ts
Normal file
@ -0,0 +1,218 @@
|
|||||||
|
import { DaysEnum } from '@app/common/constants/days.enum';
|
||||||
|
import { timeToMinutes } from '@app/common/helper/timeToMinutes';
|
||||||
|
import { BookingEntityRepository } from '@app/common/modules/booking/repositories/booking.repository';
|
||||||
|
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
|
||||||
|
import { SpaceRepository } from '@app/common/modules/space/repositories/space.repository';
|
||||||
|
import { UserRepository } from '@app/common/modules/user/repositories/user.repository';
|
||||||
|
import {
|
||||||
|
BadRequestException,
|
||||||
|
ConflictException,
|
||||||
|
ForbiddenException,
|
||||||
|
Injectable,
|
||||||
|
NotFoundException,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
import { format } from 'date-fns';
|
||||||
|
import { Between } from 'typeorm/find-options/operator/Between';
|
||||||
|
import { BookingRequestDto } from '../dtos/booking-request.dto';
|
||||||
|
import { CreateBookingDto } from '../dtos/create-booking.dto';
|
||||||
|
import { MyBookingRequestDto } from '../dtos/my-booking-request.dto';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class BookingService {
|
||||||
|
constructor(
|
||||||
|
private readonly bookingEntityRepository: BookingEntityRepository,
|
||||||
|
private readonly spaceRepository: SpaceRepository,
|
||||||
|
private readonly userRepository: UserRepository,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
async create(userUuid: string, dto: CreateBookingDto) {
|
||||||
|
console.log(userUuid);
|
||||||
|
const user = await this.userRepository.findOne({
|
||||||
|
where: { uuid: userUuid },
|
||||||
|
relations: ['userSpaces', 'userSpaces.space'],
|
||||||
|
});
|
||||||
|
console.log(user.userSpaces);
|
||||||
|
if (!user.userSpaces.some(({ space }) => space.uuid === dto.spaceUuid)) {
|
||||||
|
throw new ForbiddenException(
|
||||||
|
`User does not have permission to book this space: ${dto.spaceUuid}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
// Validate time slots first
|
||||||
|
this.validateTimeSlot(dto.startTime, dto.endTime);
|
||||||
|
|
||||||
|
// fetch spaces exist
|
||||||
|
const space = await this.getSpaceConfigurationAndBookings(dto.spaceUuid);
|
||||||
|
|
||||||
|
// Validate booking availability
|
||||||
|
this.validateBookingAvailability(space, dto);
|
||||||
|
|
||||||
|
// Create and save booking
|
||||||
|
return this.createBookings(space, userUuid, dto);
|
||||||
|
}
|
||||||
|
|
||||||
|
async findAll({ month, space }: BookingRequestDto, project: string) {
|
||||||
|
const [monthNumber, year] = month.split('-').map(Number);
|
||||||
|
const fromDate = new Date(year, monthNumber - 1, 1);
|
||||||
|
const toDate = new Date(year, monthNumber, 0, 23, 59, 59);
|
||||||
|
return this.bookingEntityRepository.find({
|
||||||
|
where: {
|
||||||
|
space: {
|
||||||
|
community: { project: { uuid: project } },
|
||||||
|
uuid: space ? space : undefined,
|
||||||
|
},
|
||||||
|
date: Between(fromDate, toDate),
|
||||||
|
},
|
||||||
|
relations: ['space', 'user', 'user.inviteUser'],
|
||||||
|
order: { date: 'DESC' },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async findMyBookings(
|
||||||
|
{ when }: MyBookingRequestDto,
|
||||||
|
userUuid: string,
|
||||||
|
project: string,
|
||||||
|
) {
|
||||||
|
const now = new Date();
|
||||||
|
const nowTime = format(now, 'HH:mm');
|
||||||
|
|
||||||
|
const query = this.bookingEntityRepository
|
||||||
|
.createQueryBuilder('booking')
|
||||||
|
.leftJoinAndSelect('booking.space', 'space')
|
||||||
|
.innerJoin(
|
||||||
|
'space.community',
|
||||||
|
'community',
|
||||||
|
'community.project = :project',
|
||||||
|
{ project },
|
||||||
|
)
|
||||||
|
.leftJoinAndSelect('booking.user', 'user')
|
||||||
|
.where('user.uuid = :userUuid', { userUuid });
|
||||||
|
|
||||||
|
if (when === 'past') {
|
||||||
|
query.andWhere(
|
||||||
|
`(DATE(booking.date) < :today OR (DATE(booking.date) = :today AND booking.startTime < :nowTime))`,
|
||||||
|
{ today: now, nowTime },
|
||||||
|
);
|
||||||
|
} else if (when === 'future') {
|
||||||
|
query.andWhere(
|
||||||
|
`(DATE(booking.date) > :today OR (DATE(booking.date) = :today AND booking.startTime >= :nowTime))`,
|
||||||
|
{ today: now, nowTime },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
query.orderBy({
|
||||||
|
'DATE(booking.date)': 'DESC',
|
||||||
|
'booking.startTime': 'DESC',
|
||||||
|
});
|
||||||
|
|
||||||
|
return query.getMany();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch space by UUID and throw an error if not found or if not configured for booking
|
||||||
|
*/
|
||||||
|
private async getSpaceConfigurationAndBookings(
|
||||||
|
spaceUuid: string,
|
||||||
|
): Promise<SpaceEntity> {
|
||||||
|
const space = await this.spaceRepository.findOne({
|
||||||
|
where: { uuid: spaceUuid },
|
||||||
|
relations: ['bookableConfig', 'bookings'],
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!space) {
|
||||||
|
throw new NotFoundException(`Space not found: ${spaceUuid}`);
|
||||||
|
}
|
||||||
|
if (!space.bookableConfig) {
|
||||||
|
throw new NotFoundException(
|
||||||
|
`This space is not configured for booking: ${spaceUuid}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return space;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensure the slot start time is before the end time
|
||||||
|
*/
|
||||||
|
private validateTimeSlot(startTime: string, endTime: string): void {
|
||||||
|
const start = timeToMinutes(startTime);
|
||||||
|
const end = timeToMinutes(endTime);
|
||||||
|
|
||||||
|
if (start >= end) {
|
||||||
|
throw new BadRequestException(
|
||||||
|
`End time must be after start time for slot: ${startTime}-${endTime}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* check if the space is available for booking on the requested day
|
||||||
|
* and if the requested time slot is within the available hours
|
||||||
|
*/
|
||||||
|
private validateBookingAvailability(
|
||||||
|
space: SpaceEntity,
|
||||||
|
dto: CreateBookingDto,
|
||||||
|
): void {
|
||||||
|
// Check if the space is available for booking on the requested day
|
||||||
|
const availableDays = space.bookableConfig?.daysAvailable || [];
|
||||||
|
const requestedDay = new Date(dto.date).toLocaleDateString('en-US', {
|
||||||
|
weekday: 'short',
|
||||||
|
}) as DaysEnum;
|
||||||
|
|
||||||
|
if (!availableDays.includes(requestedDay)) {
|
||||||
|
const dayFullName = new Date(dto.date).toLocaleDateString('en-US', {
|
||||||
|
weekday: 'long',
|
||||||
|
});
|
||||||
|
throw new BadRequestException(
|
||||||
|
`Space is not available for booking on ${dayFullName}s`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const dtoStartTimeInMinutes = timeToMinutes(dto.startTime);
|
||||||
|
const dtoEndTimeInMinutes = timeToMinutes(dto.endTime);
|
||||||
|
|
||||||
|
if (
|
||||||
|
dtoStartTimeInMinutes < timeToMinutes(space.bookableConfig.startTime) ||
|
||||||
|
dtoEndTimeInMinutes > timeToMinutes(space.bookableConfig.endTime)
|
||||||
|
) {
|
||||||
|
throw new BadRequestException(
|
||||||
|
`Booking time must be within the available hours for space: ${space.spaceName}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const previousBookings = space.bookings.filter(
|
||||||
|
(booking) =>
|
||||||
|
timeToMinutes(booking.startTime) < dtoEndTimeInMinutes &&
|
||||||
|
timeToMinutes(booking.endTime) > dtoStartTimeInMinutes &&
|
||||||
|
format(new Date(booking.date), 'yyyy-MM-dd') ===
|
||||||
|
format(new Date(dto.date), 'yyyy-MM-dd'),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (previousBookings.length > 0) {
|
||||||
|
// tell the user what time is unavailable
|
||||||
|
const unavailableTimes = previousBookings.map((booking) => {
|
||||||
|
return `${booking.startTime}-${booking.endTime}`;
|
||||||
|
});
|
||||||
|
throw new ConflictException(
|
||||||
|
`Space is already booked during this times: ${unavailableTimes.join(', ')}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Create bookable space entries after all validations pass
|
||||||
|
*/
|
||||||
|
private async createBookings(
|
||||||
|
space: SpaceEntity,
|
||||||
|
user: string,
|
||||||
|
{ spaceUuid, date, ...dto }: CreateBookingDto,
|
||||||
|
) {
|
||||||
|
const entry = this.bookingEntityRepository.create({
|
||||||
|
space: { uuid: spaceUuid },
|
||||||
|
user: { uuid: user },
|
||||||
|
...dto,
|
||||||
|
date: new Date(date),
|
||||||
|
cost: space.bookableConfig?.points || null,
|
||||||
|
});
|
||||||
|
|
||||||
|
return this.bookingEntityRepository.save(entry);
|
||||||
|
}
|
||||||
|
}
|
@ -30,6 +30,8 @@ import { PowerClampService } from '@app/common/helper/services/power.clamp.servi
|
|||||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||||
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
||||||
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
||||||
|
import { PresenceSensorDailySpaceRepository } from '@app/common/modules/presence-sensor/repositories';
|
||||||
|
import { AqiSpaceDailyPollutantStatsRepository } from '@app/common/modules/aqi/repositories';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule, SpaceRepositoryModule],
|
imports: [ConfigModule, SpaceRepositoryModule],
|
||||||
@ -59,6 +61,8 @@ import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
|||||||
SqlLoaderService,
|
SqlLoaderService,
|
||||||
OccupancyService,
|
OccupancyService,
|
||||||
AqiDataService,
|
AqiDataService,
|
||||||
|
PresenceSensorDailySpaceRepository,
|
||||||
|
AqiSpaceDailyPollutantStatsRepository,
|
||||||
],
|
],
|
||||||
exports: [],
|
exports: [],
|
||||||
})
|
})
|
||||||
|
@ -3,6 +3,7 @@ import * as fs from 'fs';
|
|||||||
|
|
||||||
import { ProjectParam } from '@app/common/dto/project-param.dto';
|
import { ProjectParam } from '@app/common/dto/project-param.dto';
|
||||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||||
|
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
|
||||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||||
@ -20,6 +21,7 @@ export class DeviceCommissionService {
|
|||||||
constructor(
|
constructor(
|
||||||
private readonly tuyaService: TuyaService,
|
private readonly tuyaService: TuyaService,
|
||||||
private readonly deviceService: DeviceService,
|
private readonly deviceService: DeviceService,
|
||||||
|
private readonly deviceStatusFirebaseService: DeviceStatusFirebaseService,
|
||||||
private readonly communityRepository: CommunityRepository,
|
private readonly communityRepository: CommunityRepository,
|
||||||
private readonly spaceRepository: SpaceRepository,
|
private readonly spaceRepository: SpaceRepository,
|
||||||
private readonly subspaceRepository: SubspaceRepository,
|
private readonly subspaceRepository: SubspaceRepository,
|
||||||
@ -209,6 +211,10 @@ export class DeviceCommissionService {
|
|||||||
rawDeviceId,
|
rawDeviceId,
|
||||||
tuyaSpaceId,
|
tuyaSpaceId,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
await this.deviceStatusFirebaseService.addDeviceStatusByDeviceUuid(
|
||||||
|
rawDeviceId,
|
||||||
|
);
|
||||||
successCount.value++;
|
successCount.value++;
|
||||||
console.log(
|
console.log(
|
||||||
`Device ${rawDeviceId} successfully processed and transferred to Tuya space ${tuyaSpaceId}`,
|
`Device ${rawDeviceId} successfully processed and transferred to Tuya space ${tuyaSpaceId}`,
|
||||||
|
@ -5,7 +5,6 @@ import { ConfigModule } from '@nestjs/config';
|
|||||||
import { SpaceRepositoryModule } from '@app/common/modules/space/space.repository.module';
|
import { SpaceRepositoryModule } from '@app/common/modules/space/space.repository.module';
|
||||||
import {
|
import {
|
||||||
InviteSpaceRepository,
|
InviteSpaceRepository,
|
||||||
SpaceLinkRepository,
|
|
||||||
SpaceProductAllocationRepository,
|
SpaceProductAllocationRepository,
|
||||||
SpaceRepository,
|
SpaceRepository,
|
||||||
} from '@app/common/modules/space/repositories';
|
} from '@app/common/modules/space/repositories';
|
||||||
@ -16,14 +15,12 @@ import { CommunityRepository } from '@app/common/modules/community/repositories'
|
|||||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||||
import { ProjectRepository } from '@app/common/modules/project/repositiories';
|
import { ProjectRepository } from '@app/common/modules/project/repositiories';
|
||||||
import {
|
import {
|
||||||
SpaceLinkService,
|
|
||||||
SpaceService,
|
SpaceService,
|
||||||
SubspaceDeviceService,
|
SubspaceDeviceService,
|
||||||
SubSpaceService,
|
SubSpaceService,
|
||||||
ValidationService,
|
ValidationService,
|
||||||
} from 'src/space/services';
|
} from 'src/space/services';
|
||||||
import { TagService as NewTagService } from 'src/tags/services';
|
import { TagService as NewTagService } from 'src/tags/services';
|
||||||
import { TagService } from 'src/space/services/tag';
|
|
||||||
import {
|
import {
|
||||||
SpaceModelService,
|
SpaceModelService,
|
||||||
SubSpaceModelService,
|
SubSpaceModelService,
|
||||||
@ -64,6 +61,8 @@ import {
|
|||||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||||
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
||||||
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
||||||
|
import { PresenceSensorDailySpaceRepository } from '@app/common/modules/presence-sensor/repositories';
|
||||||
|
import { AqiSpaceDailyPollutantStatsRepository } from '@app/common/modules/aqi/repositories';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule, SpaceRepositoryModule, UserRepositoryModule],
|
imports: [ConfigModule, SpaceRepositoryModule, UserRepositoryModule],
|
||||||
@ -78,16 +77,14 @@ import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
|||||||
ProjectRepository,
|
ProjectRepository,
|
||||||
SpaceService,
|
SpaceService,
|
||||||
InviteSpaceRepository,
|
InviteSpaceRepository,
|
||||||
SpaceLinkService,
|
// Todo: find out why this is needed
|
||||||
SubSpaceService,
|
SubSpaceService,
|
||||||
ValidationService,
|
ValidationService,
|
||||||
NewTagService,
|
NewTagService,
|
||||||
SpaceModelService,
|
SpaceModelService,
|
||||||
SpaceProductAllocationService,
|
SpaceProductAllocationService,
|
||||||
SpaceLinkRepository,
|
|
||||||
SubspaceRepository,
|
SubspaceRepository,
|
||||||
// Todo: find out why this is needed
|
// Todo: find out why this is needed
|
||||||
TagService,
|
|
||||||
SubspaceDeviceService,
|
SubspaceDeviceService,
|
||||||
SubspaceProductAllocationService,
|
SubspaceProductAllocationService,
|
||||||
SpaceModelRepository,
|
SpaceModelRepository,
|
||||||
@ -117,6 +114,8 @@ import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
|||||||
SqlLoaderService,
|
SqlLoaderService,
|
||||||
OccupancyService,
|
OccupancyService,
|
||||||
AqiDataService,
|
AqiDataService,
|
||||||
|
PresenceSensorDailySpaceRepository,
|
||||||
|
AqiSpaceDailyPollutantStatsRepository,
|
||||||
],
|
],
|
||||||
exports: [CommunityService, SpacePermissionService],
|
exports: [CommunityService, SpacePermissionService],
|
||||||
})
|
})
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
import { CommunityService } from '../services/community.service';
|
|
||||||
import {
|
import {
|
||||||
Body,
|
Body,
|
||||||
Controller,
|
Controller,
|
||||||
@ -10,17 +9,18 @@ import {
|
|||||||
Query,
|
Query,
|
||||||
UseGuards,
|
UseGuards,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
|
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||||
import { AddCommunityDto } from '../dtos/add.community.dto';
|
import { AddCommunityDto } from '../dtos/add.community.dto';
|
||||||
import { GetCommunityParams } from '../dtos/get.community.dto';
|
import { GetCommunityParams } from '../dtos/get.community.dto';
|
||||||
import { UpdateCommunityNameDto } from '../dtos/update.community.dto';
|
import { UpdateCommunityNameDto } from '../dtos/update.community.dto';
|
||||||
|
import { CommunityService } from '../services/community.service';
|
||||||
// import { CheckUserCommunityGuard } from 'src/guards/user.community.guard';
|
// import { CheckUserCommunityGuard } from 'src/guards/user.community.guard';
|
||||||
import { ControllerRoute } from '@app/common/constants/controller-route';
|
import { ControllerRoute } from '@app/common/constants/controller-route';
|
||||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||||
import { ProjectParam } from '../dtos';
|
|
||||||
import { PermissionsGuard } from 'src/guards/permissions.guard';
|
|
||||||
import { Permissions } from 'src/decorators/permissions.decorator';
|
|
||||||
import { PaginationRequestWithSearchGetListDto } from '@app/common/dto/pagination-with-search.request.dto';
|
import { PaginationRequestWithSearchGetListDto } from '@app/common/dto/pagination-with-search.request.dto';
|
||||||
|
import { Permissions } from 'src/decorators/permissions.decorator';
|
||||||
|
import { PermissionsGuard } from 'src/guards/permissions.guard';
|
||||||
|
import { ProjectParam } from '../dtos';
|
||||||
|
|
||||||
@ApiTags('Community Module')
|
@ApiTags('Community Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -45,6 +45,21 @@ export class CommunityController {
|
|||||||
return await this.communityService.createCommunity(param, addCommunityDto);
|
return await this.communityService.createCommunity(param, addCommunityDto);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(PermissionsGuard)
|
||||||
|
@Permissions('COMMUNITY_VIEW')
|
||||||
|
@ApiOperation({
|
||||||
|
summary: ControllerRoute.COMMUNITY.ACTIONS.LIST_COMMUNITY_SUMMARY,
|
||||||
|
description: ControllerRoute.COMMUNITY.ACTIONS.LIST_COMMUNITY_DESCRIPTION,
|
||||||
|
})
|
||||||
|
@Get('v2')
|
||||||
|
async getCommunitiesV2(
|
||||||
|
@Param() param: ProjectParam,
|
||||||
|
@Query() query: PaginationRequestWithSearchGetListDto,
|
||||||
|
): Promise<any> {
|
||||||
|
return this.communityService.getCommunitiesV2(param, query);
|
||||||
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(PermissionsGuard)
|
@UseGuards(PermissionsGuard)
|
||||||
@Permissions('COMMUNITY_VIEW')
|
@Permissions('COMMUNITY_VIEW')
|
||||||
|
@ -1,4 +1,7 @@
|
|||||||
import { ORPHAN_COMMUNITY_NAME } from '@app/common/constants/orphan-constant';
|
import {
|
||||||
|
ORPHAN_COMMUNITY_NAME,
|
||||||
|
ORPHAN_SPACE_NAME,
|
||||||
|
} from '@app/common/constants/orphan-constant';
|
||||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||||
import { PageResponse } from '@app/common/dto/pagination.response.dto';
|
import { PageResponse } from '@app/common/dto/pagination.response.dto';
|
||||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||||
@ -22,7 +25,7 @@ import {
|
|||||||
NotFoundException,
|
NotFoundException,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { SpaceService } from 'src/space/services';
|
import { SpaceService } from 'src/space/services';
|
||||||
import { SelectQueryBuilder } from 'typeorm';
|
import { Brackets, QueryRunner, SelectQueryBuilder } from 'typeorm';
|
||||||
import { AddCommunityDto, GetCommunityParams, ProjectParam } from '../dtos';
|
import { AddCommunityDto, GetCommunityParams, ProjectParam } from '../dtos';
|
||||||
import { UpdateCommunityNameDto } from '../dtos/update.community.dto';
|
import { UpdateCommunityNameDto } from '../dtos/update.community.dto';
|
||||||
|
|
||||||
@ -69,12 +72,18 @@ export class CommunityService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getCommunityById(params: GetCommunityParams): Promise<BaseResponseDto> {
|
async getCommunityById(
|
||||||
|
params: GetCommunityParams,
|
||||||
|
queryRunner?: QueryRunner,
|
||||||
|
): Promise<BaseResponseDto> {
|
||||||
const { communityUuid, projectUuid } = params;
|
const { communityUuid, projectUuid } = params;
|
||||||
|
|
||||||
await this.validateProject(projectUuid);
|
await this.validateProject(projectUuid);
|
||||||
|
|
||||||
const community = await this.communityRepository.findOneBy({
|
const communityRepository =
|
||||||
|
queryRunner?.manager.getRepository(CommunityEntity) ||
|
||||||
|
this.communityRepository;
|
||||||
|
const community = await communityRepository.findOneBy({
|
||||||
uuid: communityUuid,
|
uuid: communityUuid,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -162,6 +171,94 @@ export class CommunityService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getCommunitiesV2(
|
||||||
|
{ projectUuid }: ProjectParam,
|
||||||
|
{
|
||||||
|
search,
|
||||||
|
includeSpaces,
|
||||||
|
...pageable
|
||||||
|
}: Partial<ExtendedTypeORMCustomModelFindAllQuery>,
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
const project = await this.validateProject(projectUuid);
|
||||||
|
|
||||||
|
let qb: undefined | SelectQueryBuilder<CommunityEntity> = undefined;
|
||||||
|
|
||||||
|
const matchingCommunityIdsQb = this.communityRepository
|
||||||
|
.createQueryBuilder('c')
|
||||||
|
.select('c.uuid')
|
||||||
|
.where('c.project = :projectUuid', { projectUuid })
|
||||||
|
.andWhere('c.name != :orphanCommunityName', {
|
||||||
|
orphanCommunityName: `${ORPHAN_COMMUNITY_NAME}-${project.name}`,
|
||||||
|
})
|
||||||
|
.distinct(true);
|
||||||
|
if (includeSpaces) {
|
||||||
|
matchingCommunityIdsQb.leftJoin('c.spaces', 'space');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (search) {
|
||||||
|
matchingCommunityIdsQb
|
||||||
|
.andWhere(
|
||||||
|
new Brackets((qb) => {
|
||||||
|
qb.where('c.name ILIKE :search');
|
||||||
|
if (includeSpaces) qb.orWhere('space.spaceName ILIKE :search');
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.setParameter('search', `%${search}%`);
|
||||||
|
}
|
||||||
|
|
||||||
|
qb = this.communityRepository
|
||||||
|
.createQueryBuilder('c')
|
||||||
|
.where('c.project = :projectUuid', { projectUuid })
|
||||||
|
.andWhere('c.name != :orphanCommunityName', {
|
||||||
|
orphanCommunityName: `${ORPHAN_COMMUNITY_NAME}-${project.name}`,
|
||||||
|
})
|
||||||
|
.andWhere(`c.uuid IN (${matchingCommunityIdsQb.getQuery()})`)
|
||||||
|
.setParameters(matchingCommunityIdsQb.getParameters());
|
||||||
|
if (includeSpaces) {
|
||||||
|
qb.leftJoinAndSelect(
|
||||||
|
'c.spaces',
|
||||||
|
'space',
|
||||||
|
'space.disabled = :disabled AND space.spaceName != :orphanSpaceName',
|
||||||
|
{
|
||||||
|
disabled: false,
|
||||||
|
orphanSpaceName: ORPHAN_SPACE_NAME,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.leftJoinAndSelect('space.parent', 'parent')
|
||||||
|
.leftJoinAndSelect(
|
||||||
|
'space.children',
|
||||||
|
'children',
|
||||||
|
'children.disabled = :disabled',
|
||||||
|
{ disabled: false },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const customModel = TypeORMCustomModel(this.communityRepository);
|
||||||
|
|
||||||
|
const { baseResponseDto, paginationResponseDto } =
|
||||||
|
await customModel.findAll({ ...pageable, modelName: 'community' }, qb);
|
||||||
|
if (includeSpaces) {
|
||||||
|
baseResponseDto.data = baseResponseDto.data.map((community) => ({
|
||||||
|
...community,
|
||||||
|
spaces: this.spaceService.buildSpaceHierarchy(community.spaces || []),
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
return new PageResponse<CommunityDto>(
|
||||||
|
baseResponseDto,
|
||||||
|
paginationResponseDto,
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
// Generic error handling
|
||||||
|
if (error instanceof HttpException) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || 'An error occurred while fetching communities.',
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async updateCommunity(
|
async updateCommunity(
|
||||||
params: GetCommunityParams,
|
params: GetCommunityParams,
|
||||||
updateCommunityDto: UpdateCommunityNameDto,
|
updateCommunityDto: UpdateCommunityNameDto,
|
||||||
|
28
src/device/commands/cur2-commands.ts
Normal file
28
src/device/commands/cur2-commands.ts
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
interface BaseCommand {
|
||||||
|
code: string;
|
||||||
|
value: any;
|
||||||
|
}
|
||||||
|
export interface ControlCur2Command extends BaseCommand {
|
||||||
|
code: 'control';
|
||||||
|
value: 'open' | 'close' | 'stop';
|
||||||
|
}
|
||||||
|
export interface ControlCur2PercentCommand extends BaseCommand {
|
||||||
|
code: 'percent_control';
|
||||||
|
value: 10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 | 90 | 100;
|
||||||
|
}
|
||||||
|
export interface ControlCur2AccurateCalibrationCommand extends BaseCommand {
|
||||||
|
code: 'accurate_calibration';
|
||||||
|
value: 'start' | 'end'; // Assuming this is a numeric value for calibration
|
||||||
|
}
|
||||||
|
export interface ControlCur2TDirectionConCommand extends BaseCommand {
|
||||||
|
code: 'control_t_direction_con';
|
||||||
|
value: 'forward' | 'back';
|
||||||
|
}
|
||||||
|
export interface ControlCur2QuickCalibrationCommand extends BaseCommand {
|
||||||
|
code: 'tr_timecon';
|
||||||
|
value: number; // between 10 and 120
|
||||||
|
}
|
||||||
|
export interface ControlCur2MotorModeCommand extends BaseCommand {
|
||||||
|
code: 'elec_machinery_mode';
|
||||||
|
value: 'strong_power' | 'dry_contact';
|
||||||
|
}
|
@ -1,11 +1,11 @@
|
|||||||
import { DeviceService } from '../services/device.service';
|
|
||||||
import { Controller, Get, Param, Query, UseGuards } from '@nestjs/common';
|
|
||||||
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
|
|
||||||
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
|
|
||||||
import { ControllerRoute } from '@app/common/constants/controller-route';
|
import { ControllerRoute } from '@app/common/constants/controller-route';
|
||||||
import { PermissionsGuard } from 'src/guards/permissions.guard';
|
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
|
||||||
|
import { Controller, Get, Param, Query, UseGuards } from '@nestjs/common';
|
||||||
|
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||||
import { Permissions } from 'src/decorators/permissions.decorator';
|
import { Permissions } from 'src/decorators/permissions.decorator';
|
||||||
import { GetDoorLockDevices, ProjectParam } from '../dtos';
|
import { PermissionsGuard } from 'src/guards/permissions.guard';
|
||||||
|
import { GetDevicesFilterDto, ProjectParam } from '../dtos';
|
||||||
|
import { DeviceService } from '../services/device.service';
|
||||||
|
|
||||||
@ApiTags('Device Module')
|
@ApiTags('Device Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -25,7 +25,7 @@ export class DeviceProjectController {
|
|||||||
})
|
})
|
||||||
async getAllDevices(
|
async getAllDevices(
|
||||||
@Param() param: ProjectParam,
|
@Param() param: ProjectParam,
|
||||||
@Query() query: GetDoorLockDevices,
|
@Query() query: GetDevicesFilterDto,
|
||||||
) {
|
) {
|
||||||
return await this.deviceService.getAllDevices(param, query);
|
return await this.deviceService.getAllDevices(param, query);
|
||||||
}
|
}
|
||||||
|
@ -1,39 +1,41 @@
|
|||||||
import { DeviceService } from '../services/device.service';
|
import { ControllerRoute } from '@app/common/constants/controller-route';
|
||||||
|
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
|
||||||
|
import { RoleType } from '@app/common/constants/role.type.enum';
|
||||||
|
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||||
import {
|
import {
|
||||||
Body,
|
Body,
|
||||||
Controller,
|
Controller,
|
||||||
Get,
|
|
||||||
Post,
|
|
||||||
Query,
|
|
||||||
Param,
|
|
||||||
UseGuards,
|
|
||||||
Put,
|
|
||||||
Delete,
|
Delete,
|
||||||
|
Get,
|
||||||
|
Param,
|
||||||
|
Post,
|
||||||
|
Put,
|
||||||
|
Query,
|
||||||
Req,
|
Req,
|
||||||
|
UnauthorizedException,
|
||||||
|
UseGuards,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
|
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||||
|
import { Permissions } from 'src/decorators/permissions.decorator';
|
||||||
|
import { PermissionsGuard } from 'src/guards/permissions.guard';
|
||||||
|
import { CheckRoomGuard } from 'src/guards/room.guard';
|
||||||
|
import { CheckFourAndSixSceneDeviceTypeGuard } from 'src/guards/scene.device.type.guard';
|
||||||
import {
|
import {
|
||||||
AddDeviceDto,
|
AddDeviceDto,
|
||||||
AddSceneToFourSceneDeviceDto,
|
AddSceneToFourSceneDeviceDto,
|
||||||
AssignDeviceToSpaceDto,
|
AssignDeviceToSpaceDto,
|
||||||
UpdateDeviceDto,
|
UpdateDeviceDto,
|
||||||
} from '../dtos/add.device.dto';
|
} from '../dtos/add.device.dto';
|
||||||
import { GetDeviceLogsDto } from '../dtos/get.device.dto';
|
|
||||||
import {
|
import {
|
||||||
ControlDeviceDto,
|
|
||||||
BatchControlDevicesDto,
|
BatchControlDevicesDto,
|
||||||
BatchStatusDevicesDto,
|
BatchStatusDevicesDto,
|
||||||
|
ControlDeviceDto,
|
||||||
GetSceneFourSceneDeviceDto,
|
GetSceneFourSceneDeviceDto,
|
||||||
} from '../dtos/control.device.dto';
|
} from '../dtos/control.device.dto';
|
||||||
import { CheckRoomGuard } from 'src/guards/room.guard';
|
|
||||||
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
|
|
||||||
import { CheckFourAndSixSceneDeviceTypeGuard } from 'src/guards/scene.device.type.guard';
|
|
||||||
import { ControllerRoute } from '@app/common/constants/controller-route';
|
|
||||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
|
||||||
import { DeviceSceneParamDto } from '../dtos/device.param.dto';
|
|
||||||
import { DeleteSceneFromSceneDeviceDto } from '../dtos/delete.device.dto';
|
import { DeleteSceneFromSceneDeviceDto } from '../dtos/delete.device.dto';
|
||||||
import { PermissionsGuard } from 'src/guards/permissions.guard';
|
import { DeviceSceneParamDto } from '../dtos/device.param.dto';
|
||||||
import { Permissions } from 'src/decorators/permissions.decorator';
|
import { GetDeviceLogsDto } from '../dtos/get.device.dto';
|
||||||
|
import { DeviceService } from '../services/device.service';
|
||||||
|
|
||||||
@ApiTags('Device Module')
|
@ApiTags('Device Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -340,4 +342,22 @@ export class DeviceController {
|
|||||||
projectUuid,
|
projectUuid,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(PermissionsGuard)
|
||||||
|
@Permissions('DEVICE_UPDATE')
|
||||||
|
@Post('/populate-tuya-const-uuids')
|
||||||
|
@ApiOperation({
|
||||||
|
summary: ControllerRoute.DEVICE.ACTIONS.POPULATE_TUYA_CONST_UUID_SUMMARY,
|
||||||
|
description:
|
||||||
|
ControllerRoute.DEVICE.ACTIONS.POPULATE_TUYA_CONST_UUID_DESCRIPTION,
|
||||||
|
})
|
||||||
|
async populateTuyaConstUuid(@Req() req: any): Promise<void> {
|
||||||
|
const userUuid = req['user']?.userUuid;
|
||||||
|
const userRole = req['user']?.role;
|
||||||
|
if (!userUuid || (userRole && userRole !== RoleType.SUPER_ADMIN)) {
|
||||||
|
throw new UnauthorizedException('Unauthorized to perform this action');
|
||||||
|
}
|
||||||
|
return this.deviceService.addTuyaConstUuidToDevices();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,14 @@ export class AddDeviceDto {
|
|||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
public spaceUuid: string;
|
public spaceUuid: string;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'tagUuid',
|
||||||
|
required: true,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
public tagUuid: string;
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'deviceName',
|
description: 'deviceName',
|
||||||
required: true,
|
required: true,
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import { DeviceTypeEnum } from '@app/common/constants/device-type.enum';
|
import { DeviceTypeEnum } from '@app/common/constants/device-type.enum';
|
||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
|
import { Transform } from 'class-transformer';
|
||||||
import {
|
import {
|
||||||
IsEnum,
|
IsEnum,
|
||||||
IsNotEmpty,
|
IsNotEmpty,
|
||||||
@ -41,16 +42,7 @@ export class GetDeviceLogsDto {
|
|||||||
@IsOptional()
|
@IsOptional()
|
||||||
public endTime: string;
|
public endTime: string;
|
||||||
}
|
}
|
||||||
export class GetDoorLockDevices {
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'Device Type',
|
|
||||||
enum: DeviceTypeEnum,
|
|
||||||
required: false,
|
|
||||||
})
|
|
||||||
@IsEnum(DeviceTypeEnum)
|
|
||||||
@IsOptional()
|
|
||||||
public deviceType: DeviceTypeEnum;
|
|
||||||
}
|
|
||||||
export class GetDevicesBySpaceOrCommunityDto {
|
export class GetDevicesBySpaceOrCommunityDto {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'Device Product Type',
|
description: 'Device Product Type',
|
||||||
@ -72,3 +64,44 @@ export class GetDevicesBySpaceOrCommunityDto {
|
|||||||
@IsNotEmpty({ message: 'Either spaceUuid or communityUuid must be provided' })
|
@IsNotEmpty({ message: 'Either spaceUuid or communityUuid must be provided' })
|
||||||
requireEither?: never; // This ensures at least one of them is provided
|
requireEither?: never; // This ensures at least one of them is provided
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class GetDevicesFilterDto {
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'Device Type',
|
||||||
|
enum: DeviceTypeEnum,
|
||||||
|
required: false,
|
||||||
|
})
|
||||||
|
@IsEnum(DeviceTypeEnum)
|
||||||
|
@IsOptional()
|
||||||
|
public deviceType: DeviceTypeEnum;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'List of Space IDs to filter devices',
|
||||||
|
required: false,
|
||||||
|
example: ['60d21b4667d0d8992e610c85', '60d21b4967d0d8992e610c86'],
|
||||||
|
})
|
||||||
|
@IsOptional()
|
||||||
|
@Transform(({ value }) => {
|
||||||
|
if (!Array.isArray(value)) {
|
||||||
|
return [value];
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
})
|
||||||
|
@IsUUID('4', { each: true })
|
||||||
|
public spaces?: string[];
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'List of Community IDs to filter devices',
|
||||||
|
required: false,
|
||||||
|
example: ['60d21b4667d0d8992e610c85', '60d21b4967d0d8992e610c86'],
|
||||||
|
})
|
||||||
|
@Transform(({ value }) => {
|
||||||
|
if (!Array.isArray(value)) {
|
||||||
|
return [value];
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
})
|
||||||
|
@IsOptional()
|
||||||
|
@IsUUID('4', { each: true })
|
||||||
|
public communities?: string[];
|
||||||
|
}
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -30,6 +30,8 @@ import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service
|
|||||||
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
||||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||||
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
||||||
|
import { PresenceSensorDailySpaceRepository } from '@app/common/modules/presence-sensor/repositories';
|
||||||
|
import { AqiSpaceDailyPollutantStatsRepository } from '@app/common/modules/aqi/repositories';
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule, DeviceRepositoryModule],
|
imports: [ConfigModule, DeviceRepositoryModule],
|
||||||
controllers: [DoorLockController],
|
controllers: [DoorLockController],
|
||||||
@ -58,6 +60,8 @@ import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
|||||||
OccupancyService,
|
OccupancyService,
|
||||||
CommunityRepository,
|
CommunityRepository,
|
||||||
AqiDataService,
|
AqiDataService,
|
||||||
|
PresenceSensorDailySpaceRepository,
|
||||||
|
AqiSpaceDailyPollutantStatsRepository,
|
||||||
],
|
],
|
||||||
exports: [DoorLockService],
|
exports: [DoorLockService],
|
||||||
})
|
})
|
||||||
|
@ -28,6 +28,8 @@ import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service
|
|||||||
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
||||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||||
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
||||||
|
import { PresenceSensorDailySpaceRepository } from '@app/common/modules/presence-sensor/repositories';
|
||||||
|
import { AqiSpaceDailyPollutantStatsRepository } from '@app/common/modules/aqi/repositories';
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule, DeviceRepositoryModule],
|
imports: [ConfigModule, DeviceRepositoryModule],
|
||||||
controllers: [GroupController],
|
controllers: [GroupController],
|
||||||
@ -55,6 +57,8 @@ import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
|||||||
OccupancyService,
|
OccupancyService,
|
||||||
CommunityRepository,
|
CommunityRepository,
|
||||||
AqiDataService,
|
AqiDataService,
|
||||||
|
PresenceSensorDailySpaceRepository,
|
||||||
|
AqiSpaceDailyPollutantStatsRepository,
|
||||||
],
|
],
|
||||||
exports: [GroupService],
|
exports: [GroupService],
|
||||||
})
|
})
|
||||||
|
@ -2,9 +2,12 @@ import { ApiProperty } from '@nestjs/swagger';
|
|||||||
import {
|
import {
|
||||||
ArrayMinSize,
|
ArrayMinSize,
|
||||||
IsArray,
|
IsArray,
|
||||||
|
IsDate,
|
||||||
IsNotEmpty,
|
IsNotEmpty,
|
||||||
IsOptional,
|
IsOptional,
|
||||||
IsString,
|
IsString,
|
||||||
|
IsUUID,
|
||||||
|
MinDate,
|
||||||
} from 'class-validator';
|
} from 'class-validator';
|
||||||
|
|
||||||
export class AddUserInvitationDto {
|
export class AddUserInvitationDto {
|
||||||
@ -15,7 +18,7 @@ export class AddUserInvitationDto {
|
|||||||
})
|
})
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
public firstName: string;
|
firstName: string;
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'The last name of the user',
|
description: 'The last name of the user',
|
||||||
@ -24,7 +27,7 @@ export class AddUserInvitationDto {
|
|||||||
})
|
})
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
public lastName: string;
|
lastName: string;
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'The email of the user',
|
description: 'The email of the user',
|
||||||
@ -33,7 +36,7 @@ export class AddUserInvitationDto {
|
|||||||
})
|
})
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
public email: string;
|
email: string;
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'The job title of the user',
|
description: 'The job title of the user',
|
||||||
@ -42,7 +45,36 @@ export class AddUserInvitationDto {
|
|||||||
})
|
})
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
public jobTitle?: string;
|
jobTitle?: string;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'The company name of the user',
|
||||||
|
example: 'Tech Corp',
|
||||||
|
required: false,
|
||||||
|
})
|
||||||
|
@IsString()
|
||||||
|
@IsOptional()
|
||||||
|
companyName?: string;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'Access start date',
|
||||||
|
example: new Date(),
|
||||||
|
required: false,
|
||||||
|
})
|
||||||
|
@IsDate()
|
||||||
|
@MinDate(new Date())
|
||||||
|
@IsOptional()
|
||||||
|
accessStartDate?: Date;
|
||||||
|
|
||||||
|
@ApiProperty({
|
||||||
|
description: 'Access start date',
|
||||||
|
example: new Date(),
|
||||||
|
required: false,
|
||||||
|
})
|
||||||
|
@IsDate()
|
||||||
|
@MinDate(new Date())
|
||||||
|
@IsOptional()
|
||||||
|
accessEndDate?: Date;
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'The phone number of the user',
|
description: 'The phone number of the user',
|
||||||
@ -51,32 +83,34 @@ export class AddUserInvitationDto {
|
|||||||
})
|
})
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
public phoneNumber?: string;
|
phoneNumber?: string;
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'The role uuid of the user',
|
description: 'The role uuid of the user',
|
||||||
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',
|
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',
|
||||||
required: true,
|
required: true,
|
||||||
})
|
})
|
||||||
@IsString()
|
@IsUUID('4')
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
public roleUuid: string;
|
roleUuid: string;
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'The project uuid of the user',
|
description: 'The project uuid of the user',
|
||||||
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',
|
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',
|
||||||
required: true,
|
required: true,
|
||||||
})
|
})
|
||||||
@IsString()
|
@IsUUID('4')
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
public projectUuid: string;
|
projectUuid: string;
|
||||||
|
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'The array of space UUIDs (at least one required)',
|
description: 'The array of space UUIDs (at least one required)',
|
||||||
example: ['b5f3c9d2-58b7-4377-b3f7-60acb711d5d9'],
|
example: ['b5f3c9d2-58b7-4377-b3f7-60acb711d5d9'],
|
||||||
required: true,
|
required: true,
|
||||||
})
|
})
|
||||||
@IsArray()
|
@IsArray()
|
||||||
|
@IsUUID('4', { each: true })
|
||||||
@ArrayMinSize(1)
|
@ArrayMinSize(1)
|
||||||
public spaceUuids: string[];
|
spaceUuids: string[];
|
||||||
constructor(dto: Partial<AddUserInvitationDto>) {
|
constructor(dto: Partial<AddUserInvitationDto>) {
|
||||||
Object.assign(this, dto);
|
Object.assign(this, dto);
|
||||||
}
|
}
|
||||||
|
@ -1,78 +1,10 @@
|
|||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiProperty, OmitType } from '@nestjs/swagger';
|
||||||
import {
|
import { IsBoolean, IsNotEmpty, IsString } from 'class-validator';
|
||||||
ArrayMinSize,
|
import { AddUserInvitationDto } from './add.invite-user.dto';
|
||||||
IsArray,
|
|
||||||
IsBoolean,
|
|
||||||
IsNotEmpty,
|
|
||||||
IsOptional,
|
|
||||||
IsString,
|
|
||||||
} from 'class-validator';
|
|
||||||
|
|
||||||
export class UpdateUserInvitationDto {
|
export class UpdateUserInvitationDto extends OmitType(AddUserInvitationDto, [
|
||||||
@ApiProperty({
|
'email',
|
||||||
description: 'The first name of the user',
|
]) {}
|
||||||
example: 'John',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsString()
|
|
||||||
@IsNotEmpty()
|
|
||||||
public firstName: string;
|
|
||||||
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'The last name of the user',
|
|
||||||
example: 'Doe',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsString()
|
|
||||||
@IsNotEmpty()
|
|
||||||
public lastName: string;
|
|
||||||
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'The job title of the user',
|
|
||||||
example: 'Software Engineer',
|
|
||||||
required: false,
|
|
||||||
})
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
public jobTitle?: string;
|
|
||||||
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'The phone number of the user',
|
|
||||||
example: '+1234567890',
|
|
||||||
required: false,
|
|
||||||
})
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
public phoneNumber?: string;
|
|
||||||
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'The role uuid of the user',
|
|
||||||
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsString()
|
|
||||||
@IsNotEmpty()
|
|
||||||
public roleUuid: string;
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'The project uuid of the user',
|
|
||||||
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsString()
|
|
||||||
@IsNotEmpty()
|
|
||||||
public projectUuid: string;
|
|
||||||
@ApiProperty({
|
|
||||||
description: 'The array of space UUIDs (at least one required)',
|
|
||||||
example: ['b5f3c9d2-58b7-4377-b3f7-60acb711d5d9'],
|
|
||||||
required: true,
|
|
||||||
})
|
|
||||||
@IsArray()
|
|
||||||
@ArrayMinSize(1)
|
|
||||||
public spaceUuids: string[];
|
|
||||||
constructor(dto: Partial<UpdateUserInvitationDto>) {
|
|
||||||
Object.assign(this, dto);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
export class DisableUserInvitationDto {
|
export class DisableUserInvitationDto {
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
description: 'The disable status of the user',
|
description: 'The disable status of the user',
|
||||||
|
@ -9,6 +9,7 @@ import { OccupancyService } from '@app/common/helper/services/occupancy.service'
|
|||||||
import { PowerClampService } from '@app/common/helper/services/power.clamp.service';
|
import { PowerClampService } from '@app/common/helper/services/power.clamp.service';
|
||||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||||
|
import { AqiSpaceDailyPollutantStatsRepository } from '@app/common/modules/aqi/repositories';
|
||||||
import { AutomationRepository } from '@app/common/modules/automation/repositories';
|
import { AutomationRepository } from '@app/common/modules/automation/repositories';
|
||||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||||
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
|
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
|
||||||
@ -27,6 +28,7 @@ import {
|
|||||||
PowerClampHourlyRepository,
|
PowerClampHourlyRepository,
|
||||||
PowerClampMonthlyRepository,
|
PowerClampMonthlyRepository,
|
||||||
} from '@app/common/modules/power-clamp/repositories';
|
} from '@app/common/modules/power-clamp/repositories';
|
||||||
|
import { PresenceSensorDailySpaceRepository } from '@app/common/modules/presence-sensor/repositories';
|
||||||
import { ProductRepository } from '@app/common/modules/product/repositories';
|
import { ProductRepository } from '@app/common/modules/product/repositories';
|
||||||
import { ProjectRepository } from '@app/common/modules/project/repositiories';
|
import { ProjectRepository } from '@app/common/modules/project/repositiories';
|
||||||
import { RegionRepository } from '@app/common/modules/region/repositories';
|
import { RegionRepository } from '@app/common/modules/region/repositories';
|
||||||
@ -38,7 +40,6 @@ import {
|
|||||||
} from '@app/common/modules/scene/repositories';
|
} from '@app/common/modules/scene/repositories';
|
||||||
import {
|
import {
|
||||||
InviteSpaceRepository,
|
InviteSpaceRepository,
|
||||||
SpaceLinkRepository,
|
|
||||||
SpaceProductAllocationRepository,
|
SpaceProductAllocationRepository,
|
||||||
SpaceRepository,
|
SpaceRepository,
|
||||||
} from '@app/common/modules/space';
|
} from '@app/common/modules/space';
|
||||||
@ -58,7 +59,7 @@ import {
|
|||||||
UserRepository,
|
UserRepository,
|
||||||
UserSpaceRepository,
|
UserSpaceRepository,
|
||||||
} from '@app/common/modules/user/repositories';
|
} from '@app/common/modules/user/repositories';
|
||||||
import { EmailService } from '@app/common/util/email.service';
|
import { EmailService } from '@app/common/util/email/email.service';
|
||||||
import { CommunityModule } from 'src/community/community.module';
|
import { CommunityModule } from 'src/community/community.module';
|
||||||
import { CommunityService } from 'src/community/services';
|
import { CommunityService } from 'src/community/services';
|
||||||
import { DeviceService } from 'src/device/services';
|
import { DeviceService } from 'src/device/services';
|
||||||
@ -71,7 +72,6 @@ import {
|
|||||||
import { SpaceModelProductAllocationService } from 'src/space-model/services/space-model-product-allocation.service';
|
import { SpaceModelProductAllocationService } from 'src/space-model/services/space-model-product-allocation.service';
|
||||||
import { SubspaceModelProductAllocationService } from 'src/space-model/services/subspace/subspace-model-product-allocation.service';
|
import { SubspaceModelProductAllocationService } from 'src/space-model/services/subspace/subspace-model-product-allocation.service';
|
||||||
import {
|
import {
|
||||||
SpaceLinkService,
|
|
||||||
SpaceService,
|
SpaceService,
|
||||||
SpaceUserService,
|
SpaceUserService,
|
||||||
SubspaceDeviceService,
|
SubspaceDeviceService,
|
||||||
@ -115,13 +115,11 @@ import { UserService, UserSpaceService } from 'src/users/services';
|
|||||||
TimeZoneRepository,
|
TimeZoneRepository,
|
||||||
SpaceService,
|
SpaceService,
|
||||||
InviteSpaceRepository,
|
InviteSpaceRepository,
|
||||||
SpaceLinkService,
|
|
||||||
SubSpaceService,
|
SubSpaceService,
|
||||||
ValidationService,
|
ValidationService,
|
||||||
NewTagService,
|
NewTagService,
|
||||||
SpaceModelService,
|
SpaceModelService,
|
||||||
SpaceProductAllocationService,
|
SpaceProductAllocationService,
|
||||||
SpaceLinkRepository,
|
|
||||||
SubspaceRepository,
|
SubspaceRepository,
|
||||||
SubspaceDeviceService,
|
SubspaceDeviceService,
|
||||||
SubspaceProductAllocationService,
|
SubspaceProductAllocationService,
|
||||||
@ -152,6 +150,8 @@ import { UserService, UserSpaceService } from 'src/users/services';
|
|||||||
SqlLoaderService,
|
SqlLoaderService,
|
||||||
OccupancyService,
|
OccupancyService,
|
||||||
AqiDataService,
|
AqiDataService,
|
||||||
|
PresenceSensorDailySpaceRepository,
|
||||||
|
AqiSpaceDailyPollutantStatsRepository,
|
||||||
],
|
],
|
||||||
exports: [InviteUserService],
|
exports: [InviteUserService],
|
||||||
})
|
})
|
||||||
|
File diff suppressed because it is too large
Load Diff
32
src/main.ts
32
src/main.ts
@ -1,15 +1,13 @@
|
|||||||
import { NestFactory } from '@nestjs/core';
|
|
||||||
import { AppModule } from './app.module';
|
|
||||||
import rateLimit from 'express-rate-limit';
|
|
||||||
import helmet from 'helmet';
|
|
||||||
import { setupSwaggerAuthentication } from '../libs/common/src/util/user-auth.swagger.utils';
|
|
||||||
import { ValidationPipe } from '@nestjs/common';
|
|
||||||
import { json, urlencoded } from 'body-parser';
|
|
||||||
import { SeederService } from '@app/common/seed/services/seeder.service';
|
|
||||||
import { HttpExceptionFilter } from './common/filters/http-exception/http-exception.filter';
|
|
||||||
import { Logger } from '@nestjs/common';
|
|
||||||
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';
|
|
||||||
import { RequestContextMiddleware } from '@app/common/middleware/request-context.middleware';
|
import { RequestContextMiddleware } from '@app/common/middleware/request-context.middleware';
|
||||||
|
import { SeederService } from '@app/common/seed/services/seeder.service';
|
||||||
|
import { Logger, ValidationPipe } from '@nestjs/common';
|
||||||
|
import { NestFactory } from '@nestjs/core';
|
||||||
|
import { json, urlencoded } from 'body-parser';
|
||||||
|
import helmet from 'helmet';
|
||||||
|
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';
|
||||||
|
import { setupSwaggerAuthentication } from '../libs/common/src/util/user-auth.swagger.utils';
|
||||||
|
import { AppModule } from './app.module';
|
||||||
|
import { HttpExceptionFilter } from './common/filters/http-exception/http-exception.filter';
|
||||||
|
|
||||||
async function bootstrap() {
|
async function bootstrap() {
|
||||||
const app = await NestFactory.create(AppModule);
|
const app = await NestFactory.create(AppModule);
|
||||||
@ -23,13 +21,6 @@ async function bootstrap() {
|
|||||||
|
|
||||||
app.use(new RequestContextMiddleware().use);
|
app.use(new RequestContextMiddleware().use);
|
||||||
|
|
||||||
app.use(
|
|
||||||
rateLimit({
|
|
||||||
windowMs: 5 * 60 * 1000,
|
|
||||||
max: 500,
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
app.use(
|
app.use(
|
||||||
helmet({
|
helmet({
|
||||||
contentSecurityPolicy: false,
|
contentSecurityPolicy: false,
|
||||||
@ -57,8 +48,7 @@ async function bootstrap() {
|
|||||||
logger.error('Seeding failed!', error.stack || error);
|
logger.error('Seeding failed!', error.stack || error);
|
||||||
}
|
}
|
||||||
|
|
||||||
const port = process.env.PORT || 3000;
|
logger.log('Starting auth at port ...', process.env.PORT || 4000);
|
||||||
logger.log(`Starting application on port ${port}...`);
|
await app.listen(process.env.PORT || 4000);
|
||||||
await app.listen(port, '0.0.0.0');
|
|
||||||
}
|
}
|
||||||
bootstrap();
|
bootstrap();
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
|
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
|
||||||
|
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
||||||
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
||||||
import { PowerClampService } from '@app/common/helper/services/power.clamp.service';
|
import { PowerClampService } from '@app/common/helper/services/power.clamp.service';
|
||||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||||
@ -21,7 +22,6 @@ import {
|
|||||||
} from '@app/common/modules/scene/repositories';
|
} from '@app/common/modules/scene/repositories';
|
||||||
import {
|
import {
|
||||||
InviteSpaceRepository,
|
InviteSpaceRepository,
|
||||||
SpaceLinkRepository,
|
|
||||||
SpaceProductAllocationRepository,
|
SpaceProductAllocationRepository,
|
||||||
SpaceRepository,
|
SpaceRepository,
|
||||||
} from '@app/common/modules/space';
|
} from '@app/common/modules/space';
|
||||||
@ -49,7 +49,6 @@ import { SpaceModelProductAllocationService } from 'src/space-model/services/spa
|
|||||||
import { SubspaceModelProductAllocationService } from 'src/space-model/services/subspace/subspace-model-product-allocation.service';
|
import { SubspaceModelProductAllocationService } from 'src/space-model/services/subspace/subspace-model-product-allocation.service';
|
||||||
import {
|
import {
|
||||||
SpaceDeviceService,
|
SpaceDeviceService,
|
||||||
SpaceLinkService,
|
|
||||||
SpaceService,
|
SpaceService,
|
||||||
SubspaceDeviceService,
|
SubspaceDeviceService,
|
||||||
SubSpaceService,
|
SubSpaceService,
|
||||||
@ -60,7 +59,8 @@ import { SubspaceProductAllocationService } from 'src/space/services/subspace/su
|
|||||||
import { TagService } from 'src/tags/services';
|
import { TagService } from 'src/tags/services';
|
||||||
import { PowerClampController } from './controllers';
|
import { PowerClampController } from './controllers';
|
||||||
import { PowerClampService as PowerClamp } from './services/power-clamp.service';
|
import { PowerClampService as PowerClamp } from './services/power-clamp.service';
|
||||||
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
import { PresenceSensorDailySpaceRepository } from '@app/common/modules/presence-sensor/repositories';
|
||||||
|
import { AqiSpaceDailyPollutantStatsRepository } from '@app/common/modules/aqi/repositories';
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule],
|
imports: [ConfigModule],
|
||||||
controllers: [PowerClampController],
|
controllers: [PowerClampController],
|
||||||
@ -90,13 +90,11 @@ import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
|||||||
SceneRepository,
|
SceneRepository,
|
||||||
AutomationRepository,
|
AutomationRepository,
|
||||||
InviteSpaceRepository,
|
InviteSpaceRepository,
|
||||||
SpaceLinkService,
|
|
||||||
SubSpaceService,
|
SubSpaceService,
|
||||||
TagService,
|
TagService,
|
||||||
SpaceModelService,
|
SpaceModelService,
|
||||||
SpaceProductAllocationService,
|
SpaceProductAllocationService,
|
||||||
SqlLoaderService,
|
SqlLoaderService,
|
||||||
SpaceLinkRepository,
|
|
||||||
SubspaceRepository,
|
SubspaceRepository,
|
||||||
SubspaceDeviceService,
|
SubspaceDeviceService,
|
||||||
SubspaceProductAllocationService,
|
SubspaceProductAllocationService,
|
||||||
@ -111,6 +109,8 @@ import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
|||||||
SubspaceModelProductAllocationRepoitory,
|
SubspaceModelProductAllocationRepoitory,
|
||||||
OccupancyService,
|
OccupancyService,
|
||||||
AqiDataService,
|
AqiDataService,
|
||||||
|
PresenceSensorDailySpaceRepository,
|
||||||
|
AqiSpaceDailyPollutantStatsRepository,
|
||||||
],
|
],
|
||||||
exports: [PowerClamp],
|
exports: [PowerClamp],
|
||||||
})
|
})
|
||||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user