mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-09 22:57:24 +00:00
Compare commits
77 Commits
DATA-occup
...
ab59a310d9
Author | SHA1 | Date | |
---|---|---|---|
ab59a310d9 | |||
30166810ca | |||
805c5dd180 | |||
e4ba7d46bb | |||
ef21b589c0 | |||
44f83ea54e | |||
e4694db79c | |||
13064296a7 | |||
a269f833bc | |||
fbf62fcd66 | |||
374fb69804 | |||
d4d1ec817d | |||
90ab291d83 | |||
5381a949bc | |||
6973e8b195 | |||
92d102d08f | |||
7dc28d0cb3 | |||
d9ad431a23 | |||
4bf43dab2b | |||
7520b8d9c7 | |||
72753b6dfb | |||
568eef8119 | |||
a91d0f22a4 | |||
0db060ae3f | |||
f2ed04f206 | |||
ea9a65178d | |||
8503ee728d | |||
4f5e1b23f6 | |||
2cb77504ca | |||
c86be27576 | |||
3a08f9f258 | |||
5c96a3b117 | |||
97e14e70f7 | |||
03d44cb14f | |||
0793441e06 | |||
b6321c2530 | |||
b8d34b0d9f | |||
c1065126aa | |||
1742454984 | |||
7eb13088ac | |||
0d6de2df43 | |||
ef2245eae1 | |||
a40560a0b1 | |||
7d6f1bb944 | |||
434316fe51 | |||
287bb4c5e4 | |||
85602fa952 | |||
25a4d3e91b | |||
d3a560d18f | |||
ab99bb5afc | |||
67911d5ff1 | |||
13e3f3e213 | |||
327d678656 | |||
dd5447fc5f | |||
7df5b9ab08 | |||
06b4407b85 | |||
1d6f3b8e65 | |||
80659f7a48 | |||
4a5f2f3b9f | |||
a57f4e1c65 | |||
b2d52c7622 | |||
c9cbb2e085 | |||
8aa3de5fdc | |||
bc1ee9a53b | |||
19356c3833 | |||
8737ee992b | |||
e98a99be73 | |||
93efa15f3c | |||
c305e39ff2 | |||
61e4d220dc | |||
cd4bbe1788 | |||
d770a0c732 | |||
030e6ae902 | |||
9d8287b82b | |||
d741a6c1f3 | |||
6d55704dd4 | |||
d8ad9e55ea |
74
.github/workflows/main_syncrow(staging).yml
vendored
74
.github/workflows/main_syncrow(staging).yml
vendored
@ -1,4 +1,7 @@
|
||||
name: Backend deployment to Azure App Service
|
||||
# Docs for the Azure Web Apps Deploy action: https://github.com/Azure/webapps-deploy
|
||||
# More GitHub Actions for Azure: https://github.com/Azure/actions
|
||||
|
||||
name: Build and deploy container app to Azure Web App - syncrow(staging)
|
||||
|
||||
on:
|
||||
push:
|
||||
@ -6,50 +9,43 @@ on:
|
||||
- main
|
||||
workflow_dispatch:
|
||||
|
||||
env:
|
||||
AZURE_WEB_APP_NAME: 'syncrow'
|
||||
AZURE_WEB_APP_SLOT_NAME: 'staging'
|
||||
ACR_REGISTRY: 'syncrow.azurecr.io'
|
||||
IMAGE_NAME: 'backend'
|
||||
IMAGE_TAG: 'latest'
|
||||
|
||||
jobs:
|
||||
build_and_deploy:
|
||||
runs-on: ubuntu-latest
|
||||
build:
|
||||
runs-on: 'ubuntu-latest'
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v2
|
||||
|
||||
- name: Set up Node.js
|
||||
uses: actions/setup-node@v3
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v2
|
||||
|
||||
- name: Log in to registry
|
||||
uses: docker/login-action@v2
|
||||
with:
|
||||
node-version: '20'
|
||||
registry: https://syncrow.azurecr.io/
|
||||
username: ${{ secrets.AzureAppService_ContainerUsername_47395803300340b49931ea82f6d80be3 }}
|
||||
password: ${{ secrets.AzureAppService_ContainerPassword_e7b0ff54f54d44cba04a970a22384848 }}
|
||||
|
||||
- name: Install dependencies and build project
|
||||
run: |
|
||||
npm install
|
||||
npm run build
|
||||
|
||||
- name: Log in to Azure
|
||||
uses: azure/login@v1
|
||||
- name: Build and push container image to registry
|
||||
uses: docker/build-push-action@v3
|
||||
with:
|
||||
creds: ${{ secrets.AZURE_CREDENTIALS }}
|
||||
push: true
|
||||
tags: syncrow.azurecr.io/${{ secrets.AzureAppService_ContainerUsername_47395803300340b49931ea82f6d80be3 }}/syncrow/backend:${{ github.sha }}
|
||||
file: ./Dockerfile
|
||||
|
||||
- name: Log in to Azure Container Registry
|
||||
run: az acr login --name ${{ env.ACR_REGISTRY }}
|
||||
deploy:
|
||||
runs-on: ubuntu-latest
|
||||
needs: build
|
||||
environment:
|
||||
name: 'staging'
|
||||
url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
|
||||
|
||||
- name: List build output
|
||||
run: ls -R dist/
|
||||
|
||||
- name: Build and push Docker image
|
||||
run: |
|
||||
docker build . -t ${{ env.ACR_REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }}
|
||||
docker push ${{ env.ACR_REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }}
|
||||
|
||||
- name: Set Web App with Docker container
|
||||
run: |
|
||||
az webapp config container set \
|
||||
--name ${{ env.AZURE_WEB_APP_NAME }} \
|
||||
--resource-group backend \
|
||||
--docker-custom-image-name ${{ env.ACR_REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }} \
|
||||
--docker-registry-server-url https://${{ env.ACR_REGISTRY }}
|
||||
steps:
|
||||
- name: Deploy to Azure Web App
|
||||
id: deploy-to-webapp
|
||||
uses: azure/webapps-deploy@v2
|
||||
with:
|
||||
app-name: 'syncrow'
|
||||
slot-name: 'staging'
|
||||
publish-profile: ${{ secrets.AzureAppService_PublishProfile_44f7766441ec4796b74789e9761ef589 }}
|
||||
images: 'syncrow.azurecr.io/${{ secrets.AzureAppService_ContainerUsername_47395803300340b49931ea82f6d80be3 }}/syncrow/backend:${{ github.sha }}'
|
||||
|
73
.github/workflows/main_syncrow(stg).yml
vendored
Normal file
73
.github/workflows/main_syncrow(stg).yml
vendored
Normal file
@ -0,0 +1,73 @@
|
||||
# Docs for the Azure Web Apps Deploy action: https://github.com/Azure/webapps-deploy
|
||||
# More GitHub Actions for Azure: https://github.com/Azure/actions
|
||||
|
||||
name: Build and deploy Node.js app to Azure Web App - syncrow
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: read #This is required for actions/checkout
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Node.js version
|
||||
uses: actions/setup-node@v3
|
||||
with:
|
||||
node-version: '20.x'
|
||||
|
||||
- name: npm install, build, and test
|
||||
run: |
|
||||
npm install
|
||||
npm run build --if-present
|
||||
npm run test --if-present
|
||||
|
||||
- name: Zip artifact for deployment
|
||||
run: zip release.zip ./* -r
|
||||
|
||||
- name: Upload artifact for deployment job
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: node-app
|
||||
path: release.zip
|
||||
|
||||
deploy:
|
||||
runs-on: ubuntu-latest
|
||||
needs: build
|
||||
environment:
|
||||
name: 'stg'
|
||||
url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
|
||||
permissions:
|
||||
id-token: write #This is required for requesting the JWT
|
||||
contents: read #This is required for actions/checkout
|
||||
|
||||
steps:
|
||||
- name: Download artifact from build job
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
name: node-app
|
||||
|
||||
- name: Unzip artifact for deployment
|
||||
run: unzip release.zip
|
||||
|
||||
- name: Login to Azure
|
||||
uses: azure/login@v2
|
||||
with:
|
||||
client-id: ${{ secrets.AZUREAPPSERVICE_CLIENTID_515C8E782CFF431AB20448C85CA0FE58 }}
|
||||
tenant-id: ${{ secrets.AZUREAPPSERVICE_TENANTID_2AEFE5534424490387C08FAE41573CC2 }}
|
||||
subscription-id: ${{ secrets.AZUREAPPSERVICE_SUBSCRIPTIONID_00623C33023749FEA5F6BC36884F9C8A }}
|
||||
|
||||
- name: 'Deploy to Azure Web App'
|
||||
id: deploy-to-webapp
|
||||
uses: azure/webapps-deploy@v3
|
||||
with:
|
||||
app-name: 'syncrow'
|
||||
slot-name: 'stg'
|
||||
package: .
|
7
.gitignore
vendored
7
.gitignore
vendored
@ -58,4 +58,9 @@ pids
|
||||
# Diagnostic reports (https://nodejs.org/api/report.html)
|
||||
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,16 +1,28 @@
|
||||
FROM node:20-alpine
|
||||
FROM --platform=linux/amd64 node:20-alpine
|
||||
|
||||
# curl for health checks
|
||||
RUN apk add --no-cache curl
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
|
||||
COPY package*.json ./
|
||||
|
||||
RUN npm install
|
||||
RUN npm install -g @nestjs/cli
|
||||
RUN npm install --production --ignore-scripts
|
||||
|
||||
COPY . .
|
||||
|
||||
RUN npm run build
|
||||
|
||||
EXPOSE 4000
|
||||
RUN addgroup -g 1001 -S nodejs
|
||||
RUN adduser -S nestjs -u 1001
|
||||
|
||||
CMD ["npm", "run", "start"]
|
||||
RUN chown -R nestjs:nodejs /app
|
||||
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"]
|
||||
|
119
GITHUB_SETUP.md
Normal file
119
GITHUB_SETUP.md
Normal file
@ -0,0 +1,119 @@
|
||||
# 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.syncrow.me
|
||||
|
||||
## 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
|
26
README.md
26
README.md
@ -107,3 +107,29 @@ $ npm run test:cov
|
||||
| | 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.syncrow.me
|
||||
• Health check: https://api.syncrow.me/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
Normal file
46
build.sh
Normal file
@ -0,0 +1,46 @@
|
||||
#!/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."
|
25
cdk.context.json
Normal file
25
cdk.context.json
Normal file
@ -0,0 +1,25 @@
|
||||
{
|
||||
"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."
|
||||
}
|
||||
}
|
58
cdk.json
Normal file
58
cdk.json
Normal file
@ -0,0 +1,58 @@
|
||||
{
|
||||
"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
Executable file
22
deploy.sh
Executable file
@ -0,0 +1,22 @@
|
||||
#!/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
|
16
infrastructure/app.ts
Normal file
16
infrastructure/app.ts
Normal file
@ -0,0 +1,16 @@
|
||||
#!/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/bea1e2ae-84a1-414e-8dbf-4599397e7ed0',
|
||||
});
|
393
infrastructure/stack.ts
Normal file
393
infrastructure/stack.ts
Normal file
@ -0,0 +1,393 @@
|
||||
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.syncrow.me',
|
||||
validation: acm.CertificateValidation.fromDns(),
|
||||
});
|
||||
|
||||
// ECS Fargate Service with Application Load Balancer
|
||||
const fargateService =
|
||||
new ecsPatterns.ApplicationLoadBalancedFargateService(
|
||||
this,
|
||||
'SyncrowBackendService',
|
||||
{
|
||||
cluster,
|
||||
memoryLimitMiB: 1024,
|
||||
cpu: 512,
|
||||
desiredCount: 1,
|
||||
domainName: 'api.syncrow.me',
|
||||
domainZone: route53.HostedZone.fromLookup(this, 'SyncrowZone', {
|
||||
domainName: 'syncrow.me',
|
||||
}),
|
||||
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.syncrow.me';
|
||||
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`,
|
||||
});
|
||||
}
|
||||
}
|
@ -397,6 +397,11 @@ export class ControllerRoute {
|
||||
public static readonly DELETE_USER_SUMMARY = 'Delete user by UUID';
|
||||
public static readonly DELETE_USER_DESCRIPTION =
|
||||
'This endpoint deletes a user identified by their UUID. Accessible only by users with the Super Admin role.';
|
||||
|
||||
public static readonly DELETE_USER_PROFILE_SUMMARY =
|
||||
'Delete user profile by UUID';
|
||||
public static readonly DELETE_USER_PROFILE_DESCRIPTION =
|
||||
'This endpoint deletes a user profile identified by their UUID. Accessible only by users with the Super Admin role.';
|
||||
public static readonly UPDATE_USER_WEB_AGREEMENT_SUMMARY =
|
||||
'Update user web agreement by user UUID';
|
||||
public static readonly UPDATE_USER_WEB_AGREEMENT_DESCRIPTION =
|
||||
@ -501,7 +506,6 @@ export class ControllerRoute {
|
||||
};
|
||||
static PowerClamp = class {
|
||||
public static readonly ROUTE = 'power-clamp';
|
||||
|
||||
static ACTIONS = class {
|
||||
public static readonly GET_ENERGY_SUMMARY =
|
||||
'Get power clamp historical data';
|
||||
@ -524,6 +528,20 @@ export class ControllerRoute {
|
||||
'This endpoint retrieves the occupancy heat map data based on the provided parameters.';
|
||||
};
|
||||
};
|
||||
static AQI = class {
|
||||
public static readonly ROUTE = 'aqi';
|
||||
|
||||
static ACTIONS = class {
|
||||
public static readonly GET_AQI_RANGE_DATA_SUMMARY = 'Get AQI range data';
|
||||
public static readonly GET_AQI_RANGE_DATA_DESCRIPTION =
|
||||
'This endpoint retrieves the AQI (Air Quality Index) range data based on the provided parameters.';
|
||||
|
||||
public static readonly GET_AQI_DISTRIBUTION_DATA_SUMMARY =
|
||||
'Get AQI distribution data';
|
||||
public static readonly GET_AQI_DISTRIBUTION_DATA_DESCRIPTION =
|
||||
'This endpoint retrieves the AQI (Air Quality Index) distribution data based on the provided parameters.';
|
||||
};
|
||||
};
|
||||
static DEVICE = class {
|
||||
public static readonly ROUTE = 'devices';
|
||||
|
||||
|
8
libs/common/src/constants/pollutants.enum.ts
Normal file
8
libs/common/src/constants/pollutants.enum.ts
Normal file
@ -0,0 +1,8 @@
|
||||
export enum PollutantType {
|
||||
AQI = 'aqi',
|
||||
PM25 = 'pm25',
|
||||
PM10 = 'pm10',
|
||||
VOC = 'voc',
|
||||
CO2 = 'co2',
|
||||
CH2O = 'ch2o',
|
||||
}
|
@ -15,8 +15,10 @@ export enum ProductType {
|
||||
WL = 'WL',
|
||||
GD = 'GD',
|
||||
CUR = 'CUR',
|
||||
CUR_2 = 'CUR_2',
|
||||
PC = 'PC',
|
||||
FOUR_S = '4S',
|
||||
SIX_S = '6S',
|
||||
SOS = 'SOS',
|
||||
AQI = 'AQI',
|
||||
}
|
||||
|
@ -1,51 +1,30 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { SnakeNamingStrategy } from './strategies';
|
||||
import { UserEntity } from '../modules/user/entities/user.entity';
|
||||
import { UserSessionEntity } from '../modules/session/entities/session.entity';
|
||||
import { UserOtpEntity } from '../modules/user/entities';
|
||||
import { ProductEntity } from '../modules/product/entities';
|
||||
import { DeviceEntity } from '../modules/device/entities';
|
||||
import { PermissionTypeEntity } from '../modules/permission/entities';
|
||||
import { ProductEntity } from '../modules/product/entities';
|
||||
import { UserSessionEntity } from '../modules/session/entities/session.entity';
|
||||
import { UserOtpEntity } from '../modules/user/entities';
|
||||
import { UserEntity } from '../modules/user/entities/user.entity';
|
||||
import { SnakeNamingStrategy } from './strategies';
|
||||
|
||||
import { UserSpaceEntity } from '../modules/user/entities';
|
||||
import { DeviceUserPermissionEntity } from '../modules/device/entities';
|
||||
import { RoleTypeEntity } from '../modules/role-type/entities';
|
||||
import { UserNotificationEntity } from '../modules/user/entities';
|
||||
import { DeviceNotificationEntity } from '../modules/device/entities';
|
||||
import { RegionEntity } from '../modules/region/entities';
|
||||
import { TimeZoneEntity } from '../modules/timezone/entities';
|
||||
import { VisitorPasswordEntity } from '../modules/visitor-password/entities';
|
||||
import { TypeOrmWinstonLogger } from '@app/common/logger/services/typeorm.logger';
|
||||
import { createLogger } from 'winston';
|
||||
import { winstonLoggerOptions } from '../logger/services/winston.logger';
|
||||
import { AqiSpaceDailyPollutantStatsEntity } from '../modules/aqi/entities';
|
||||
import { AutomationEntity } from '../modules/automation/entities';
|
||||
import { ClientEntity } from '../modules/client/entities';
|
||||
import { CommunityEntity } from '../modules/community/entities';
|
||||
import { DeviceStatusLogEntity } from '../modules/device-status-log/entities';
|
||||
import { SceneEntity, SceneIconEntity } from '../modules/scene/entities';
|
||||
import { SceneDeviceEntity } from '../modules/scene-device/entities';
|
||||
import { ProjectEntity } from '../modules/project/entities';
|
||||
import {
|
||||
SpaceModelEntity,
|
||||
SubspaceModelEntity,
|
||||
TagModel,
|
||||
SpaceModelProductAllocationEntity,
|
||||
SubspaceModelProductAllocationEntity,
|
||||
} from '../modules/space-model/entities';
|
||||
DeviceNotificationEntity,
|
||||
DeviceUserPermissionEntity,
|
||||
} from '../modules/device/entities';
|
||||
import {
|
||||
InviteUserEntity,
|
||||
InviteUserSpaceEntity,
|
||||
} from '../modules/Invite-user/entities';
|
||||
import { InviteSpaceEntity } from '../modules/space/entities/invite-space.entity';
|
||||
import { AutomationEntity } from '../modules/automation/entities';
|
||||
import { SpaceProductAllocationEntity } from '../modules/space/entities/space-product-allocation.entity';
|
||||
import { NewTagEntity } from '../modules/tag/entities/tag.entity';
|
||||
import { SpaceEntity } from '../modules/space/entities/space.entity';
|
||||
import { SpaceLinkEntity } from '../modules/space/entities/space-link.entity';
|
||||
import { SubspaceProductAllocationEntity } from '../modules/space/entities/subspace/subspace-product-allocation.entity';
|
||||
import { SubspaceEntity } from '../modules/space/entities/subspace/subspace.entity';
|
||||
import { TagEntity } from '../modules/space/entities/tag.entity';
|
||||
import { ClientEntity } from '../modules/client/entities';
|
||||
import { TypeOrmWinstonLogger } from '@app/common/logger/services/typeorm.logger';
|
||||
import { createLogger } from 'winston';
|
||||
import { winstonLoggerOptions } from '../logger/services/winston.logger';
|
||||
import {
|
||||
PowerClampDailyEntity,
|
||||
PowerClampHourlyEntity,
|
||||
@ -55,6 +34,31 @@ import {
|
||||
PresenceSensorDailyDeviceEntity,
|
||||
PresenceSensorDailySpaceEntity,
|
||||
} from '../modules/presence-sensor/entities';
|
||||
import { ProjectEntity } from '../modules/project/entities';
|
||||
import { RegionEntity } from '../modules/region/entities';
|
||||
import { RoleTypeEntity } from '../modules/role-type/entities';
|
||||
import { SceneDeviceEntity } from '../modules/scene-device/entities';
|
||||
import { SceneEntity, SceneIconEntity } from '../modules/scene/entities';
|
||||
import {
|
||||
SpaceModelEntity,
|
||||
SpaceModelProductAllocationEntity,
|
||||
SubspaceModelEntity,
|
||||
SubspaceModelProductAllocationEntity,
|
||||
} from '../modules/space-model/entities';
|
||||
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 { SpaceEntity } from '../modules/space/entities/space.entity';
|
||||
import { SubspaceProductAllocationEntity } from '../modules/space/entities/subspace/subspace-product-allocation.entity';
|
||||
import { SubspaceEntity } from '../modules/space/entities/subspace/subspace.entity';
|
||||
import { NewTagEntity } from '../modules/tag/entities/tag.entity';
|
||||
import { TimeZoneEntity } from '../modules/timezone/entities';
|
||||
import {
|
||||
UserNotificationEntity,
|
||||
UserSpaceEntity,
|
||||
} from '../modules/user/entities';
|
||||
import { VisitorPasswordEntity } from '../modules/visitor-password/entities';
|
||||
import { SpaceDailyOccupancyDurationEntity } from '../modules/occupancy/entities';
|
||||
@Module({
|
||||
imports: [
|
||||
TypeOrmModule.forRootAsync({
|
||||
@ -85,7 +89,6 @@ import {
|
||||
SpaceEntity,
|
||||
SpaceLinkEntity,
|
||||
SubspaceEntity,
|
||||
TagEntity,
|
||||
UserSpaceEntity,
|
||||
DeviceUserPermissionEntity,
|
||||
RoleTypeEntity,
|
||||
@ -100,7 +103,6 @@ import {
|
||||
SceneDeviceEntity,
|
||||
SpaceModelEntity,
|
||||
SubspaceModelEntity,
|
||||
TagModel,
|
||||
InviteUserEntity,
|
||||
InviteUserSpaceEntity,
|
||||
InviteSpaceEntity,
|
||||
@ -115,6 +117,8 @@ import {
|
||||
PowerClampMonthlyEntity,
|
||||
PresenceSensorDailyDeviceEntity,
|
||||
PresenceSensorDailySpaceEntity,
|
||||
AqiSpaceDailyPollutantStatsEntity,
|
||||
SpaceDailyOccupancyDurationEntity,
|
||||
],
|
||||
namingStrategy: new SnakeNamingStrategy(),
|
||||
synchronize: Boolean(JSON.parse(configService.get('DB_SYNC'))),
|
||||
|
@ -11,6 +11,7 @@ import {
|
||||
} 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({
|
||||
providers: [
|
||||
@ -23,6 +24,7 @@ import { OccupancyService } from '@app/common/helper/services/occupancy.service'
|
||||
PowerClampMonthlyRepository,
|
||||
SqlLoaderService,
|
||||
OccupancyService,
|
||||
AqiDataService,
|
||||
],
|
||||
controllers: [DeviceStatusFirebaseController],
|
||||
exports: [DeviceStatusFirebaseService, DeviceStatusLogRepository],
|
||||
|
@ -13,6 +13,7 @@ class StatusDto {
|
||||
|
||||
@IsNotEmpty()
|
||||
value: any;
|
||||
t?: string | number | Date;
|
||||
}
|
||||
|
||||
export class AddDeviceStatusDto {
|
||||
|
@ -23,15 +23,19 @@ import { PowerClampService } from '@app/common/helper/services/power.clamp.servi
|
||||
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()
|
||||
export class DeviceStatusFirebaseService {
|
||||
private tuya: TuyaContext;
|
||||
private firebaseDb: Database;
|
||||
private readonly isDevEnv: boolean;
|
||||
|
||||
constructor(
|
||||
private readonly configService: ConfigService,
|
||||
private readonly deviceRepository: DeviceRepository,
|
||||
private readonly powerClampService: PowerClampService,
|
||||
private readonly occupancyService: OccupancyService,
|
||||
private readonly aqiDataService: AqiDataService,
|
||||
private deviceStatusLogRepository: DeviceStatusLogRepository,
|
||||
) {
|
||||
const accessKey = this.configService.get<string>('auth-config.ACCESS_KEY');
|
||||
@ -44,7 +48,14 @@ export class DeviceStatusFirebaseService {
|
||||
});
|
||||
|
||||
// Initialize firebaseDb using firebaseDataBase function
|
||||
this.firebaseDb = firebaseDataBase(this.configService);
|
||||
try {
|
||||
this.firebaseDb = firebaseDataBase(this.configService);
|
||||
} catch (error) {
|
||||
console.warn('Firebase initialization failed, continuing without Firebase:', error.message);
|
||||
this.firebaseDb = null;
|
||||
}
|
||||
this.isDevEnv =
|
||||
this.configService.get<string>('NODE_ENV') === 'development';
|
||||
}
|
||||
async addDeviceStatusByDeviceUuid(
|
||||
deviceTuyaUuid: string,
|
||||
@ -59,7 +70,7 @@ export class DeviceStatusFirebaseService {
|
||||
const deviceStatusSaved = await this.createDeviceStatusFirebase({
|
||||
deviceUuid: device.uuid,
|
||||
deviceTuyaUuid: deviceTuyaUuid,
|
||||
status: deviceStatus.status,
|
||||
status: deviceStatus?.status,
|
||||
productUuid: deviceStatus.productUuid,
|
||||
productType: deviceStatus.productType,
|
||||
});
|
||||
@ -120,7 +131,7 @@ export class DeviceStatusFirebaseService {
|
||||
return {
|
||||
productUuid: deviceDetails.productDevice.uuid,
|
||||
productType: deviceDetails.productDevice.prodType,
|
||||
status: deviceStatus.result[0].status,
|
||||
status: deviceStatus.result[0]?.status,
|
||||
};
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
@ -164,6 +175,14 @@ export class DeviceStatusFirebaseService {
|
||||
async createDeviceStatusFirebase(
|
||||
addDeviceStatusDto: AddDeviceStatusDto,
|
||||
): 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(
|
||||
this.firebaseDb,
|
||||
`device-status/${addDeviceStatusDto.deviceUuid}`,
|
||||
@ -185,18 +204,18 @@ export class DeviceStatusFirebaseService {
|
||||
if (!existingData.productType) {
|
||||
existingData.productType = addDeviceStatusDto.productType;
|
||||
}
|
||||
if (!existingData.status) {
|
||||
if (!existingData?.status) {
|
||||
existingData.status = [];
|
||||
}
|
||||
|
||||
// Create a map to track existing status codes
|
||||
const statusMap = new Map(
|
||||
existingData.status.map((item) => [item.code, item.value]),
|
||||
existingData?.status.map((item) => [item.code, item.value]),
|
||||
);
|
||||
|
||||
// Update or add status codes
|
||||
|
||||
for (const statusItem of addDeviceStatusDto.status) {
|
||||
for (const statusItem of addDeviceStatusDto?.status) {
|
||||
statusMap.set(statusItem.code, statusItem.value);
|
||||
}
|
||||
|
||||
@ -209,62 +228,251 @@ export class DeviceStatusFirebaseService {
|
||||
return existingData;
|
||||
});
|
||||
|
||||
// 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(),
|
||||
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);
|
||||
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,
|
||||
]);
|
||||
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),
|
||||
);
|
||||
const energyStatus = addDeviceStatusDto?.log?.properties?.find(
|
||||
(status) => energyCodes.has(status.code),
|
||||
);
|
||||
|
||||
if (energyStatus) {
|
||||
await this.powerClampService.updateEnergyConsumedHistoricalData(
|
||||
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,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Return the updated data
|
||||
const snapshot: DataSnapshot = await get(dataRef);
|
||||
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,21 +3,32 @@ import { getDatabase } from 'firebase/database';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
|
||||
export const initializeFirebaseApp = (configService: ConfigService) => {
|
||||
const firebaseConfig = {
|
||||
apiKey: configService.get<string>('FIREBASE_API_KEY'),
|
||||
authDomain: configService.get<string>('FIREBASE_AUTH_DOMAIN'),
|
||||
projectId: configService.get<string>('FIREBASE_PROJECT_ID'),
|
||||
storageBucket: configService.get<string>('FIREBASE_STORAGE_BUCKET'),
|
||||
messagingSenderId: configService.get<string>(
|
||||
'FIREBASE_MESSAGING_SENDER_ID',
|
||||
),
|
||||
appId: configService.get<string>('FIREBASE_APP_ID'),
|
||||
measurementId: configService.get<string>('FIREBASE_MEASUREMENT_ID'),
|
||||
databaseURL: configService.get<string>('FIREBASE_DATABASE_URL'),
|
||||
};
|
||||
try {
|
||||
const firebaseConfig = {
|
||||
apiKey: configService.get<string>('FIREBASE_API_KEY'),
|
||||
authDomain: configService.get<string>('FIREBASE_AUTH_DOMAIN'),
|
||||
projectId: configService.get<string>('FIREBASE_PROJECT_ID'),
|
||||
storageBucket: configService.get<string>('FIREBASE_STORAGE_BUCKET'),
|
||||
messagingSenderId: configService.get<string>(
|
||||
'FIREBASE_MESSAGING_SENDER_ID',
|
||||
),
|
||||
appId: configService.get<string>('FIREBASE_APP_ID'),
|
||||
measurementId: configService.get<string>('FIREBASE_MEASUREMENT_ID'),
|
||||
databaseURL: configService.get<string>('FIREBASE_DATABASE_URL'),
|
||||
};
|
||||
|
||||
const app = initializeApp(firebaseConfig);
|
||||
return getDatabase(app);
|
||||
// Check if required Firebase config is available
|
||||
if (!firebaseConfig.projectId || firebaseConfig.projectId === 'placeholder-project') {
|
||||
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) =>
|
||||
|
47
libs/common/src/helper/services/aqi.data.service.ts
Normal file
47
libs/common/src/helper/services/aqi.data.service.ts
Normal file
@ -0,0 +1,47 @@
|
||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { SqlLoaderService } from './sql-loader.service';
|
||||
import { DataSource } from 'typeorm';
|
||||
import { SQL_PROCEDURES_PATH } from '@app/common/constants/sql-query-path';
|
||||
|
||||
@Injectable()
|
||||
export class AqiDataService {
|
||||
constructor(
|
||||
private readonly sqlLoader: SqlLoaderService,
|
||||
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(
|
||||
'fact_daily_space_aqi',
|
||||
'proceduce_update_daily_space_aqi',
|
||||
[dateStr, device.spaceDevice?.uuid],
|
||||
);
|
||||
} catch (err) {
|
||||
console.error('Failed to insert or update aqi data:', err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
private async executeProcedure(
|
||||
procedureFolderName: string,
|
||||
procedureFileName: string,
|
||||
params: (string | number | null)[],
|
||||
): Promise<void> {
|
||||
const query = this.loadQuery(procedureFolderName, procedureFileName);
|
||||
await this.dataSource.query(query, params);
|
||||
console.log(`Procedure ${procedureFileName} executed successfully.`);
|
||||
}
|
||||
|
||||
private loadQuery(folderName: string, fileName: string): string {
|
||||
return this.sqlLoader.loadQuery(folderName, fileName, SQL_PROCEDURES_PATH);
|
||||
}
|
||||
}
|
@ -1,43 +1,26 @@
|
||||
import { utilities as nestWinstonModuleUtilities } from 'nest-winston';
|
||||
import * as winston from 'winston';
|
||||
const environment = process.env.NODE_ENV || 'local';
|
||||
|
||||
export const winstonLoggerOptions: winston.LoggerOptions = {
|
||||
level:
|
||||
environment === 'local'
|
||||
? 'debug'
|
||||
: environment === 'development'
|
||||
? 'warn'
|
||||
: 'error',
|
||||
process.env.AZURE_POSTGRESQL_DATABASE === 'development' ? 'debug' : 'error',
|
||||
transports: [
|
||||
new winston.transports.Console({
|
||||
level:
|
||||
environment === 'local'
|
||||
? 'debug'
|
||||
: environment === 'development'
|
||||
? 'warn'
|
||||
: 'error',
|
||||
format: winston.format.combine(
|
||||
winston.format.timestamp(),
|
||||
nestWinstonModuleUtilities.format.nestLike('MyApp', {
|
||||
prettyPrint: environment === 'local',
|
||||
prettyPrint: true,
|
||||
}),
|
||||
),
|
||||
}),
|
||||
// Only create file logs if NOT local
|
||||
...(environment !== 'local'
|
||||
? [
|
||||
new winston.transports.File({
|
||||
filename: 'logs/error.log',
|
||||
level: 'error',
|
||||
format: winston.format.json(),
|
||||
}),
|
||||
new winston.transports.File({
|
||||
filename: 'logs/combined.log',
|
||||
level: 'info',
|
||||
format: winston.format.json(),
|
||||
}),
|
||||
]
|
||||
: []),
|
||||
new winston.transports.File({
|
||||
filename: 'logs/error.log',
|
||||
level: 'error',
|
||||
format: winston.format.json(),
|
||||
}),
|
||||
new winston.transports.File({
|
||||
filename: 'logs/combined.log',
|
||||
format: winston.format.json(),
|
||||
}),
|
||||
],
|
||||
};
|
||||
|
@ -8,14 +8,14 @@ import {
|
||||
Unique,
|
||||
} from 'typeorm';
|
||||
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import { RoleTypeEntity } from '../../role-type/entities';
|
||||
import { UserStatusEnum } from '@app/common/constants/user-status.enum';
|
||||
import { UserEntity } from '../../user/entities';
|
||||
import { RoleType } from '@app/common/constants/role.type.enum';
|
||||
import { InviteUserDto, InviteUserSpaceDto } from '../dtos';
|
||||
import { UserStatusEnum } from '@app/common/constants/user-status.enum';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import { ProjectEntity } from '../../project/entities';
|
||||
import { RoleTypeEntity } from '../../role-type/entities';
|
||||
import { SpaceEntity } from '../../space/entities/space.entity';
|
||||
import { UserEntity } from '../../user/entities';
|
||||
import { InviteUserDto, InviteUserSpaceDto } from '../dtos';
|
||||
|
||||
@Entity({ name: 'invite-user' })
|
||||
@Unique(['email', 'project'])
|
||||
@ -82,7 +82,10 @@ export class InviteUserEntity extends AbstractEntity<InviteUserDto> {
|
||||
onDelete: 'CASCADE',
|
||||
})
|
||||
public roleType: RoleTypeEntity;
|
||||
@OneToOne(() => UserEntity, (user) => user.inviteUser, { nullable: true })
|
||||
@OneToOne(() => UserEntity, (user) => user.inviteUser, {
|
||||
nullable: true,
|
||||
onDelete: 'CASCADE',
|
||||
})
|
||||
@JoinColumn({ name: 'user_uuid' })
|
||||
user: UserEntity;
|
||||
@OneToMany(
|
||||
@ -112,7 +115,9 @@ export class InviteUserSpaceEntity extends AbstractEntity<InviteUserSpaceDto> {
|
||||
})
|
||||
public uuid: string;
|
||||
|
||||
@ManyToOne(() => InviteUserEntity, (inviteUser) => inviteUser.spaces)
|
||||
@ManyToOne(() => InviteUserEntity, (inviteUser) => inviteUser.spaces, {
|
||||
onDelete: 'CASCADE',
|
||||
})
|
||||
@JoinColumn({ name: 'invite_user_uuid' })
|
||||
public inviteUser: InviteUserEntity;
|
||||
|
||||
|
11
libs/common/src/modules/aqi/aqi.repository.module.ts
Normal file
11
libs/common/src/modules/aqi/aqi.repository.module.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { AqiSpaceDailyPollutantStatsEntity } from './entities/aqi.entity';
|
||||
|
||||
@Module({
|
||||
providers: [],
|
||||
exports: [],
|
||||
controllers: [],
|
||||
imports: [TypeOrmModule.forFeature([AqiSpaceDailyPollutantStatsEntity])],
|
||||
})
|
||||
export class AqiRepositoryModule {}
|
82
libs/common/src/modules/aqi/dtos/aqi.dto.ts
Normal file
82
libs/common/src/modules/aqi/dtos/aqi.dto.ts
Normal file
@ -0,0 +1,82 @@
|
||||
import { IsNotEmpty, IsNumber, IsString } from 'class-validator';
|
||||
|
||||
export class AqiSpaceDailyPollutantStatsDto {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public uuid: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
@IsString()
|
||||
spaceUuid: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
@IsString()
|
||||
eventDay: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
@IsNumber()
|
||||
eventHour: number;
|
||||
|
||||
@IsNumber()
|
||||
pm1Min: number;
|
||||
|
||||
@IsNumber()
|
||||
pm1Avg: number;
|
||||
|
||||
@IsNumber()
|
||||
pm1Max: number;
|
||||
|
||||
@IsNumber()
|
||||
pm10Min: number;
|
||||
|
||||
@IsNumber()
|
||||
pm10Avg: number;
|
||||
|
||||
@IsNumber()
|
||||
pm10Max: number;
|
||||
|
||||
@IsNumber()
|
||||
pm25Min: number;
|
||||
|
||||
@IsNumber()
|
||||
pm25Avg: number;
|
||||
|
||||
@IsNumber()
|
||||
pm25Max: number;
|
||||
|
||||
@IsNumber()
|
||||
ch2oMin: number;
|
||||
|
||||
@IsNumber()
|
||||
ch2oAvg: number;
|
||||
|
||||
@IsNumber()
|
||||
ch2oMax: number;
|
||||
|
||||
@IsNumber()
|
||||
vocMin: number;
|
||||
|
||||
@IsNumber()
|
||||
vocAvg: number;
|
||||
|
||||
@IsNumber()
|
||||
vocMax: number;
|
||||
|
||||
@IsNumber()
|
||||
co2Min: number;
|
||||
|
||||
@IsNumber()
|
||||
co2Avg: number;
|
||||
|
||||
@IsNumber()
|
||||
co2Max: number;
|
||||
|
||||
@IsNumber()
|
||||
aqiMin: number;
|
||||
|
||||
@IsNumber()
|
||||
aqiAvg: number;
|
||||
|
||||
@IsNumber()
|
||||
aqiMax: number;
|
||||
}
|
1
libs/common/src/modules/aqi/dtos/index.ts
Normal file
1
libs/common/src/modules/aqi/dtos/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './aqi.dto';
|
184
libs/common/src/modules/aqi/entities/aqi.entity.ts
Normal file
184
libs/common/src/modules/aqi/entities/aqi.entity.ts
Normal file
@ -0,0 +1,184 @@
|
||||
import { Column, Entity, ManyToOne, Unique } from 'typeorm';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import { SpaceEntity } from '../../space/entities/space.entity';
|
||||
import { AqiSpaceDailyPollutantStatsDto } from '../dtos';
|
||||
|
||||
@Entity({ name: 'space-daily-pollutant-stats' })
|
||||
@Unique(['spaceUuid', 'eventDate'])
|
||||
export class AqiSpaceDailyPollutantStatsEntity extends AbstractEntity<AqiSpaceDailyPollutantStatsDto> {
|
||||
@Column({ nullable: false })
|
||||
public spaceUuid: string;
|
||||
|
||||
@ManyToOne(() => SpaceEntity, (space) => space.aqiSensorDaily)
|
||||
space: SpaceEntity;
|
||||
|
||||
@Column({ type: 'date', nullable: false })
|
||||
public eventDate: Date;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public goodAqiPercentage?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public moderateAqiPercentage?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public unhealthySensitiveAqiPercentage?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public unhealthyAqiPercentage?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public veryUnhealthyAqiPercentage?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public hazardousAqiPercentage?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public dailyAvgAqi?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public dailyMaxAqi?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public dailyMinAqi?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public goodPm25Percentage?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public moderatePm25Percentage?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public unhealthySensitivePm25Percentage?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public unhealthyPm25Percentage?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public veryUnhealthyPm25Percentage?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public hazardousPm25Percentage?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public dailyAvgPm25?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public dailyMaxPm25?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public dailyMinPm25?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public goodPm10Percentage?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public moderatePm10Percentage?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public unhealthySensitivePm10Percentage?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public unhealthyPm10Percentage?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public veryUnhealthyPm10Percentage?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public hazardousPm10Percentage?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public dailyAvgPm10?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public dailyMaxPm10?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public dailyMinPm10?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public goodVocPercentage?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public moderateVocPercentage?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public unhealthySensitiveVocPercentage?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public unhealthyVocPercentage?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public veryUnhealthyVocPercentage?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public hazardousVocPercentage?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public dailyAvgVoc?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public dailyMaxVoc?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public dailyMinVoc?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public goodCo2Percentage?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public moderateCo2Percentage?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public unhealthySensitiveCo2Percentage?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public unhealthyCo2Percentage?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public veryUnhealthyCo2Percentage?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public hazardousCo2Percentage?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public dailyAvgCo2?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public dailyMaxCo2?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public dailyMinCo2?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public goodCh2oPercentage?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public moderateCh2oPercentage?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public unhealthySensitiveCh2oPercentage?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public unhealthyCh2oPercentage?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public veryUnhealthyCh2oPercentage?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public hazardousCh2oPercentage?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public dailyAvgCh2o?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public dailyMaxCh2o?: number;
|
||||
|
||||
@Column('float', { nullable: true })
|
||||
public dailyMinCh2o?: number;
|
||||
|
||||
constructor(partial: Partial<AqiSpaceDailyPollutantStatsEntity>) {
|
||||
super();
|
||||
Object.assign(this, partial);
|
||||
}
|
||||
}
|
1
libs/common/src/modules/aqi/entities/index.ts
Normal file
1
libs/common/src/modules/aqi/entities/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './aqi.entity';
|
10
libs/common/src/modules/aqi/repositories/aqi.repository.ts
Normal file
10
libs/common/src/modules/aqi/repositories/aqi.repository.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import { DataSource, Repository } from 'typeorm';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { AqiSpaceDailyPollutantStatsEntity } from '../entities';
|
||||
|
||||
@Injectable()
|
||||
export class AqiSpaceDailyPollutantStatsRepository extends Repository<AqiSpaceDailyPollutantStatsEntity> {
|
||||
constructor(private dataSource: DataSource) {
|
||||
super(AqiSpaceDailyPollutantStatsEntity, dataSource.createEntityManager());
|
||||
}
|
||||
}
|
1
libs/common/src/modules/aqi/repositories/index.ts
Normal file
1
libs/common/src/modules/aqi/repositories/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './aqi.repository';
|
@ -2,15 +2,15 @@ import { SourceType } from '@app/common/constants/source-type.enum';
|
||||
import { Entity, Column, PrimaryColumn, Unique } from 'typeorm';
|
||||
|
||||
@Entity('device-status-log')
|
||||
@Unique('event_time_idx', ['eventTime'])
|
||||
@Unique('event_time_idx', ['eventTime', 'deviceId', 'code', 'value'])
|
||||
export class DeviceStatusLogEntity {
|
||||
@Column({ type: 'int', generated: true, unsigned: true })
|
||||
@PrimaryColumn({ type: 'int', generated: true, unsigned: true })
|
||||
id: number;
|
||||
|
||||
@Column({ type: 'text' })
|
||||
eventId: string;
|
||||
|
||||
@PrimaryColumn({ type: 'timestamptz' })
|
||||
@Column({ type: 'timestamptz' })
|
||||
eventTime: Date;
|
||||
|
||||
@Column({
|
||||
|
@ -1,24 +1,24 @@
|
||||
import {
|
||||
Column,
|
||||
Entity,
|
||||
Index,
|
||||
JoinColumn,
|
||||
ManyToOne,
|
||||
OneToMany,
|
||||
Unique,
|
||||
Index,
|
||||
JoinColumn,
|
||||
} from 'typeorm';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import { DeviceDto, DeviceUserPermissionDto } from '../dtos/device.dto';
|
||||
import { ProductEntity } from '../../product/entities';
|
||||
import { UserEntity } from '../../user/entities';
|
||||
import { DeviceNotificationDto } from '../dtos';
|
||||
import { PermissionTypeEntity } from '../../permission/entities';
|
||||
import { PowerClampHourlyEntity } from '../../power-clamp/entities/power-clamp.entity';
|
||||
import { PresenceSensorDailyDeviceEntity } from '../../presence-sensor/entities';
|
||||
import { ProductEntity } from '../../product/entities';
|
||||
import { SceneDeviceEntity } from '../../scene-device/entities';
|
||||
import { SpaceEntity } from '../../space/entities/space.entity';
|
||||
import { SubspaceEntity } from '../../space/entities/subspace/subspace.entity';
|
||||
import { NewTagEntity } from '../../tag';
|
||||
import { PowerClampHourlyEntity } from '../../power-clamp/entities/power-clamp.entity';
|
||||
import { PresenceSensorDailyDeviceEntity } from '../../presence-sensor/entities';
|
||||
import { UserEntity } from '../../user/entities';
|
||||
import { DeviceNotificationDto } from '../dtos';
|
||||
import { DeviceDto, DeviceUserPermissionDto } from '../dtos/device.dto';
|
||||
|
||||
@Entity({ name: 'device' })
|
||||
@Unique(['deviceTuyaUuid'])
|
||||
@ -78,8 +78,8 @@ export class DeviceEntity extends AbstractEntity<DeviceDto> {
|
||||
@OneToMany(() => SceneDeviceEntity, (sceneDevice) => sceneDevice.device, {})
|
||||
sceneDevices: SceneDeviceEntity[];
|
||||
|
||||
@OneToMany(() => NewTagEntity, (tag) => tag.devices)
|
||||
// @JoinTable({ name: 'device_tags' })
|
||||
@ManyToOne(() => NewTagEntity, (tag) => tag.devices)
|
||||
@JoinColumn({ name: 'tag_uuid' })
|
||||
public tag: NewTagEntity;
|
||||
@OneToMany(() => PowerClampHourlyEntity, (powerClamp) => powerClamp.device)
|
||||
powerClampHourly: PowerClampHourlyEntity[];
|
||||
@ -111,6 +111,7 @@ export class DeviceNotificationEntity extends AbstractEntity<DeviceNotificationD
|
||||
|
||||
@ManyToOne(() => UserEntity, (user) => user.userPermission, {
|
||||
nullable: false,
|
||||
onDelete: 'CASCADE',
|
||||
})
|
||||
user: UserEntity;
|
||||
|
||||
@ -149,6 +150,7 @@ export class DeviceUserPermissionEntity extends AbstractEntity<DeviceUserPermiss
|
||||
|
||||
@ManyToOne(() => UserEntity, (user) => user.userPermission, {
|
||||
nullable: false,
|
||||
onDelete: 'CASCADE',
|
||||
})
|
||||
user: UserEntity;
|
||||
constructor(partial: Partial<DeviceUserPermissionEntity>) {
|
||||
|
1
libs/common/src/modules/occupancy/dtos/index.ts
Normal file
1
libs/common/src/modules/occupancy/dtos/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './occupancy.dto';
|
23
libs/common/src/modules/occupancy/dtos/occupancy.dto.ts
Normal file
23
libs/common/src/modules/occupancy/dtos/occupancy.dto.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import { IsNotEmpty, IsNumber, IsString } from 'class-validator';
|
||||
|
||||
export class SpaceDailyOccupancyDurationDto {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public uuid: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public spaceUuid: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public eventDate: string;
|
||||
|
||||
@IsNumber()
|
||||
@IsNotEmpty()
|
||||
public occupancyPercentage: number;
|
||||
|
||||
@IsNumber()
|
||||
@IsNotEmpty()
|
||||
public occupiedSeconds: number;
|
||||
}
|
1
libs/common/src/modules/occupancy/entities/index.ts
Normal file
1
libs/common/src/modules/occupancy/entities/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './occupancy.entity';
|
@ -0,0 +1,32 @@
|
||||
import { Column, Entity, ManyToOne, Unique } from 'typeorm';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import { SpaceEntity } from '../../space/entities/space.entity';
|
||||
import { SpaceDailyOccupancyDurationDto } from '../dtos';
|
||||
|
||||
@Entity({ name: 'space-daily-occupancy-duration' })
|
||||
@Unique(['spaceUuid', 'eventDate'])
|
||||
export class SpaceDailyOccupancyDurationEntity extends AbstractEntity<SpaceDailyOccupancyDurationDto> {
|
||||
@Column({ nullable: false })
|
||||
public spaceUuid: string;
|
||||
|
||||
@Column({ nullable: false, type: 'date' })
|
||||
public eventDate: string;
|
||||
|
||||
public CountTotalPresenceDetected: number;
|
||||
|
||||
@ManyToOne(() => SpaceEntity, (space) => space.presenceSensorDaily)
|
||||
space: SpaceEntity;
|
||||
|
||||
@Column({ type: 'int' })
|
||||
occupancyPercentage: number;
|
||||
|
||||
@Column({ type: 'int', nullable: true })
|
||||
occupiedSeconds?: number;
|
||||
|
||||
@Column({ type: 'int', nullable: true })
|
||||
deviceCount?: number;
|
||||
constructor(partial: Partial<SpaceDailyOccupancyDurationEntity>) {
|
||||
super();
|
||||
Object.assign(this, partial);
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { SpaceDailyOccupancyDurationEntity } from './entities/occupancy.entity';
|
||||
|
||||
@Module({
|
||||
providers: [],
|
||||
exports: [],
|
||||
controllers: [],
|
||||
imports: [TypeOrmModule.forFeature([SpaceDailyOccupancyDurationEntity])],
|
||||
})
|
||||
export class SpaceDailyOccupancyDurationRepositoryModule {}
|
1
libs/common/src/modules/occupancy/repositories/index.ts
Normal file
1
libs/common/src/modules/occupancy/repositories/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './occupancy.repository';
|
@ -0,0 +1,10 @@
|
||||
import { DataSource, Repository } from 'typeorm';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { SpaceDailyOccupancyDurationEntity } from '../entities/occupancy.entity';
|
||||
|
||||
@Injectable()
|
||||
export class SpaceDailyOccupancyDurationEntityRepository extends Repository<SpaceDailyOccupancyDurationEntity> {
|
||||
constructor(private dataSource: DataSource) {
|
||||
super(SpaceDailyOccupancyDurationEntity, dataSource.createEntityManager());
|
||||
}
|
||||
}
|
@ -1,10 +1,7 @@
|
||||
import { Column, Entity, OneToMany } from 'typeorm';
|
||||
import { ProductDto } from '../dtos';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import { DeviceEntity } from '../../device/entities';
|
||||
import { TagModel } from '../../space-model';
|
||||
import { TagEntity } from '../../space/entities/tag.entity';
|
||||
import { NewTagEntity } from '../../tag/entities';
|
||||
import { ProductDto } from '../dtos';
|
||||
@Entity({ name: 'product' })
|
||||
export class ProductEntity extends AbstractEntity<ProductDto> {
|
||||
@Column({
|
||||
@ -28,15 +25,6 @@ export class ProductEntity extends AbstractEntity<ProductDto> {
|
||||
})
|
||||
public prodType: string;
|
||||
|
||||
@OneToMany(() => NewTagEntity, (tag) => tag.product, { cascade: true })
|
||||
public newTags: NewTagEntity[];
|
||||
|
||||
@OneToMany(() => TagModel, (tag) => tag.product)
|
||||
tagModels: TagModel[];
|
||||
|
||||
@OneToMany(() => TagEntity, (tag) => tag.product)
|
||||
tags: TagEntity[];
|
||||
|
||||
@OneToMany(
|
||||
() => DeviceEntity,
|
||||
(devicesProductEntity) => devicesProductEntity.productDevice,
|
||||
|
@ -1,21 +0,0 @@
|
||||
import { IsNotEmpty, IsString } from 'class-validator';
|
||||
|
||||
export class TagModelDto {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public uuid: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public name: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public productUuid: string;
|
||||
|
||||
@IsString()
|
||||
spaceModelUuid: string;
|
||||
|
||||
@IsString()
|
||||
subspaceModelUuid: string;
|
||||
}
|
@ -1,4 +1,3 @@
|
||||
export * from './space-model-product-allocation.entity';
|
||||
export * from './space-model.entity';
|
||||
export * from './subspace-model';
|
||||
export * from './tag-model.entity';
|
||||
export * from './space-model-product-allocation.entity';
|
||||
|
@ -1,18 +1,12 @@
|
||||
import {
|
||||
Entity,
|
||||
Column,
|
||||
ManyToOne,
|
||||
ManyToMany,
|
||||
JoinTable,
|
||||
OneToMany,
|
||||
} from 'typeorm';
|
||||
import { SpaceModelEntity } from './space-model.entity';
|
||||
import { NewTagEntity } from '../../tag/entities/tag.entity';
|
||||
import { Column, Entity, ManyToOne, OneToMany, Unique } from 'typeorm';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import { ProductEntity } from '../../product/entities/product.entity';
|
||||
import { SpaceProductAllocationEntity } from '../../space/entities/space-product-allocation.entity';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import { NewTagEntity } from '../../tag/entities/tag.entity';
|
||||
import { SpaceModelEntity } from './space-model.entity';
|
||||
|
||||
@Entity({ name: 'space_model_product_allocation' })
|
||||
@Unique(['spaceModel', 'product', 'tag'])
|
||||
export class SpaceModelProductAllocationEntity extends AbstractEntity<SpaceModelProductAllocationEntity> {
|
||||
@Column({
|
||||
type: 'uuid',
|
||||
@ -31,9 +25,8 @@ export class SpaceModelProductAllocationEntity extends AbstractEntity<SpaceModel
|
||||
@ManyToOne(() => ProductEntity, { nullable: false, onDelete: 'CASCADE' })
|
||||
public product: ProductEntity;
|
||||
|
||||
@ManyToMany(() => NewTagEntity, { cascade: true, onDelete: 'CASCADE' })
|
||||
@JoinTable({ name: 'space_model_product_tags' })
|
||||
public tags: NewTagEntity[];
|
||||
@ManyToOne(() => NewTagEntity, { nullable: true, onDelete: 'CASCADE' })
|
||||
public tag: NewTagEntity;
|
||||
|
||||
@OneToMany(
|
||||
() => SpaceProductAllocationEntity,
|
||||
|
@ -1,11 +1,10 @@
|
||||
import { Entity, Column, OneToMany, ManyToOne, JoinColumn } from 'typeorm';
|
||||
import { Column, Entity, JoinColumn, ManyToOne, OneToMany } from 'typeorm';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import { SpaceModelDto } from '../dtos';
|
||||
import { SubspaceModelEntity } from './subspace-model';
|
||||
import { ProjectEntity } from '../../project/entities';
|
||||
import { TagModel } from './tag-model.entity';
|
||||
import { SpaceModelProductAllocationEntity } from './space-model-product-allocation.entity';
|
||||
import { SpaceEntity } from '../../space/entities/space.entity';
|
||||
import { SpaceModelDto } from '../dtos';
|
||||
import { SpaceModelProductAllocationEntity } from './space-model-product-allocation.entity';
|
||||
import { SubspaceModelEntity } from './subspace-model';
|
||||
|
||||
@Entity({ name: 'space-model' })
|
||||
export class SpaceModelEntity extends AbstractEntity<SpaceModelDto> {
|
||||
@ -49,9 +48,6 @@ export class SpaceModelEntity extends AbstractEntity<SpaceModelDto> {
|
||||
})
|
||||
public spaces: SpaceEntity[];
|
||||
|
||||
@OneToMany(() => TagModel, (tag) => tag.spaceModel)
|
||||
tags: TagModel[];
|
||||
|
||||
@OneToMany(
|
||||
() => SpaceModelProductAllocationEntity,
|
||||
(allocation) => allocation.spaceModel,
|
||||
|
@ -1,11 +1,12 @@
|
||||
import { Entity, Column, ManyToOne, ManyToMany, JoinTable } from 'typeorm';
|
||||
import { SubspaceModelEntity } from './subspace-model.entity';
|
||||
import { AbstractEntity } from '@app/common/modules/abstract/entities/abstract.entity';
|
||||
import { ProductEntity } from '@app/common/modules/product/entities/product.entity';
|
||||
import { NewTagEntity } from '@app/common/modules/tag/entities/tag.entity';
|
||||
import { Column, Entity, ManyToOne, Unique } from 'typeorm';
|
||||
import { SubspaceModelProductAllocationDto } from '../../dtos/subspace-model/subspace-model-product-allocation.dto';
|
||||
import { AbstractEntity } from '@app/common/modules/abstract/entities/abstract.entity';
|
||||
import { SubspaceModelEntity } from './subspace-model.entity';
|
||||
|
||||
@Entity({ name: 'subspace_model_product_allocation' })
|
||||
@Unique(['subspaceModel', 'product', 'tag'])
|
||||
export class SubspaceModelProductAllocationEntity extends AbstractEntity<SubspaceModelProductAllocationDto> {
|
||||
@Column({
|
||||
type: 'uuid',
|
||||
@ -27,12 +28,8 @@ export class SubspaceModelProductAllocationEntity extends AbstractEntity<Subspac
|
||||
@ManyToOne(() => ProductEntity, { nullable: false, onDelete: 'CASCADE' })
|
||||
public product: ProductEntity;
|
||||
|
||||
@ManyToMany(() => NewTagEntity, (tag) => tag.subspaceModelAllocations, {
|
||||
cascade: true,
|
||||
onDelete: 'CASCADE',
|
||||
})
|
||||
@JoinTable({ name: 'subspace_model_product_tags' })
|
||||
public tags: NewTagEntity[];
|
||||
@ManyToOne(() => NewTagEntity, { nullable: true, onDelete: 'CASCADE' })
|
||||
public tag: NewTagEntity;
|
||||
|
||||
constructor(partial: Partial<SubspaceModelProductAllocationEntity>) {
|
||||
super();
|
||||
|
@ -1,10 +1,9 @@
|
||||
import { AbstractEntity } from '@app/common/modules/abstract/entities/abstract.entity';
|
||||
import { SubspaceEntity } from '@app/common/modules/space/entities/subspace/subspace.entity';
|
||||
import { Column, Entity, ManyToOne, OneToMany } from 'typeorm';
|
||||
import { SubSpaceModelDto } from '../../dtos';
|
||||
import { SpaceModelEntity } from '../space-model.entity';
|
||||
import { TagModel } from '../tag-model.entity';
|
||||
import { SubspaceModelProductAllocationEntity } from './subspace-model-product-allocation.entity';
|
||||
import { SubspaceEntity } from '@app/common/modules/space/entities/subspace/subspace.entity';
|
||||
|
||||
@Entity({ name: 'subspace-model' })
|
||||
export class SubspaceModelEntity extends AbstractEntity<SubSpaceModelDto> {
|
||||
@ -41,9 +40,6 @@ export class SubspaceModelEntity extends AbstractEntity<SubSpaceModelDto> {
|
||||
})
|
||||
public disabled: boolean;
|
||||
|
||||
@OneToMany(() => TagModel, (tag) => tag.subspaceModel)
|
||||
tags: TagModel[];
|
||||
|
||||
@OneToMany(
|
||||
() => SubspaceModelProductAllocationEntity,
|
||||
(allocation) => allocation.subspaceModel,
|
||||
|
@ -1,38 +0,0 @@
|
||||
import { Column, Entity, JoinColumn, ManyToOne, OneToMany } from 'typeorm';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import { TagModelDto } from '../dtos/tag-model.dto';
|
||||
import { SpaceModelEntity } from './space-model.entity';
|
||||
import { SubspaceModelEntity } from './subspace-model';
|
||||
import { ProductEntity } from '../../product/entities';
|
||||
import { TagEntity } from '../../space/entities/tag.entity';
|
||||
|
||||
@Entity({ name: 'tag_model' })
|
||||
export class TagModel extends AbstractEntity<TagModelDto> {
|
||||
@Column({ type: 'varchar', length: 255 })
|
||||
tag: string;
|
||||
|
||||
@ManyToOne(() => ProductEntity, (product) => product.tagModels, {
|
||||
nullable: false,
|
||||
})
|
||||
@JoinColumn({ name: 'product_id' })
|
||||
product: ProductEntity;
|
||||
|
||||
@ManyToOne(() => SpaceModelEntity, (space) => space.tags, { nullable: true })
|
||||
@JoinColumn({ name: 'space_model_id' })
|
||||
spaceModel: SpaceModelEntity;
|
||||
|
||||
@ManyToOne(() => SubspaceModelEntity, (subspace) => subspace.tags, {
|
||||
nullable: true,
|
||||
})
|
||||
@JoinColumn({ name: 'subspace_model_id' })
|
||||
subspaceModel: SubspaceModelEntity;
|
||||
|
||||
@Column({
|
||||
nullable: false,
|
||||
default: false,
|
||||
})
|
||||
public disabled: boolean;
|
||||
|
||||
@OneToMany(() => TagEntity, (tag) => tag.model)
|
||||
tags: TagEntity[];
|
||||
}
|
@ -1,11 +1,10 @@
|
||||
import { DataSource, Repository } from 'typeorm';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { DataSource, Repository } from 'typeorm';
|
||||
import {
|
||||
SpaceModelEntity,
|
||||
SpaceModelProductAllocationEntity,
|
||||
SubspaceModelEntity,
|
||||
SubspaceModelProductAllocationEntity,
|
||||
TagModel,
|
||||
} from '../entities';
|
||||
|
||||
@Injectable()
|
||||
@ -21,13 +20,6 @@ export class SubspaceModelRepository extends Repository<SubspaceModelEntity> {
|
||||
}
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class TagModelRepository extends Repository<TagModel> {
|
||||
constructor(private dataSource: DataSource) {
|
||||
super(TagModel, dataSource.createEntityManager());
|
||||
}
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class SpaceModelProductAllocationRepoitory extends Repository<SpaceModelProductAllocationEntity> {
|
||||
constructor(private dataSource: DataSource) {
|
||||
|
@ -1,13 +1,11 @@
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { SpaceModelEntity, SubspaceModelEntity, TagModel } from './entities';
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { SpaceModelEntity, SubspaceModelEntity } from './entities';
|
||||
|
||||
@Module({
|
||||
providers: [],
|
||||
exports: [],
|
||||
controllers: [],
|
||||
imports: [
|
||||
TypeOrmModule.forFeature([SpaceModelEntity, SubspaceModelEntity, TagModel]),
|
||||
],
|
||||
imports: [TypeOrmModule.forFeature([SpaceModelEntity, SubspaceModelEntity])],
|
||||
})
|
||||
export class SpaceModelRepositoryModule {}
|
||||
|
@ -1,12 +1,13 @@
|
||||
import { Entity, Column, ManyToOne, ManyToMany, JoinTable } from 'typeorm';
|
||||
import { SpaceEntity } from './space.entity';
|
||||
import { SpaceModelProductAllocationEntity } from '../../space-model/entities/space-model-product-allocation.entity';
|
||||
import { ProductEntity } from '../../product/entities/product.entity';
|
||||
import { NewTagEntity } from '../../tag/entities/tag.entity';
|
||||
import { Column, Entity, ManyToOne, Unique } from 'typeorm';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import { ProductEntity } from '../../product/entities/product.entity';
|
||||
import { SpaceModelProductAllocationEntity } from '../../space-model/entities/space-model-product-allocation.entity';
|
||||
import { NewTagEntity } from '../../tag/entities/tag.entity';
|
||||
import { SpaceProductAllocationDto } from '../dtos/space-product-allocation.dto';
|
||||
import { SpaceEntity } from './space.entity';
|
||||
|
||||
@Entity({ name: 'space_product_allocation' })
|
||||
@Unique(['space', 'product', 'tag'], {})
|
||||
export class SpaceProductAllocationEntity extends AbstractEntity<SpaceProductAllocationDto> {
|
||||
@Column({
|
||||
type: 'uuid',
|
||||
@ -30,9 +31,8 @@ export class SpaceProductAllocationEntity extends AbstractEntity<SpaceProductAll
|
||||
@ManyToOne(() => ProductEntity, { nullable: false, onDelete: 'CASCADE' })
|
||||
public product: ProductEntity;
|
||||
|
||||
@ManyToMany(() => NewTagEntity)
|
||||
@JoinTable({ name: 'space_product_tags' })
|
||||
public tags: NewTagEntity[];
|
||||
@ManyToOne(() => NewTagEntity, { nullable: true, onDelete: 'CASCADE' })
|
||||
public tag: NewTagEntity;
|
||||
|
||||
constructor(partial: Partial<SpaceProductAllocationEntity>) {
|
||||
super();
|
||||
|
@ -11,6 +11,8 @@ import { InviteUserSpaceEntity } from '../../Invite-user/entities';
|
||||
import { SpaceProductAllocationEntity } from './space-product-allocation.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' })
|
||||
export class SpaceEntity extends AbstractEntity<SpaceDto> {
|
||||
@ -115,6 +117,15 @@ export class SpaceEntity extends AbstractEntity<SpaceDto> {
|
||||
@OneToMany(() => PresenceSensorDailySpaceEntity, (sensor) => sensor.space)
|
||||
presenceSensorDaily: PresenceSensorDailySpaceEntity[];
|
||||
|
||||
@OneToMany(() => AqiSpaceDailyPollutantStatsEntity, (aqi) => aqi.space)
|
||||
aqiSensorDaily: AqiSpaceDailyPollutantStatsEntity[];
|
||||
|
||||
@OneToMany(
|
||||
() => SpaceDailyOccupancyDurationEntity,
|
||||
(occupancy) => occupancy.space,
|
||||
)
|
||||
occupancyDaily: SpaceDailyOccupancyDurationEntity[];
|
||||
|
||||
constructor(partial: Partial<SpaceEntity>) {
|
||||
super();
|
||||
Object.assign(this, partial);
|
||||
|
@ -1,20 +1,13 @@
|
||||
import {
|
||||
Entity,
|
||||
Column,
|
||||
ManyToOne,
|
||||
ManyToMany,
|
||||
JoinTable,
|
||||
Unique,
|
||||
} from 'typeorm';
|
||||
import { SubspaceEntity } from './subspace.entity';
|
||||
import { AbstractEntity } from '@app/common/modules/abstract/entities/abstract.entity';
|
||||
import { ProductEntity } from '@app/common/modules/product/entities';
|
||||
import { SubspaceModelProductAllocationEntity } from '@app/common/modules/space-model';
|
||||
import { NewTagEntity } from '@app/common/modules/tag/entities/tag.entity';
|
||||
import { AbstractEntity } from '@app/common/modules/abstract/entities/abstract.entity';
|
||||
import { Column, Entity, ManyToOne, Unique } from 'typeorm';
|
||||
import { SubspaceProductAllocationDto } from '../../dtos/subspace-product-allocation.dto';
|
||||
import { SubspaceEntity } from './subspace.entity';
|
||||
|
||||
@Entity({ name: 'subspace_product_allocation' })
|
||||
// @Unique(['subspace', 'product'])
|
||||
@Unique(['subspace', 'product', 'tag'])
|
||||
export class SubspaceProductAllocationEntity extends AbstractEntity<SubspaceProductAllocationDto> {
|
||||
@Column({
|
||||
type: 'uuid',
|
||||
@ -38,9 +31,8 @@ export class SubspaceProductAllocationEntity extends AbstractEntity<SubspaceProd
|
||||
@ManyToOne(() => ProductEntity, { nullable: false, onDelete: 'CASCADE' })
|
||||
public product: ProductEntity;
|
||||
|
||||
@ManyToMany(() => NewTagEntity)
|
||||
@JoinTable({ name: 'subspace_product_tags' })
|
||||
public tags: NewTagEntity[];
|
||||
@ManyToOne(() => NewTagEntity, { nullable: true, onDelete: 'CASCADE' })
|
||||
public tag: NewTagEntity;
|
||||
|
||||
constructor(partial: Partial<SubspaceProductAllocationEntity>) {
|
||||
super();
|
||||
|
@ -4,7 +4,6 @@ import { SubspaceModelEntity } from '@app/common/modules/space-model';
|
||||
import { Column, Entity, JoinColumn, ManyToOne, OneToMany } from 'typeorm';
|
||||
import { SubspaceDto } from '../../dtos';
|
||||
import { SpaceEntity } from '../space.entity';
|
||||
import { TagEntity } from '../tag.entity';
|
||||
import { SubspaceProductAllocationEntity } from './subspace-product-allocation.entity';
|
||||
|
||||
@Entity({ name: 'subspace' })
|
||||
@ -43,9 +42,6 @@ export class SubspaceEntity extends AbstractEntity<SubspaceDto> {
|
||||
})
|
||||
subSpaceModel?: SubspaceModelEntity;
|
||||
|
||||
@OneToMany(() => TagEntity, (tag) => tag.subspace)
|
||||
tags: TagEntity[];
|
||||
|
||||
@OneToMany(
|
||||
() => SubspaceProductAllocationEntity,
|
||||
(allocation) => allocation.subspace,
|
||||
|
@ -1,41 +0,0 @@
|
||||
import { Entity, Column, ManyToOne, JoinColumn, OneToOne } from 'typeorm';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import { ProductEntity } from '../../product/entities';
|
||||
import { TagDto } from '../dtos';
|
||||
import { TagModel } from '../../space-model/entities/tag-model.entity';
|
||||
import { DeviceEntity } from '../../device/entities';
|
||||
import { SubspaceEntity } from './subspace/subspace.entity';
|
||||
|
||||
@Entity({ name: 'tag' })
|
||||
export class TagEntity extends AbstractEntity<TagDto> {
|
||||
@Column({ type: 'varchar', length: 255, nullable: true })
|
||||
tag: string;
|
||||
|
||||
@ManyToOne(() => TagModel, (model) => model.tags, {
|
||||
nullable: true,
|
||||
})
|
||||
model: TagModel;
|
||||
|
||||
@ManyToOne(() => ProductEntity, (product) => product.tags, {
|
||||
nullable: false,
|
||||
})
|
||||
product: ProductEntity;
|
||||
|
||||
@ManyToOne(() => SubspaceEntity, (subspace) => subspace.tags, {
|
||||
nullable: true,
|
||||
})
|
||||
@JoinColumn({ name: 'subspace_id' })
|
||||
subspace: SubspaceEntity;
|
||||
|
||||
@Column({
|
||||
nullable: false,
|
||||
default: false,
|
||||
})
|
||||
public disabled: boolean;
|
||||
|
||||
@OneToOne(() => DeviceEntity, (device) => device.tag, {
|
||||
nullable: true,
|
||||
})
|
||||
@JoinColumn({ name: 'device_id' })
|
||||
device: DeviceEntity;
|
||||
}
|
@ -1,10 +1,9 @@
|
||||
import { DataSource, Repository } from 'typeorm';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { DataSource, Repository } from 'typeorm';
|
||||
import { InviteSpaceEntity } from '../entities/invite-space.entity';
|
||||
import { SpaceLinkEntity } from '../entities/space-link.entity';
|
||||
import { SpaceEntity } from '../entities/space.entity';
|
||||
import { TagEntity } from '../entities/tag.entity';
|
||||
import { SpaceProductAllocationEntity } from '../entities/space-product-allocation.entity';
|
||||
import { SpaceEntity } from '../entities/space.entity';
|
||||
|
||||
@Injectable()
|
||||
export class SpaceRepository extends Repository<SpaceEntity> {
|
||||
@ -20,13 +19,6 @@ export class SpaceLinkRepository extends Repository<SpaceLinkEntity> {
|
||||
}
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class TagRepository extends Repository<TagEntity> {
|
||||
constructor(private dataSource: DataSource) {
|
||||
super(TagEntity, dataSource.createEntityManager());
|
||||
}
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class InviteSpaceRepository extends Repository<InviteSpaceEntity> {
|
||||
constructor(private dataSource: DataSource) {
|
||||
|
@ -6,7 +6,6 @@ import { SpaceProductAllocationEntity } from './entities/space-product-allocatio
|
||||
import { SpaceEntity } from './entities/space.entity';
|
||||
import { SubspaceProductAllocationEntity } from './entities/subspace/subspace-product-allocation.entity';
|
||||
import { SubspaceEntity } from './entities/subspace/subspace.entity';
|
||||
import { TagEntity } from './entities/tag.entity';
|
||||
|
||||
@Module({
|
||||
providers: [],
|
||||
@ -16,7 +15,6 @@ import { TagEntity } from './entities/tag.entity';
|
||||
TypeOrmModule.forFeature([
|
||||
SpaceEntity,
|
||||
SubspaceEntity,
|
||||
TagEntity,
|
||||
InviteSpaceEntity,
|
||||
SpaceProductAllocationEntity,
|
||||
SubspaceProductAllocationEntity,
|
||||
|
@ -1,11 +1,10 @@
|
||||
import { Entity, Column, ManyToOne, Unique, ManyToMany } from 'typeorm';
|
||||
import { ProductEntity } from '../../product/entities';
|
||||
import { ProjectEntity } from '../../project/entities';
|
||||
import { Column, Entity, ManyToOne, OneToMany, Unique } from 'typeorm';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import { NewTagDto } from '../dtos/tag.dto';
|
||||
import { DeviceEntity } from '../../device/entities/device.entity';
|
||||
import { ProjectEntity } from '../../project/entities';
|
||||
import { SpaceModelProductAllocationEntity } from '../../space-model/entities/space-model-product-allocation.entity';
|
||||
import { SubspaceModelProductAllocationEntity } from '../../space-model/entities/subspace-model/subspace-model-product-allocation.entity';
|
||||
import { DeviceEntity } from '../../device/entities/device.entity';
|
||||
import { NewTagDto } from '../dtos/tag.dto';
|
||||
|
||||
@Entity({ name: 'new_tag' })
|
||||
@Unique(['name', 'project'])
|
||||
@ -24,31 +23,25 @@ export class NewTagEntity extends AbstractEntity<NewTagDto> {
|
||||
})
|
||||
name: string;
|
||||
|
||||
@ManyToOne(() => ProductEntity, (product) => product.newTags, {
|
||||
nullable: true,
|
||||
onDelete: 'CASCADE',
|
||||
})
|
||||
public product: ProductEntity;
|
||||
|
||||
@ManyToOne(() => ProjectEntity, (project) => project.tags, {
|
||||
nullable: false,
|
||||
onDelete: 'CASCADE',
|
||||
})
|
||||
public project: ProjectEntity;
|
||||
|
||||
@ManyToMany(
|
||||
@OneToMany(
|
||||
() => SpaceModelProductAllocationEntity,
|
||||
(allocation) => allocation.tags,
|
||||
(allocation) => allocation.tag,
|
||||
)
|
||||
public spaceModelAllocations: SpaceModelProductAllocationEntity[];
|
||||
|
||||
@ManyToMany(
|
||||
@OneToMany(
|
||||
() => SubspaceModelProductAllocationEntity,
|
||||
(allocation) => allocation.tags,
|
||||
(allocation) => allocation.tag,
|
||||
)
|
||||
public subspaceModelAllocations: SubspaceModelProductAllocationEntity[];
|
||||
|
||||
@ManyToOne(() => DeviceEntity, (device) => device.tag)
|
||||
@OneToMany(() => DeviceEntity, (device) => device.tag)
|
||||
public devices: DeviceEntity[];
|
||||
|
||||
constructor(partial: Partial<NewTagEntity>) {
|
||||
|
@ -1,3 +1,4 @@
|
||||
import { defaultProfilePicture } from '@app/common/constants/default.profile.picture';
|
||||
import {
|
||||
Column,
|
||||
DeleteDateColumn,
|
||||
@ -8,27 +9,26 @@ import {
|
||||
OneToOne,
|
||||
Unique,
|
||||
} from 'typeorm';
|
||||
import { OtpType } from '../../../../src/constants/otp-type.enum';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import { ClientEntity } from '../../client/entities';
|
||||
import {
|
||||
DeviceNotificationEntity,
|
||||
DeviceUserPermissionEntity,
|
||||
} from '../../device/entities';
|
||||
import { InviteUserEntity } from '../../Invite-user/entities';
|
||||
import { ProjectEntity } from '../../project/entities';
|
||||
import { RegionEntity } from '../../region/entities';
|
||||
import { RoleTypeEntity } from '../../role-type/entities';
|
||||
import { SpaceEntity } from '../../space/entities/space.entity';
|
||||
import { TimeZoneEntity } from '../../timezone/entities';
|
||||
import { VisitorPasswordEntity } from '../../visitor-password/entities';
|
||||
import {
|
||||
UserDto,
|
||||
UserNotificationDto,
|
||||
UserOtpDto,
|
||||
UserSpaceDto,
|
||||
} from '../dtos';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import {
|
||||
DeviceNotificationEntity,
|
||||
DeviceUserPermissionEntity,
|
||||
} from '../../device/entities';
|
||||
import { defaultProfilePicture } from '@app/common/constants/default.profile.picture';
|
||||
import { RegionEntity } from '../../region/entities';
|
||||
import { TimeZoneEntity } from '../../timezone/entities';
|
||||
import { OtpType } from '../../../../src/constants/otp-type.enum';
|
||||
import { RoleTypeEntity } from '../../role-type/entities';
|
||||
import { VisitorPasswordEntity } from '../../visitor-password/entities';
|
||||
import { InviteUserEntity } from '../../Invite-user/entities';
|
||||
import { ProjectEntity } from '../../project/entities';
|
||||
import { SpaceEntity } from '../../space/entities/space.entity';
|
||||
import { ClientEntity } from '../../client/entities';
|
||||
|
||||
@Entity({ name: 'user' })
|
||||
export class UserEntity extends AbstractEntity<UserDto> {
|
||||
@ -94,7 +94,9 @@ export class UserEntity extends AbstractEntity<UserDto> {
|
||||
@Column({ type: 'timestamp', nullable: true })
|
||||
appAgreementAcceptedAt: Date;
|
||||
|
||||
@OneToMany(() => UserSpaceEntity, (userSpace) => userSpace.user)
|
||||
@OneToMany(() => UserSpaceEntity, (userSpace) => userSpace.user, {
|
||||
onDelete: 'CASCADE',
|
||||
})
|
||||
userSpaces: UserSpaceEntity[];
|
||||
|
||||
@OneToMany(
|
||||
@ -158,6 +160,7 @@ export class UserEntity extends AbstractEntity<UserDto> {
|
||||
export class UserNotificationEntity extends AbstractEntity<UserNotificationDto> {
|
||||
@ManyToOne(() => UserEntity, (user) => user.roleType, {
|
||||
nullable: false,
|
||||
onDelete: 'CASCADE',
|
||||
})
|
||||
user: UserEntity;
|
||||
@Column({
|
||||
@ -219,7 +222,10 @@ export class UserSpaceEntity extends AbstractEntity<UserSpaceDto> {
|
||||
})
|
||||
public uuid: string;
|
||||
|
||||
@ManyToOne(() => UserEntity, (user) => user.userSpaces, { nullable: false })
|
||||
@ManyToOne(() => UserEntity, (user) => user.userSpaces, {
|
||||
nullable: false,
|
||||
onDelete: 'CASCADE',
|
||||
})
|
||||
user: UserEntity;
|
||||
|
||||
@ManyToOne(() => SpaceEntity, (space) => space.userSpaces, {
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { Column, Entity, ManyToOne, JoinColumn, Index } from 'typeorm';
|
||||
import { VisitorPasswordDto } from '../dtos';
|
||||
import { Column, Entity, Index, JoinColumn, ManyToOne } from 'typeorm';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import { UserEntity } from '../../user/entities/user.entity';
|
||||
import { VisitorPasswordDto } from '../dtos';
|
||||
|
||||
@Entity({ name: 'visitor-password' })
|
||||
@Index('IDX_PASSWORD_TUYA_UUID', ['passwordTuyaUuid'])
|
||||
@ -14,6 +14,7 @@ export class VisitorPasswordEntity extends AbstractEntity<VisitorPasswordDto> {
|
||||
|
||||
@ManyToOne(() => UserEntity, (user) => user.visitorPasswords, {
|
||||
nullable: false,
|
||||
onDelete: 'CASCADE',
|
||||
})
|
||||
@JoinColumn({ name: 'authorizer_uuid' })
|
||||
public user: UserEntity;
|
||||
|
@ -61,6 +61,10 @@ export class SuperAdminSeeder {
|
||||
lastName: 'Admin',
|
||||
isUserVerified: true,
|
||||
isActive: true,
|
||||
hasAcceptedAppAgreement: true,
|
||||
hasAcceptedWebAgreement: true,
|
||||
appAgreementAcceptedAt: new Date(),
|
||||
webAgreementAcceptedAt: new Date(),
|
||||
roleType: { uuid: defaultUserRoleUuid },
|
||||
});
|
||||
} catch (err) {
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import * as nodemailer from 'nodemailer';
|
||||
import axios from 'axios';
|
||||
import * as nodemailer from 'nodemailer';
|
||||
import {
|
||||
SEND_EMAIL_API_URL_DEV,
|
||||
SEND_EMAIL_API_URL_PROD,
|
||||
@ -83,12 +83,17 @@ export class EmailService {
|
||||
);
|
||||
}
|
||||
}
|
||||
async sendEmailWithTemplate(
|
||||
email: string,
|
||||
name: string,
|
||||
isEnable: boolean,
|
||||
isDelete: boolean,
|
||||
): Promise<void> {
|
||||
async sendEmailWithTemplate({
|
||||
email,
|
||||
name,
|
||||
isEnable,
|
||||
isDelete,
|
||||
}: {
|
||||
email: string;
|
||||
name: string;
|
||||
isEnable: boolean;
|
||||
isDelete: boolean;
|
||||
}): Promise<void> {
|
||||
const isProduction = process.env.NODE_ENV === 'production';
|
||||
const API_TOKEN = this.configService.get<string>(
|
||||
'email-config.MAILTRAP_API_TOKEN',
|
||||
|
660
package-lock.json
generated
660
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
30
package.json
30
package.json
@ -6,18 +6,25 @@
|
||||
"private": true,
|
||||
"license": "UNLICENSED",
|
||||
"scripts": {
|
||||
"build": "npm run test && npx nest build",
|
||||
"build": "npx nest build",
|
||||
"build:lambda": "npx nest build && cp package*.json dist/",
|
||||
"format": "prettier --write \"apps/**/*.ts\" \"libs/**/*.ts\"",
|
||||
"start": "npm run test && node dist/main",
|
||||
"start:dev": "npm run test && npx nest start --watch",
|
||||
"start:debug": "npm run test && npx nest start --debug --watch",
|
||||
"start:prod": "npm run test && node dist/main",
|
||||
"start": "node dist/main",
|
||||
"start:dev": "npx nest start --watch",
|
||||
"dev": "npx nest start --watch",
|
||||
"start:debug": "npx nest start --debug --watch",
|
||||
"start:prod": "node dist/main",
|
||||
"start:lambda": "node dist/lambda",
|
||||
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
|
||||
"test": "jest --config jest.config.js",
|
||||
"test:watch": "jest --watch --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: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": {
|
||||
"@fast-csv/format": "^5.0.2",
|
||||
@ -35,13 +42,16 @@
|
||||
"@nestjs/typeorm": "^10.0.2",
|
||||
"@nestjs/websockets": "^10.3.8",
|
||||
"@tuya/tuya-connector-nodejs": "^2.1.2",
|
||||
"@types/aws-lambda": "^8.10.150",
|
||||
"argon2": "^0.40.1",
|
||||
"aws-serverless-express": "^3.4.0",
|
||||
"axios": "^1.7.7",
|
||||
"bcryptjs": "^2.4.3",
|
||||
"class-transformer": "^0.5.1",
|
||||
"class-validator": "^0.14.1",
|
||||
"crypto-js": "^4.2.0",
|
||||
"csv-parser": "^3.2.0",
|
||||
"dotenv": "^17.0.0",
|
||||
"express-rate-limit": "^7.1.5",
|
||||
"firebase": "^10.12.5",
|
||||
"google-auth-library": "^9.14.1",
|
||||
@ -51,11 +61,13 @@
|
||||
"nest-winston": "^1.10.2",
|
||||
"nodemailer": "^6.9.10",
|
||||
"onesignal-node": "^3.4.0",
|
||||
"passport": "^0.7.0",
|
||||
"passport-jwt": "^4.0.1",
|
||||
"pg": "^8.11.3",
|
||||
"reflect-metadata": "^0.2.0",
|
||||
"rxjs": "^7.8.1",
|
||||
"typeorm": "^0.3.20",
|
||||
"webpack": "^5.99.9",
|
||||
"winston": "^3.17.0",
|
||||
"ws": "^8.17.0"
|
||||
},
|
||||
@ -72,7 +84,9 @@
|
||||
"@types/supertest": "^6.0.0",
|
||||
"@typescript-eslint/eslint-plugin": "^6.0.0",
|
||||
"@typescript-eslint/parser": "^6.0.0",
|
||||
"aws-cdk-lib": "^2.202.0",
|
||||
"concurrently": "^8.2.2",
|
||||
"constructs": "^10.4.2",
|
||||
"eslint": "^8.42.0",
|
||||
"eslint-config-prettier": "^9.0.0",
|
||||
"eslint-plugin-import": "^2.31.0",
|
||||
@ -86,5 +100,9 @@
|
||||
"ts-node": "^10.9.1",
|
||||
"tsconfig-paths": "^4.2.0",
|
||||
"typescript": "^5.1.3"
|
||||
},
|
||||
"engines": {
|
||||
"node": "20.x",
|
||||
"npm": "10.x"
|
||||
}
|
||||
}
|
||||
|
@ -1,42 +1,42 @@
|
||||
import { SeederModule } from '@app/common/seed/seeder.module';
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import config from './config';
|
||||
import { APP_INTERCEPTOR } from '@nestjs/core';
|
||||
import { WinstonModule } from 'nest-winston';
|
||||
import { AuthenticationModule } from './auth/auth.module';
|
||||
import { UserModule } from './users/user.module';
|
||||
import { GroupModule } from './group/group.module';
|
||||
import { DeviceModule } from './device/device.module';
|
||||
import { UserDevicePermissionModule } from './user-device-permission/user-device-permission.module';
|
||||
import { CommunityModule } from './community/community.module';
|
||||
import { SeederModule } from '@app/common/seed/seeder.module';
|
||||
import { UserNotificationModule } from './user-notification/user-notification.module';
|
||||
import { DeviceMessagesSubscriptionModule } from './device-messages/device-messages.module';
|
||||
import { SceneModule } from './scene/scene.module';
|
||||
import { DoorLockModule } from './door-lock/door.lock.module';
|
||||
import { APP_GUARD, APP_INTERCEPTOR } from '@nestjs/core';
|
||||
import { LoggingInterceptor } from './interceptors/logging.interceptor';
|
||||
import { AutomationModule } from './automation/automation.module';
|
||||
import { RegionModule } from './region/region.module';
|
||||
import { TimeZoneModule } from './timezone/timezone.module';
|
||||
import { VisitorPasswordModule } from './vistor-password/visitor-password.module';
|
||||
import { ScheduleModule } from './schedule/schedule.module';
|
||||
import { SpaceModule } from './space/space.module';
|
||||
import { ProductModule } from './product';
|
||||
import { ProjectModule } from './project';
|
||||
import { SpaceModelModule } from './space-model';
|
||||
import { InviteUserModule } from './invite-user/invite-user.module';
|
||||
import { PermissionModule } from './permission/permission.module';
|
||||
import { RoleModule } from './role/role.module';
|
||||
import { TermsConditionsModule } from './terms-conditions/terms-conditions.module';
|
||||
import { PrivacyPolicyModule } from './privacy-policy/privacy-policy.module';
|
||||
import { TagModule } from './tags/tags.module';
|
||||
import { ClientModule } from './client/client.module';
|
||||
import { DeviceCommissionModule } from './commission-device/commission-device.module';
|
||||
import { PowerClampModule } from './power-clamp/power-clamp.module';
|
||||
import { WinstonModule } from 'nest-winston';
|
||||
import { ThrottlerGuard, ThrottlerModule } from '@nestjs/throttler';
|
||||
import { CommunityModule } from './community/community.module';
|
||||
import config from './config';
|
||||
import { DeviceMessagesSubscriptionModule } from './device-messages/device-messages.module';
|
||||
import { DeviceModule } from './device/device.module';
|
||||
import { DoorLockModule } from './door-lock/door.lock.module';
|
||||
import { GroupModule } from './group/group.module';
|
||||
import { HealthModule } from './health/health.module';
|
||||
import { LoggingInterceptor } from './interceptors/logging.interceptor';
|
||||
import { InviteUserModule } from './invite-user/invite-user.module';
|
||||
import { PermissionModule } from './permission/permission.module';
|
||||
import { PowerClampModule } from './power-clamp/power-clamp.module';
|
||||
import { PrivacyPolicyModule } from './privacy-policy/privacy-policy.module';
|
||||
import { ProductModule } from './product';
|
||||
import { ProjectModule } from './project';
|
||||
import { RegionModule } from './region/region.module';
|
||||
import { RoleModule } from './role/role.module';
|
||||
import { SceneModule } from './scene/scene.module';
|
||||
import { ScheduleModule } from './schedule/schedule.module';
|
||||
import { SpaceModelModule } from './space-model';
|
||||
import { SpaceModule } from './space/space.module';
|
||||
import { TagModule } from './tags/tags.module';
|
||||
import { TermsConditionsModule } from './terms-conditions/terms-conditions.module';
|
||||
import { TimeZoneModule } from './timezone/timezone.module';
|
||||
import { UserDevicePermissionModule } from './user-device-permission/user-device-permission.module';
|
||||
import { UserNotificationModule } from './user-notification/user-notification.module';
|
||||
import { UserModule } from './users/user.module';
|
||||
import { VisitorPasswordModule } from './vistor-password/visitor-password.module';
|
||||
|
||||
import { winstonLoggerOptions } from '../libs/common/src/logger/services/winston.logger';
|
||||
import { AqiModule } from './aqi/aqi.module';
|
||||
import { OccupancyModule } from './occupancy/occupancy.module';
|
||||
import { WeatherModule } from './weather/weather.module';
|
||||
@Module({
|
||||
@ -81,6 +81,7 @@ import { WeatherModule } from './weather/weather.module';
|
||||
HealthModule,
|
||||
OccupancyModule,
|
||||
WeatherModule,
|
||||
AqiModule,
|
||||
],
|
||||
providers: [
|
||||
{
|
||||
|
11
src/aqi/aqi.module.ts
Normal file
11
src/aqi/aqi.module.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||
import { AqiService } from './services';
|
||||
import { AqiController } from './controllers';
|
||||
@Module({
|
||||
imports: [ConfigModule],
|
||||
controllers: [AqiController],
|
||||
providers: [AqiService, SqlLoaderService],
|
||||
})
|
||||
export class AqiModule {}
|
64
src/aqi/controllers/aqi.controller.ts
Normal file
64
src/aqi/controllers/aqi.controller.ts
Normal file
@ -0,0 +1,64 @@
|
||||
import { Controller, Get, Param, Query, UseGuards } from '@nestjs/common';
|
||||
import {
|
||||
ApiTags,
|
||||
ApiBearerAuth,
|
||||
ApiOperation,
|
||||
ApiParam,
|
||||
} from '@nestjs/swagger';
|
||||
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
|
||||
import { ControllerRoute } from '@app/common/constants/controller-route';
|
||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||
import { AqiService } from '../services/aqi.service';
|
||||
import {
|
||||
GetAqiDailyBySpaceDto,
|
||||
GetAqiPollutantBySpaceDto,
|
||||
} from '../dto/get-aqi.dto';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import { SpaceParamsDto } from '../dto/aqi-params.dto';
|
||||
|
||||
@ApiTags('AQI Module')
|
||||
@Controller({
|
||||
version: EnableDisableStatusEnum.ENABLED,
|
||||
path: ControllerRoute.AQI.ROUTE,
|
||||
})
|
||||
export class AqiController {
|
||||
constructor(private readonly aqiService: AqiService) {}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get('range/space/:spaceUuid')
|
||||
@ApiOperation({
|
||||
summary: ControllerRoute.AQI.ACTIONS.GET_AQI_RANGE_DATA_SUMMARY,
|
||||
description: ControllerRoute.AQI.ACTIONS.GET_AQI_RANGE_DATA_DESCRIPTION,
|
||||
})
|
||||
@ApiParam({
|
||||
name: 'spaceUuid',
|
||||
description: 'UUID of the Space',
|
||||
required: true,
|
||||
})
|
||||
async getAQIRangeDataBySpace(
|
||||
@Param() params: SpaceParamsDto,
|
||||
@Query() query: GetAqiDailyBySpaceDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
return await this.aqiService.getAQIRangeDataBySpace(params, query);
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get('distribution/space/:spaceUuid')
|
||||
@ApiOperation({
|
||||
summary: ControllerRoute.AQI.ACTIONS.GET_AQI_DISTRIBUTION_DATA_SUMMARY,
|
||||
description:
|
||||
ControllerRoute.AQI.ACTIONS.GET_AQI_DISTRIBUTION_DATA_DESCRIPTION,
|
||||
})
|
||||
@ApiParam({
|
||||
name: 'spaceUuid',
|
||||
description: 'UUID of the Space',
|
||||
required: true,
|
||||
})
|
||||
async getAQIDistributionDataBySpace(
|
||||
@Param() params: SpaceParamsDto,
|
||||
@Query() query: GetAqiPollutantBySpaceDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
return await this.aqiService.getAQIDistributionDataBySpace(params, query);
|
||||
}
|
||||
}
|
1
src/aqi/controllers/index.ts
Normal file
1
src/aqi/controllers/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './aqi.controller';
|
7
src/aqi/dto/aqi-params.dto.ts
Normal file
7
src/aqi/dto/aqi-params.dto.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import { IsNotEmpty, IsUUID } from 'class-validator';
|
||||
|
||||
export class SpaceParamsDto {
|
||||
@IsUUID('4', { message: 'Invalid UUID format' })
|
||||
@IsNotEmpty()
|
||||
spaceUuid: string;
|
||||
}
|
37
src/aqi/dto/get-aqi.dto.ts
Normal file
37
src/aqi/dto/get-aqi.dto.ts
Normal file
@ -0,0 +1,37 @@
|
||||
import { PollutantType } from '@app/common/constants/pollutants.enum';
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { Matches, IsNotEmpty, IsString } from 'class-validator';
|
||||
|
||||
export class GetAqiDailyBySpaceDto {
|
||||
@ApiProperty({
|
||||
description: 'Month and year in format YYYY-MM',
|
||||
example: '2025-03',
|
||||
required: true,
|
||||
})
|
||||
@Matches(/^\d{4}-(0[1-9]|1[0-2])$/, {
|
||||
message: 'monthDate must be in YYYY-MM format',
|
||||
})
|
||||
@IsNotEmpty()
|
||||
monthDate: string;
|
||||
}
|
||||
export class GetAqiPollutantBySpaceDto {
|
||||
@ApiProperty({
|
||||
description: 'Pollutant Type',
|
||||
enum: PollutantType,
|
||||
example: PollutantType.AQI,
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public pollutantType: string;
|
||||
@ApiProperty({
|
||||
description: 'Month and year in format YYYY-MM',
|
||||
example: '2025-03',
|
||||
required: true,
|
||||
})
|
||||
@Matches(/^\d{4}-(0[1-9]|1[0-2])$/, {
|
||||
message: 'monthDate must be in YYYY-MM format',
|
||||
})
|
||||
@IsNotEmpty()
|
||||
monthDate: string;
|
||||
}
|
138
src/aqi/services/aqi.service.ts
Normal file
138
src/aqi/services/aqi.service.ts
Normal file
@ -0,0 +1,138 @@
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import {
|
||||
GetAqiDailyBySpaceDto,
|
||||
GetAqiPollutantBySpaceDto,
|
||||
} from '../dto/get-aqi.dto';
|
||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||
import { SpaceParamsDto } from '../dto/aqi-params.dto';
|
||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||
import { DataSource } from 'typeorm';
|
||||
import { SQL_PROCEDURES_PATH } from '@app/common/constants/sql-query-path';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
||||
import { PollutantType } from '@app/common/constants/pollutants.enum';
|
||||
|
||||
@Injectable()
|
||||
export class AqiService {
|
||||
constructor(
|
||||
private readonly sqlLoader: SqlLoaderService,
|
||||
private readonly dataSource: DataSource,
|
||||
) {}
|
||||
async getAQIDistributionDataBySpace(
|
||||
params: SpaceParamsDto,
|
||||
query: GetAqiPollutantBySpaceDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
const { monthDate, pollutantType } = query;
|
||||
const { spaceUuid } = params;
|
||||
|
||||
try {
|
||||
const data = await this.executeProcedure(
|
||||
'fact_daily_space_aqi',
|
||||
'proceduce_select_daily_space_aqi',
|
||||
[spaceUuid, monthDate],
|
||||
);
|
||||
|
||||
const categories = [
|
||||
'good',
|
||||
'moderate',
|
||||
'unhealthy_sensitive',
|
||||
'unhealthy',
|
||||
'very_unhealthy',
|
||||
'hazardous',
|
||||
];
|
||||
|
||||
const transformedData = data.map((item) => {
|
||||
const date = new Date(item.event_date).toLocaleDateString('en-CA'); // YYYY-MM-DD
|
||||
|
||||
const categoryData = categories.map((category) => {
|
||||
const key = `${category}_${pollutantType.toLowerCase()}_percentage`;
|
||||
return {
|
||||
type: category,
|
||||
percentage: item[key] ?? 0,
|
||||
};
|
||||
});
|
||||
|
||||
return { date, data: categoryData };
|
||||
});
|
||||
|
||||
const response = this.buildResponse(
|
||||
`AQI distribution data fetched successfully for ${spaceUuid} space and pollutant ${pollutantType}`,
|
||||
transformedData,
|
||||
);
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch AQI distribution data', {
|
||||
error,
|
||||
spaceUuid,
|
||||
});
|
||||
throw new HttpException(
|
||||
error.response?.message || 'Failed to fetch AQI distribution data',
|
||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async getAQIRangeDataBySpace(
|
||||
params: SpaceParamsDto,
|
||||
query: GetAqiDailyBySpaceDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
const { monthDate } = query;
|
||||
const { spaceUuid } = params;
|
||||
|
||||
try {
|
||||
const data = await this.executeProcedure(
|
||||
'fact_daily_space_aqi',
|
||||
'proceduce_select_daily_space_aqi',
|
||||
[spaceUuid, monthDate],
|
||||
);
|
||||
|
||||
// Define pollutants dynamically
|
||||
const pollutants = Object.values(PollutantType);
|
||||
|
||||
const transformedData = data.map((item) => {
|
||||
const date = new Date(item.event_date).toLocaleDateString('en-CA'); // YYYY-MM-DD
|
||||
const dailyData = pollutants.map((type) => ({
|
||||
type,
|
||||
min: item[`daily_min_${type}`],
|
||||
max: item[`daily_max_${type}`],
|
||||
average: item[`daily_avg_${type}`],
|
||||
}));
|
||||
return { date, data: dailyData };
|
||||
});
|
||||
|
||||
const response = this.buildResponse(
|
||||
`AQI data fetched successfully for ${spaceUuid} space`,
|
||||
transformedData,
|
||||
);
|
||||
return convertKeysToCamelCase(response);
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch AQI data', {
|
||||
error,
|
||||
spaceUuid,
|
||||
});
|
||||
throw new HttpException(
|
||||
error.response?.message || 'Failed to fetch AQI data',
|
||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private buildResponse(message: string, data: any[]) {
|
||||
return new SuccessResponseDto({
|
||||
message,
|
||||
data,
|
||||
statusCode: HttpStatus.OK,
|
||||
});
|
||||
}
|
||||
private async executeProcedure(
|
||||
procedureFolderName: string,
|
||||
procedureFileName: string,
|
||||
params: (string | number | null)[],
|
||||
): Promise<any[]> {
|
||||
const query = this.loadQuery(procedureFolderName, procedureFileName);
|
||||
return await this.dataSource.query(query, params);
|
||||
}
|
||||
private loadQuery(folderName: string, fileName: string): string {
|
||||
return this.sqlLoader.loadQuery(folderName, fileName, SQL_PROCEDURES_PATH);
|
||||
}
|
||||
}
|
1
src/aqi/services/index.ts
Normal file
1
src/aqi/services/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './aqi.service';
|
@ -29,6 +29,7 @@ import {
|
||||
import { PowerClampService } from '@app/common/helper/services/power.clamp.service';
|
||||
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({
|
||||
imports: [ConfigModule, SpaceRepositoryModule],
|
||||
@ -57,6 +58,7 @@ import { OccupancyService } from '@app/common/helper/services/occupancy.service'
|
||||
PowerClampMonthlyRepository,
|
||||
SqlLoaderService,
|
||||
OccupancyService,
|
||||
AqiDataService,
|
||||
],
|
||||
exports: [],
|
||||
})
|
||||
|
@ -1,17 +1,19 @@
|
||||
import * as fs from 'fs';
|
||||
import * as csv from 'csv-parser';
|
||||
import * as fs from 'fs';
|
||||
|
||||
import { ProjectParam } from '@app/common/dto/project-param.dto';
|
||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||
import { ProjectRepository } from '@app/common/modules/project/repositiories';
|
||||
import { SpaceRepository } from '@app/common/modules/space';
|
||||
import { SpaceProductAllocationEntity } from '@app/common/modules/space/entities/space-product-allocation.entity';
|
||||
import { SubspaceProductAllocationEntity } from '@app/common/modules/space/entities/subspace/subspace-product-allocation.entity';
|
||||
import { SubspaceEntity } from '@app/common/modules/space/entities/subspace/subspace.entity';
|
||||
import { SubspaceRepository } from '@app/common/modules/space/repositories/subspace.repository';
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { DeviceService } from 'src/device/services';
|
||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||
import { SpaceRepository } from '@app/common/modules/space';
|
||||
import { SubspaceRepository } from '@app/common/modules/space/repositories/subspace.repository';
|
||||
import { SubspaceEntity } from '@app/common/modules/space/entities/subspace/subspace.entity';
|
||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||
import { ProjectParam } from '@app/common/dto/project-param.dto';
|
||||
import { ProjectRepository } from '@app/common/modules/project/repositiories';
|
||||
|
||||
@Injectable()
|
||||
export class DeviceCommissionService {
|
||||
@ -118,7 +120,7 @@ export class DeviceCommissionService {
|
||||
where: { uuid: spaceId },
|
||||
relations: [
|
||||
'productAllocations',
|
||||
'productAllocations.tags',
|
||||
'productAllocations.tag',
|
||||
'productAllocations.product',
|
||||
],
|
||||
});
|
||||
@ -135,7 +137,7 @@ export class DeviceCommissionService {
|
||||
where: { uuid: subspaceId },
|
||||
relations: [
|
||||
'productAllocations',
|
||||
'productAllocations.tags',
|
||||
'productAllocations.tag',
|
||||
'productAllocations.product',
|
||||
],
|
||||
});
|
||||
@ -151,19 +153,23 @@ export class DeviceCommissionService {
|
||||
subspace?.productAllocations || space.productAllocations;
|
||||
|
||||
const match = allocations
|
||||
.flatMap((pa) =>
|
||||
(pa.tags || []).map((tag) => ({ product: pa.product, tag })),
|
||||
.map(
|
||||
({
|
||||
product,
|
||||
tag,
|
||||
}:
|
||||
| SpaceProductAllocationEntity
|
||||
| SubspaceProductAllocationEntity) => ({ product, tag }),
|
||||
)
|
||||
.find(({ tag }) => tag.name === tagName);
|
||||
.find(
|
||||
({ tag, product }) =>
|
||||
tag.name === tagName && product.name === productName,
|
||||
);
|
||||
|
||||
if (!match) {
|
||||
console.error(`No matching tag found for Device ID: ${rawDeviceId}`);
|
||||
failureCount.value++;
|
||||
return;
|
||||
}
|
||||
|
||||
if (match.product.name !== productName) {
|
||||
console.error(`Product name mismatch for Device ID: ${rawDeviceId}`);
|
||||
console.error(
|
||||
`No matching tag-product combination found for Device ID: ${rawDeviceId}`,
|
||||
);
|
||||
failureCount.value++;
|
||||
return;
|
||||
}
|
||||
|
@ -8,7 +8,6 @@ import {
|
||||
SpaceLinkRepository,
|
||||
SpaceProductAllocationRepository,
|
||||
SpaceRepository,
|
||||
TagRepository,
|
||||
} from '@app/common/modules/space/repositories';
|
||||
import { UserSpaceRepository } from '@app/common/modules/user/repositories';
|
||||
import { UserRepositoryModule } from '@app/common/modules/user/user.repository.module';
|
||||
@ -64,6 +63,7 @@ import {
|
||||
} 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({
|
||||
imports: [ConfigModule, SpaceRepositoryModule, UserRepositoryModule],
|
||||
@ -86,6 +86,7 @@ import { OccupancyService } from '@app/common/helper/services/occupancy.service'
|
||||
SpaceProductAllocationService,
|
||||
SpaceLinkRepository,
|
||||
SubspaceRepository,
|
||||
// Todo: find out why this is needed
|
||||
TagService,
|
||||
SubspaceDeviceService,
|
||||
SubspaceProductAllocationService,
|
||||
@ -97,7 +98,6 @@ import { OccupancyService } from '@app/common/helper/services/occupancy.service'
|
||||
SpaceModelProductAllocationService,
|
||||
SpaceProductAllocationRepository,
|
||||
SubspaceProductAllocationRepository,
|
||||
TagRepository,
|
||||
SubspaceModelRepository,
|
||||
SubspaceModelProductAllocationService,
|
||||
SpaceModelProductAllocationRepoitory,
|
||||
@ -116,6 +116,7 @@ import { OccupancyService } from '@app/common/helper/services/occupancy.service'
|
||||
PowerClampMonthlyRepository,
|
||||
SqlLoaderService,
|
||||
OccupancyService,
|
||||
AqiDataService,
|
||||
],
|
||||
exports: [CommunityService, SpacePermissionService],
|
||||
})
|
||||
|
@ -1,29 +1,30 @@
|
||||
import {
|
||||
Injectable,
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { AddCommunityDto, GetCommunityParams, ProjectParam } from '../dtos';
|
||||
import { UpdateCommunityNameDto } from '../dtos/update.community.dto';
|
||||
import { ORPHAN_COMMUNITY_NAME } from '@app/common/constants/orphan-constant';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import { PageResponse } from '@app/common/dto/pagination.response.dto';
|
||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||
import {
|
||||
ExtendedTypeORMCustomModelFindAllQuery,
|
||||
TypeORMCustomModel,
|
||||
} from '@app/common/models/typeOrmCustom.model';
|
||||
import { PageResponse } from '@app/common/dto/pagination.response.dto';
|
||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||
import { CommunityDto } from '@app/common/modules/community/dtos';
|
||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||
import { ProjectRepository } from '@app/common/modules/project/repositiories';
|
||||
import { ORPHAN_COMMUNITY_NAME } from '@app/common/constants/orphan-constant';
|
||||
import { ILike, In, Not } from 'typeorm';
|
||||
import { SpaceService } from 'src/space/services';
|
||||
import { SpaceRepository } from '@app/common/modules/space';
|
||||
import { CommunityEntity } from '@app/common/modules/community/entities';
|
||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||
import { DeviceEntity } from '@app/common/modules/device/entities';
|
||||
import { ProjectRepository } from '@app/common/modules/project/repositiories';
|
||||
import { SpaceRepository } from '@app/common/modules/space';
|
||||
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
|
||||
import { addSpaceUuidToDevices } from '@app/common/util/device-utils';
|
||||
import {
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { SpaceService } from 'src/space/services';
|
||||
import { SelectQueryBuilder } from 'typeorm';
|
||||
import { AddCommunityDto, GetCommunityParams, ProjectParam } from '../dtos';
|
||||
import { UpdateCommunityNameDto } from '../dtos/update.community.dto';
|
||||
|
||||
@Injectable()
|
||||
export class CommunityService {
|
||||
@ -93,56 +94,37 @@ export class CommunityService {
|
||||
}
|
||||
|
||||
async getCommunities(
|
||||
param: ProjectParam,
|
||||
{ projectUuid }: ProjectParam,
|
||||
pageable: Partial<ExtendedTypeORMCustomModelFindAllQuery>,
|
||||
): Promise<BaseResponseDto> {
|
||||
try {
|
||||
const project = await this.validateProject(param.projectUuid);
|
||||
const project = await this.validateProject(projectUuid);
|
||||
|
||||
pageable.modelName = 'community';
|
||||
pageable.where = {
|
||||
project: { uuid: param.projectUuid },
|
||||
name: Not(`${ORPHAN_COMMUNITY_NAME}-${project.name}`),
|
||||
};
|
||||
/**
|
||||
* TODO: removing this breaks the code (should be fixed when refactoring @see TypeORMCustomModel
|
||||
*/
|
||||
pageable.where = {};
|
||||
let qb: undefined | SelectQueryBuilder<CommunityEntity> = undefined;
|
||||
|
||||
qb = this.communityRepository
|
||||
.createQueryBuilder('c')
|
||||
.leftJoin('c.spaces', 's', 's.disabled = false')
|
||||
.where('c.project = :projectUuid', { projectUuid })
|
||||
.andWhere(`c.name != '${ORPHAN_COMMUNITY_NAME}-${project.name}'`)
|
||||
.orderBy('c.createdAt', 'DESC')
|
||||
.distinct(true);
|
||||
if (pageable.search) {
|
||||
const matchingCommunities = await this.communityRepository.find({
|
||||
where: {
|
||||
project: { uuid: param.projectUuid },
|
||||
name: ILike(`%${pageable.search}%`),
|
||||
},
|
||||
});
|
||||
|
||||
const matchingSpaces = await this.spaceRepository.find({
|
||||
where: {
|
||||
spaceName: ILike(`%${pageable.search}%`),
|
||||
disabled: false,
|
||||
community: { project: { uuid: param.projectUuid } },
|
||||
},
|
||||
relations: ['community'],
|
||||
});
|
||||
|
||||
const spaceCommunityUuids = [
|
||||
...new Set(matchingSpaces.map((space) => space.community.uuid)),
|
||||
];
|
||||
|
||||
const allMatchedCommunityUuids = [
|
||||
...new Set([
|
||||
...matchingCommunities.map((c) => c.uuid),
|
||||
...spaceCommunityUuids,
|
||||
]),
|
||||
];
|
||||
|
||||
pageable.where = {
|
||||
...pageable.where,
|
||||
uuid: In(allMatchedCommunityUuids),
|
||||
};
|
||||
qb.andWhere(
|
||||
`c.name ILIKE '%${pageable.search}%' OR s.space_name ILIKE '%${pageable.search}%'`,
|
||||
);
|
||||
}
|
||||
|
||||
const customModel = TypeORMCustomModel(this.communityRepository);
|
||||
|
||||
const { baseResponseDto, paginationResponseDto } =
|
||||
await customModel.findAll(pageable);
|
||||
await customModel.findAll({ ...pageable, modelName: 'community' }, qb);
|
||||
|
||||
// todo: refactor this to minimize the number of queries
|
||||
if (pageable.includeSpaces) {
|
||||
const communitiesWithSpaces = await Promise.all(
|
||||
baseResponseDto.data.map(async (community: CommunityDto) => {
|
||||
@ -150,7 +132,7 @@ export class CommunityService {
|
||||
await this.spaceService.getSpacesHierarchyForCommunity(
|
||||
{
|
||||
communityUuid: community.uuid,
|
||||
projectUuid: param.projectUuid,
|
||||
projectUuid: projectUuid,
|
||||
},
|
||||
{
|
||||
onlyWithDevices: false,
|
||||
|
@ -1,39 +1,46 @@
|
||||
import { ORPHAN_SPACE_NAME } from './../../../libs/common/src/constants/orphan-constant';
|
||||
import { ProductRepository } from './../../../libs/common/src/modules/product/repositories/product.repository';
|
||||
import { AUTOMATION_CONFIG } from '@app/common/constants/automation.enum';
|
||||
import { BatchDeviceTypeEnum } from '@app/common/constants/batch-device.enum';
|
||||
import { BatteryStatus } from '@app/common/constants/battery-status.enum';
|
||||
import { DeviceStatuses } from '@app/common/constants/device-status.enum';
|
||||
import { DeviceTypeEnum } from '@app/common/constants/device-type.enum';
|
||||
import { CommonErrorCodes } from '@app/common/constants/error-codes.enum';
|
||||
import { ProductType } from '@app/common/constants/product-type.enum';
|
||||
import { SceneSwitchesTypeEnum } from '@app/common/constants/scene-switch-type.enum';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
|
||||
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||
import { DeviceEntity } from '@app/common/modules/device/entities';
|
||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||
import { ProjectEntity } from '@app/common/modules/project/entities';
|
||||
import { ProjectRepository } from '@app/common/modules/project/repositiories';
|
||||
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
||||
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
|
||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||
import { addSpaceUuidToDevices } from '@app/common/util/device-utils';
|
||||
import {
|
||||
Injectable,
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
NotFoundException,
|
||||
BadRequestException,
|
||||
forwardRef,
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
Inject,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||
import { AddAutomationDto } from 'src/automation/dtos';
|
||||
import { SceneService } from 'src/scene/services';
|
||||
import { In, Not, QueryRunner } from 'typeorm';
|
||||
import { ProjectParam } from '../dtos';
|
||||
import {
|
||||
AddDeviceDto,
|
||||
AddSceneToFourSceneDeviceDto,
|
||||
UpdateDeviceDto,
|
||||
AssignDeviceToSpaceDto,
|
||||
UpdateDeviceDto,
|
||||
} from '../dtos/add.device.dto';
|
||||
import {
|
||||
DeviceInstructionResponse,
|
||||
GetDeviceDetailsFunctionsInterface,
|
||||
GetDeviceDetailsFunctionsStatusInterface,
|
||||
GetDeviceDetailsInterface,
|
||||
GetMacAddressInterface,
|
||||
GetPowerClampFunctionsStatusInterface,
|
||||
controlDeviceInterface,
|
||||
getDeviceLogsInterface,
|
||||
updateDeviceFirmwareInterface,
|
||||
} from '../interfaces/get.device.interface';
|
||||
import {
|
||||
GetDeviceBySpaceUuidDto,
|
||||
GetDeviceLogsDto,
|
||||
GetDevicesBySpaceOrCommunityDto,
|
||||
GetDoorLockDevices,
|
||||
} from '../dtos/get.device.dto';
|
||||
import {
|
||||
BatchControlDevicesDto,
|
||||
BatchFactoryResetDevicesDto,
|
||||
@ -41,33 +48,29 @@ import {
|
||||
ControlDeviceDto,
|
||||
GetSceneFourSceneDeviceDto,
|
||||
} from '../dtos/control.device.dto';
|
||||
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||
import { In, Not, QueryRunner } from 'typeorm';
|
||||
import { ProductType } from '@app/common/constants/product-type.enum';
|
||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
|
||||
import { DeviceStatuses } from '@app/common/constants/device-status.enum';
|
||||
import { CommonErrorCodes } from '@app/common/constants/error-codes.enum';
|
||||
import { BatteryStatus } from '@app/common/constants/battery-status.enum';
|
||||
import { SceneService } from 'src/scene/services';
|
||||
import { AddAutomationDto } from 'src/automation/dtos';
|
||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
||||
import { SceneSwitchesTypeEnum } from '@app/common/constants/scene-switch-type.enum';
|
||||
import { AUTOMATION_CONFIG } from '@app/common/constants/automation.enum';
|
||||
import { DeviceSceneParamDto } from '../dtos/device.param.dto';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||
import { DeleteSceneFromSceneDeviceDto } from '../dtos/delete.device.dto';
|
||||
import { DeviceEntity } from '@app/common/modules/device/entities';
|
||||
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
|
||||
import { ProjectRepository } from '@app/common/modules/project/repositiories';
|
||||
import { ProjectParam } from '../dtos';
|
||||
import { BatchDeviceTypeEnum } from '@app/common/constants/batch-device.enum';
|
||||
import { DeviceTypeEnum } from '@app/common/constants/device-type.enum';
|
||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||
import { addSpaceUuidToDevices } from '@app/common/util/device-utils';
|
||||
import { DeviceSceneParamDto } from '../dtos/device.param.dto';
|
||||
import {
|
||||
GetDeviceLogsDto,
|
||||
GetDevicesBySpaceOrCommunityDto,
|
||||
GetDoorLockDevices,
|
||||
} from '../dtos/get.device.dto';
|
||||
import {
|
||||
controlDeviceInterface,
|
||||
DeviceInstructionResponse,
|
||||
GetDeviceDetailsFunctionsInterface,
|
||||
GetDeviceDetailsFunctionsStatusInterface,
|
||||
GetDeviceDetailsInterface,
|
||||
getDeviceLogsInterface,
|
||||
GetMacAddressInterface,
|
||||
GetPowerClampFunctionsStatusInterface,
|
||||
updateDeviceFirmwareInterface,
|
||||
} from '../interfaces/get.device.interface';
|
||||
import {
|
||||
ORPHAN_COMMUNITY_NAME,
|
||||
ORPHAN_SPACE_NAME,
|
||||
} from './../../../libs/common/src/constants/orphan-constant';
|
||||
import { ProductRepository } from './../../../libs/common/src/modules/product/repositories/product.repository';
|
||||
|
||||
@Injectable()
|
||||
export class DeviceService {
|
||||
@ -198,46 +201,6 @@ export class DeviceService {
|
||||
}
|
||||
}
|
||||
|
||||
async getDevicesBySpaceId(
|
||||
getDeviceBySpaceUuidDto: GetDeviceBySpaceUuidDto,
|
||||
): Promise<GetDeviceDetailsInterface[]> {
|
||||
try {
|
||||
const devices = await this.deviceRepository.find({
|
||||
where: {
|
||||
spaceDevice: { uuid: getDeviceBySpaceUuidDto.spaceUuid },
|
||||
isActive: true,
|
||||
},
|
||||
relations: [
|
||||
'spaceDevice',
|
||||
'productDevice',
|
||||
'permission',
|
||||
'permission.permissionType',
|
||||
],
|
||||
});
|
||||
const devicesData = await Promise.all(
|
||||
devices.map(async (device) => {
|
||||
return {
|
||||
haveRoom: device.spaceDevice ? true : false,
|
||||
productUuid: device.productDevice.uuid,
|
||||
productType: device.productDevice.prodType,
|
||||
permissionType: device.permission[0].permissionType.type,
|
||||
...(await this.getDeviceDetailsByDeviceIdTuya(
|
||||
device.deviceTuyaUuid,
|
||||
)),
|
||||
uuid: device.uuid,
|
||||
} as GetDeviceDetailsInterface;
|
||||
}),
|
||||
);
|
||||
|
||||
return devicesData;
|
||||
} catch (error) {
|
||||
// Handle the error here
|
||||
throw new HttpException(
|
||||
'Error fetching devices by space',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
async transferDeviceInSpaces(
|
||||
assignDeviceToSpaceDto: AssignDeviceToSpaceDto,
|
||||
projectUuid: string,
|
||||
@ -1199,39 +1162,6 @@ export class DeviceService {
|
||||
}
|
||||
}
|
||||
|
||||
async getFullSpaceHierarchy(
|
||||
space: SpaceEntity,
|
||||
): Promise<{ uuid: string; spaceName: string }[]> {
|
||||
try {
|
||||
// Fetch only the relevant spaces, starting with the target space
|
||||
const targetSpace = await this.spaceRepository.findOne({
|
||||
where: { uuid: space.uuid },
|
||||
relations: ['parent', 'children'],
|
||||
});
|
||||
|
||||
// Fetch only the ancestors of the target space
|
||||
const ancestors = await this.fetchAncestors(targetSpace);
|
||||
|
||||
// Optionally, fetch descendants if required
|
||||
const descendants = await this.fetchDescendants(targetSpace);
|
||||
|
||||
const fullHierarchy = [...ancestors, targetSpace, ...descendants].map(
|
||||
(space) => ({
|
||||
uuid: space.uuid,
|
||||
spaceName: space.spaceName,
|
||||
}),
|
||||
);
|
||||
|
||||
return fullHierarchy;
|
||||
} catch (error) {
|
||||
console.error('Error fetching space hierarchy:', error.message);
|
||||
throw new HttpException(
|
||||
'Error fetching space hierarchy',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async getPowerClampInstructionStatus(deviceDetails: any) {
|
||||
try {
|
||||
const deviceStatus = await this.getPowerClampInstructionStatusTuya(
|
||||
@ -1331,27 +1261,6 @@ export class DeviceService {
|
||||
return ancestors.reverse();
|
||||
}
|
||||
|
||||
private async fetchDescendants(space: SpaceEntity): Promise<SpaceEntity[]> {
|
||||
const descendants: SpaceEntity[] = [];
|
||||
|
||||
// Fetch the immediate children of the current space
|
||||
const children = await this.spaceRepository.find({
|
||||
where: { parent: { uuid: space.uuid } },
|
||||
relations: ['children'], // To continue fetching downwards
|
||||
});
|
||||
|
||||
for (const child of children) {
|
||||
// Add the child to the descendants list
|
||||
descendants.push(child);
|
||||
|
||||
// Recursively fetch the child's descendants
|
||||
const childDescendants = await this.fetchDescendants(child);
|
||||
descendants.push(...childDescendants);
|
||||
}
|
||||
|
||||
return descendants;
|
||||
}
|
||||
|
||||
async addSceneToSceneDevice(
|
||||
deviceUuid: string,
|
||||
addSceneToFourSceneDeviceDto: AddSceneToFourSceneDeviceDto,
|
||||
@ -1654,22 +1563,6 @@ export class DeviceService {
|
||||
}
|
||||
}
|
||||
|
||||
async moveDevicesToSpace(
|
||||
targetSpace: SpaceEntity,
|
||||
deviceIds: string[],
|
||||
): Promise<void> {
|
||||
if (!deviceIds || deviceIds.length === 0) {
|
||||
throw new HttpException(
|
||||
'No device IDs provided for transfer',
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
|
||||
await this.deviceRepository.update(
|
||||
{ uuid: In(deviceIds) },
|
||||
{ spaceDevice: targetSpace },
|
||||
);
|
||||
}
|
||||
async getDoorLockDevices(projectUuid: string) {
|
||||
await this.validateProject(projectUuid);
|
||||
|
||||
@ -1860,4 +1753,39 @@ export class DeviceService {
|
||||
|
||||
return allDevices;
|
||||
}
|
||||
|
||||
async addDevicesToOrphanSpace(
|
||||
space: SpaceEntity,
|
||||
project: ProjectEntity,
|
||||
queryRunner: QueryRunner,
|
||||
) {
|
||||
const spaceRepository = queryRunner.manager.getRepository(SpaceEntity);
|
||||
const deviceRepository = queryRunner.manager.getRepository(DeviceEntity);
|
||||
try {
|
||||
const orphanSpace = await spaceRepository.findOne({
|
||||
where: {
|
||||
community: {
|
||||
name: `${ORPHAN_COMMUNITY_NAME}-${project.name}`,
|
||||
},
|
||||
spaceName: ORPHAN_SPACE_NAME,
|
||||
},
|
||||
});
|
||||
|
||||
if (!orphanSpace) {
|
||||
throw new HttpException(
|
||||
`Orphan space not found in community ${project.name}`,
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
|
||||
await deviceRepository.update(
|
||||
{ uuid: In(space.devices.map((device) => device.uuid)) },
|
||||
{ spaceDevice: orphanSpace },
|
||||
);
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
`Failed to add devices to orphan spaces: ${error.message}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -29,6 +29,7 @@ import {
|
||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
||||
@Module({
|
||||
imports: [ConfigModule, DeviceRepositoryModule],
|
||||
controllers: [DoorLockController],
|
||||
@ -56,6 +57,7 @@ import { CommunityRepository } from '@app/common/modules/community/repositories'
|
||||
SqlLoaderService,
|
||||
OccupancyService,
|
||||
CommunityRepository,
|
||||
AqiDataService,
|
||||
],
|
||||
exports: [DoorLockService],
|
||||
})
|
||||
|
@ -27,6 +27,7 @@ import {
|
||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
||||
@Module({
|
||||
imports: [ConfigModule, DeviceRepositoryModule],
|
||||
controllers: [GroupController],
|
||||
@ -53,6 +54,7 @@ import { CommunityRepository } from '@app/common/modules/community/repositories'
|
||||
SqlLoaderService,
|
||||
OccupancyService,
|
||||
CommunityRepository,
|
||||
AqiDataService,
|
||||
],
|
||||
exports: [GroupService],
|
||||
})
|
||||
|
@ -1,18 +1,75 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { InviteUserService } from './services/invite-user.service';
|
||||
import { InviteUserController } from './controllers/invite-user.controller';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { InviteUserController } from './controllers/invite-user.controller';
|
||||
import { InviteUserService } from './services/invite-user.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 { PowerClampService } from '@app/common/helper/services/power.clamp.service';
|
||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||
import { AutomationRepository } from '@app/common/modules/automation/repositories';
|
||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
|
||||
import {
|
||||
UserRepository,
|
||||
UserSpaceRepository,
|
||||
} from '@app/common/modules/user/repositories';
|
||||
DeviceRepository,
|
||||
DeviceUserPermissionRepository,
|
||||
} from '@app/common/modules/device/repositories';
|
||||
import { InviteUserRepositoryModule } from '@app/common/modules/Invite-user/Invite-user.repository.module';
|
||||
import {
|
||||
InviteUserRepository,
|
||||
InviteUserSpaceRepository,
|
||||
} from '@app/common/modules/Invite-user/repositiories';
|
||||
import { PermissionTypeRepository } from '@app/common/modules/permission/repositories';
|
||||
import {
|
||||
PowerClampDailyRepository,
|
||||
PowerClampHourlyRepository,
|
||||
PowerClampMonthlyRepository,
|
||||
} from '@app/common/modules/power-clamp/repositories';
|
||||
import { ProductRepository } from '@app/common/modules/product/repositories';
|
||||
import { ProjectRepository } from '@app/common/modules/project/repositiories';
|
||||
import { RegionRepository } from '@app/common/modules/region/repositories';
|
||||
import { RoleTypeRepository } from '@app/common/modules/role-type/repositories';
|
||||
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
||||
import {
|
||||
SceneIconRepository,
|
||||
SceneRepository,
|
||||
} from '@app/common/modules/scene/repositories';
|
||||
import {
|
||||
InviteSpaceRepository,
|
||||
SpaceLinkRepository,
|
||||
SpaceProductAllocationRepository,
|
||||
SpaceRepository,
|
||||
} from '@app/common/modules/space';
|
||||
import {
|
||||
SpaceModelProductAllocationRepoitory,
|
||||
SpaceModelRepository,
|
||||
SubspaceModelProductAllocationRepoitory,
|
||||
SubspaceModelRepository,
|
||||
} from '@app/common/modules/space-model';
|
||||
import {
|
||||
SubspaceProductAllocationRepository,
|
||||
SubspaceRepository,
|
||||
} from '@app/common/modules/space/repositories/subspace.repository';
|
||||
import { NewTagRepository } from '@app/common/modules/tag/repositories/tag-repository';
|
||||
import { TimeZoneRepository } from '@app/common/modules/timezone/repositories';
|
||||
import {
|
||||
UserRepository,
|
||||
UserSpaceRepository,
|
||||
} from '@app/common/modules/user/repositories';
|
||||
import { EmailService } from '@app/common/util/email.service';
|
||||
import { CommunityModule } from 'src/community/community.module';
|
||||
import { CommunityService } from 'src/community/services';
|
||||
import { DeviceService } from 'src/device/services';
|
||||
import { ProjectUserService } from 'src/project/services/project-user.service';
|
||||
import { SceneService } from 'src/scene/services';
|
||||
import {
|
||||
SpaceModelService,
|
||||
SubSpaceModelService,
|
||||
} from 'src/space-model/services';
|
||||
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 {
|
||||
SpaceLinkService,
|
||||
SpaceService,
|
||||
@ -21,69 +78,11 @@ import {
|
||||
SubSpaceService,
|
||||
ValidationService,
|
||||
} from 'src/space/services';
|
||||
import { CommunityService } from 'src/community/services';
|
||||
import {
|
||||
InviteSpaceRepository,
|
||||
SpaceLinkRepository,
|
||||
SpaceProductAllocationRepository,
|
||||
SpaceRepository,
|
||||
TagRepository,
|
||||
} from '@app/common/modules/space';
|
||||
import { SpaceModelRepository } from '@app/common/modules/space-model';
|
||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||
import { ProjectRepository } from '@app/common/modules/project/repositiories';
|
||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||
import { UserService, UserSpaceService } from 'src/users/services';
|
||||
import { UserDevicePermissionService } from 'src/user-device-permission/services';
|
||||
import {
|
||||
DeviceRepository,
|
||||
DeviceUserPermissionRepository,
|
||||
} from '@app/common/modules/device/repositories';
|
||||
import { PermissionTypeRepository } from '@app/common/modules/permission/repositories';
|
||||
import { ProjectUserService } from 'src/project/services/project-user.service';
|
||||
import { RoleTypeRepository } from '@app/common/modules/role-type/repositories';
|
||||
import { RegionRepository } from '@app/common/modules/region/repositories';
|
||||
import { TimeZoneRepository } from '@app/common/modules/timezone/repositories';
|
||||
import { CommunityModule } from 'src/community/community.module';
|
||||
import { TagService as NewTagService } from 'src/tags/services';
|
||||
import { TagService } from 'src/space/services/tag';
|
||||
import {
|
||||
SpaceModelService,
|
||||
SubSpaceModelService,
|
||||
} from 'src/space-model/services';
|
||||
import { SpaceProductAllocationService } from 'src/space/services/space-product-allocation.service';
|
||||
import {
|
||||
SubspaceProductAllocationRepository,
|
||||
SubspaceRepository,
|
||||
} from '@app/common/modules/space/repositories/subspace.repository';
|
||||
import { SubspaceProductAllocationService } from 'src/space/services/subspace/subspace-product-allocation.service';
|
||||
import {
|
||||
SpaceModelProductAllocationRepoitory,
|
||||
SubspaceModelProductAllocationRepoitory,
|
||||
SubspaceModelRepository,
|
||||
} from '@app/common/modules/space-model';
|
||||
import { NewTagRepository } from '@app/common/modules/tag/repositories/tag-repository';
|
||||
import { ProductRepository } from '@app/common/modules/product/repositories';
|
||||
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 { DeviceService } from 'src/device/services';
|
||||
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
||||
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
|
||||
import { SceneService } from 'src/scene/services';
|
||||
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
|
||||
import {
|
||||
SceneIconRepository,
|
||||
SceneRepository,
|
||||
} from '@app/common/modules/scene/repositories';
|
||||
import { AutomationRepository } from '@app/common/modules/automation/repositories';
|
||||
import {
|
||||
PowerClampHourlyRepository,
|
||||
PowerClampDailyRepository,
|
||||
PowerClampMonthlyRepository,
|
||||
} from '@app/common/modules/power-clamp/repositories';
|
||||
import { PowerClampService } from '@app/common/helper/services/power.clamp.service';
|
||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
||||
import { TagService as NewTagService } from 'src/tags/services';
|
||||
import { UserDevicePermissionService } from 'src/user-device-permission/services';
|
||||
import { UserService, UserSpaceService } from 'src/users/services';
|
||||
|
||||
@Module({
|
||||
imports: [ConfigModule, InviteUserRepositoryModule, CommunityModule],
|
||||
@ -124,7 +123,6 @@ import { OccupancyService } from '@app/common/helper/services/occupancy.service'
|
||||
SpaceProductAllocationService,
|
||||
SpaceLinkRepository,
|
||||
SubspaceRepository,
|
||||
TagService,
|
||||
SubspaceDeviceService,
|
||||
SubspaceProductAllocationService,
|
||||
SpaceModelRepository,
|
||||
@ -135,7 +133,6 @@ import { OccupancyService } from '@app/common/helper/services/occupancy.service'
|
||||
SpaceModelProductAllocationService,
|
||||
SpaceProductAllocationRepository,
|
||||
SubspaceProductAllocationRepository,
|
||||
TagRepository,
|
||||
SubspaceModelRepository,
|
||||
SubspaceModelProductAllocationService,
|
||||
SpaceModelProductAllocationRepoitory,
|
||||
@ -154,6 +151,7 @@ import { OccupancyService } from '@app/common/helper/services/occupancy.service'
|
||||
PowerClampMonthlyRepository,
|
||||
SqlLoaderService,
|
||||
OccupancyService,
|
||||
AqiDataService,
|
||||
],
|
||||
exports: [InviteUserService],
|
||||
})
|
||||
|
@ -1,36 +1,42 @@
|
||||
import {
|
||||
Injectable,
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
BadRequestException,
|
||||
} from '@nestjs/common';
|
||||
import { AddUserInvitationDto } from '../dtos';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import { RoleType } from '@app/common/constants/role.type.enum';
|
||||
import { UserStatusEnum } from '@app/common/constants/user-status.enum';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||
import { generateRandomString } from '@app/common/helper/randomString';
|
||||
import { EntityManager, In, IsNull, Not, QueryRunner } from 'typeorm';
|
||||
import { DataSource } from 'typeorm';
|
||||
import { UserEntity } from '@app/common/modules/user/entities';
|
||||
import { RoleType } from '@app/common/constants/role.type.enum';
|
||||
import { InviteUserEntity } from '@app/common/modules/Invite-user/entities';
|
||||
import {
|
||||
InviteUserRepository,
|
||||
InviteUserSpaceRepository,
|
||||
} from '@app/common/modules/Invite-user/repositiories';
|
||||
import { CheckEmailDto } from '../dtos/check-email.dto';
|
||||
import { RoleTypeRepository } from '@app/common/modules/role-type/repositories';
|
||||
import { SpaceRepository } from '@app/common/modules/space';
|
||||
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
|
||||
import { UserEntity } from '@app/common/modules/user/entities';
|
||||
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||
import { EmailService } from '@app/common/util/email.service';
|
||||
import { SpaceRepository } from '@app/common/modules/space';
|
||||
import { ActivateCodeDto } from '../dtos/active-code.dto';
|
||||
import { UserSpaceService } from 'src/users/services';
|
||||
import {
|
||||
BadRequestException,
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
Injectable,
|
||||
} from '@nestjs/common';
|
||||
import { SpaceUserService } from 'src/space/services';
|
||||
import { UserSpaceService } from 'src/users/services';
|
||||
import {
|
||||
DataSource,
|
||||
EntityManager,
|
||||
In,
|
||||
IsNull,
|
||||
Not,
|
||||
QueryRunner,
|
||||
} from 'typeorm';
|
||||
import { AddUserInvitationDto } from '../dtos';
|
||||
import { ActivateCodeDto } from '../dtos/active-code.dto';
|
||||
import { CheckEmailDto } from '../dtos/check-email.dto';
|
||||
import {
|
||||
DisableUserInvitationDto,
|
||||
UpdateUserInvitationDto,
|
||||
} from '../dtos/update.invite-user.dto';
|
||||
import { RoleTypeRepository } from '@app/common/modules/role-type/repositories';
|
||||
import { InviteUserEntity } from '@app/common/modules/Invite-user/entities';
|
||||
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
|
||||
|
||||
@Injectable()
|
||||
export class InviteUserService {
|
||||
@ -658,12 +664,12 @@ export class InviteUserService {
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
await this.emailService.sendEmailWithTemplate(
|
||||
userData.email,
|
||||
userData.firstName,
|
||||
disable,
|
||||
false,
|
||||
);
|
||||
await this.emailService.sendEmailWithTemplate({
|
||||
email: userData.email,
|
||||
name: userData.firstName,
|
||||
isEnable: !disable,
|
||||
isDelete: false,
|
||||
});
|
||||
await queryRunner.commitTransaction();
|
||||
|
||||
return new SuccessResponseDto({
|
||||
@ -797,12 +803,12 @@ export class InviteUserService {
|
||||
{ isActive: false },
|
||||
);
|
||||
}
|
||||
await this.emailService.sendEmailWithTemplate(
|
||||
userData.email,
|
||||
userData.firstName,
|
||||
false,
|
||||
true,
|
||||
);
|
||||
await this.emailService.sendEmailWithTemplate({
|
||||
email: userData.email,
|
||||
name: userData.firstName,
|
||||
isEnable: false,
|
||||
isDelete: true,
|
||||
});
|
||||
await queryRunner.commitTransaction();
|
||||
|
||||
return new SuccessResponseDto({
|
||||
|
@ -57,7 +57,8 @@ async function bootstrap() {
|
||||
logger.error('Seeding failed!', error.stack || error);
|
||||
}
|
||||
|
||||
logger.log('Starting auth at port ...', process.env.PORT || 4000);
|
||||
await app.listen(process.env.PORT || 4000);
|
||||
const port = process.env.PORT || 3000;
|
||||
logger.log(`Starting application on port ${port}...`);
|
||||
await app.listen(port, '0.0.0.0');
|
||||
}
|
||||
bootstrap();
|
||||
|
@ -1,25 +1,24 @@
|
||||
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 { Controller, Get, Param, Query, UseGuards } from '@nestjs/common';
|
||||
import {
|
||||
ApiTags,
|
||||
ApiBearerAuth,
|
||||
ApiOperation,
|
||||
ApiParam,
|
||||
ApiQuery,
|
||||
ApiTags,
|
||||
} from '@nestjs/swagger';
|
||||
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
|
||||
import { ControllerRoute } from '@app/common/constants/controller-route';
|
||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||
import { PowerClampService } from '../services/power-clamp.service';
|
||||
import {
|
||||
GetPowerClampBySpaceDto,
|
||||
GetPowerClampDto,
|
||||
} from '../dto/get-power-clamp.dto';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import {
|
||||
PowerClampParamsDto,
|
||||
ResourceParamsDto,
|
||||
} from '../dto/power-clamp-params.dto';
|
||||
|
||||
import { PowerClampService } from '../services/power-clamp.service';
|
||||
@ApiTags('Power Clamp Module')
|
||||
@Controller({
|
||||
version: EnableDisableStatusEnum.ENABLED,
|
||||
@ -27,7 +26,6 @@ import {
|
||||
})
|
||||
export class PowerClampController {
|
||||
constructor(private readonly powerClampService: PowerClampService) {}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get(':powerClampUuid/historical')
|
||||
|
@ -1,13 +1,52 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { PowerClampService as PowerClamp } from './services/power-clamp.service';
|
||||
import { PowerClampController } from './controllers';
|
||||
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
|
||||
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
||||
import { PowerClampService } from '@app/common/helper/services/power.clamp.service';
|
||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||
import { AutomationRepository } from '@app/common/modules/automation/repositories';
|
||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
|
||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||
import {
|
||||
PowerClampDailyRepository,
|
||||
PowerClampHourlyRepository,
|
||||
PowerClampMonthlyRepository,
|
||||
} from '@app/common/modules/power-clamp/repositories';
|
||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||
import { ProductRepository } from '@app/common/modules/product/repositories';
|
||||
import { ProjectRepository } from '@app/common/modules/project/repositiories';
|
||||
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
||||
import {
|
||||
SceneIconRepository,
|
||||
SceneRepository,
|
||||
} from '@app/common/modules/scene/repositories';
|
||||
import {
|
||||
InviteSpaceRepository,
|
||||
SpaceLinkRepository,
|
||||
SpaceProductAllocationRepository,
|
||||
SpaceRepository,
|
||||
} from '@app/common/modules/space';
|
||||
import {
|
||||
SpaceModelProductAllocationRepoitory,
|
||||
SpaceModelRepository,
|
||||
SubspaceModelProductAllocationRepoitory,
|
||||
SubspaceModelRepository,
|
||||
} from '@app/common/modules/space-model';
|
||||
import {
|
||||
SubspaceProductAllocationRepository,
|
||||
SubspaceRepository,
|
||||
} from '@app/common/modules/space/repositories/subspace.repository';
|
||||
import { NewTagRepository } from '@app/common/modules/tag/repositories/tag-repository';
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { CommunityService } from 'src/community/services';
|
||||
import { DeviceService } from 'src/device/services';
|
||||
import { SceneService } from 'src/scene/services';
|
||||
import {
|
||||
SpaceModelService,
|
||||
SubSpaceModelService,
|
||||
} from 'src/space-model/services';
|
||||
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 {
|
||||
SpaceDeviceService,
|
||||
SpaceLinkService,
|
||||
@ -16,51 +55,12 @@ import {
|
||||
SubSpaceService,
|
||||
ValidationService,
|
||||
} from 'src/space/services';
|
||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||
import { DeviceService } from 'src/device/services';
|
||||
import {
|
||||
InviteSpaceRepository,
|
||||
SpaceLinkRepository,
|
||||
SpaceProductAllocationRepository,
|
||||
SpaceRepository,
|
||||
TagRepository,
|
||||
} from '@app/common/modules/space';
|
||||
import { CommunityService } from 'src/community/services';
|
||||
import { ProjectRepository } from '@app/common/modules/project/repositiories';
|
||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||
import {
|
||||
SpaceModelProductAllocationRepoitory,
|
||||
SpaceModelRepository,
|
||||
SubspaceModelProductAllocationRepoitory,
|
||||
SubspaceModelRepository,
|
||||
} from '@app/common/modules/space-model';
|
||||
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
||||
import { ProductRepository } from '@app/common/modules/product/repositories';
|
||||
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
|
||||
import { SceneService } from 'src/scene/services';
|
||||
import { PowerClampService } from '@app/common/helper/services/power.clamp.service';
|
||||
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
|
||||
import {
|
||||
SceneIconRepository,
|
||||
SceneRepository,
|
||||
} from '@app/common/modules/scene/repositories';
|
||||
import { AutomationRepository } from '@app/common/modules/automation/repositories';
|
||||
import { TagService } from 'src/tags/services';
|
||||
import {
|
||||
SpaceModelService,
|
||||
SubSpaceModelService,
|
||||
} from 'src/space-model/services';
|
||||
import { SpaceProductAllocationService } from 'src/space/services/space-product-allocation.service';
|
||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||
import {
|
||||
SubspaceProductAllocationRepository,
|
||||
SubspaceRepository,
|
||||
} from '@app/common/modules/space/repositories/subspace.repository';
|
||||
import { SubspaceProductAllocationService } from 'src/space/services/subspace/subspace-product-allocation.service';
|
||||
import { NewTagRepository } from '@app/common/modules/tag/repositories/tag-repository';
|
||||
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 { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
||||
import { TagService } from 'src/tags/services';
|
||||
import { PowerClampController } from './controllers';
|
||||
import { PowerClampService as PowerClamp } from './services/power-clamp.service';
|
||||
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
||||
@Module({
|
||||
imports: [ConfigModule],
|
||||
controllers: [PowerClampController],
|
||||
@ -105,12 +105,12 @@ import { OccupancyService } from '@app/common/helper/services/occupancy.service'
|
||||
SpaceModelProductAllocationService,
|
||||
SpaceProductAllocationRepository,
|
||||
SubspaceProductAllocationRepository,
|
||||
TagRepository,
|
||||
SubspaceModelRepository,
|
||||
SubspaceModelProductAllocationService,
|
||||
SpaceModelProductAllocationRepoitory,
|
||||
SubspaceModelProductAllocationRepoitory,
|
||||
OccupancyService,
|
||||
AqiDataService,
|
||||
],
|
||||
exports: [PowerClamp],
|
||||
})
|
||||
|
@ -23,10 +23,10 @@ import { SpaceDeviceService } from 'src/space/services';
|
||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||
import { DataSource } from 'typeorm';
|
||||
import { SQL_PROCEDURES_PATH } from '@app/common/constants/sql-query-path';
|
||||
import { filterByMonth, toMMYYYY } from '@app/common/helper/date-format';
|
||||
import { ProductType } from '@app/common/constants/product-type.enum';
|
||||
import { CommunityService } from 'src/community/services';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import { filterByMonth, toMMYYYY } from '@app/common/helper/date-format';
|
||||
|
||||
@Injectable()
|
||||
export class PowerClampService {
|
||||
|
@ -1,24 +1,58 @@
|
||||
import { Global, Module } from '@nestjs/common';
|
||||
import { CqrsModule } from '@nestjs/cqrs';
|
||||
import { ProjectController } from './controllers';
|
||||
import { ProjectService } from './services';
|
||||
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 { PowerClampService } from '@app/common/helper/services/power.clamp.service';
|
||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||
import { AutomationRepository } from '@app/common/modules/automation/repositories';
|
||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
|
||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||
import { InviteUserRepository } from '@app/common/modules/Invite-user/repositiories';
|
||||
import {
|
||||
PowerClampDailyRepository,
|
||||
PowerClampHourlyRepository,
|
||||
PowerClampMonthlyRepository,
|
||||
} from '@app/common/modules/power-clamp/repositories';
|
||||
import { ProductRepository } from '@app/common/modules/product/repositories';
|
||||
import { ProjectRepository } from '@app/common/modules/project/repositiories';
|
||||
import { CreateOrphanSpaceHandler } from './handler';
|
||||
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
||||
import {
|
||||
SceneIconRepository,
|
||||
SceneRepository,
|
||||
} from '@app/common/modules/scene/repositories';
|
||||
import {
|
||||
InviteSpaceRepository,
|
||||
SpaceLinkRepository,
|
||||
SpaceProductAllocationRepository,
|
||||
SpaceRepository,
|
||||
TagRepository,
|
||||
} from '@app/common/modules/space';
|
||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||
import { InviteUserRepository } from '@app/common/modules/Invite-user/repositiories';
|
||||
import { ProjectUserController } from './controllers/project-user.controller';
|
||||
import { ProjectUserService } from './services/project-user.service';
|
||||
import {
|
||||
SpaceModelProductAllocationRepoitory,
|
||||
SpaceModelRepository,
|
||||
SubspaceModelProductAllocationRepoitory,
|
||||
SubspaceModelRepository,
|
||||
} from '@app/common/modules/space-model';
|
||||
import {
|
||||
SubspaceProductAllocationRepository,
|
||||
SubspaceRepository,
|
||||
} from '@app/common/modules/space/repositories/subspace.repository';
|
||||
import { NewTagRepository } from '@app/common/modules/tag/repositories/tag-repository';
|
||||
import {
|
||||
UserRepository,
|
||||
UserSpaceRepository,
|
||||
} from '@app/common/modules/user/repositories';
|
||||
import { Global, Module } from '@nestjs/common';
|
||||
import { CqrsModule } from '@nestjs/cqrs';
|
||||
import { CommunityService } from 'src/community/services';
|
||||
import { DeviceService } from 'src/device/services';
|
||||
import { SceneService } from 'src/scene/services';
|
||||
import {
|
||||
SpaceModelService,
|
||||
SubSpaceModelService,
|
||||
} from 'src/space-model/services';
|
||||
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 {
|
||||
SpaceLinkService,
|
||||
SpaceService,
|
||||
@ -26,48 +60,14 @@ import {
|
||||
SubSpaceService,
|
||||
ValidationService,
|
||||
} from 'src/space/services';
|
||||
import { TagService } from 'src/tags/services';
|
||||
import {
|
||||
SpaceModelService,
|
||||
SubSpaceModelService,
|
||||
} from 'src/space-model/services';
|
||||
import { DeviceService } from 'src/device/services';
|
||||
import { SpaceProductAllocationService } from 'src/space/services/space-product-allocation.service';
|
||||
import {
|
||||
SubspaceProductAllocationRepository,
|
||||
SubspaceRepository,
|
||||
} from '@app/common/modules/space/repositories/subspace.repository';
|
||||
import { SubspaceProductAllocationService } from 'src/space/services/subspace/subspace-product-allocation.service';
|
||||
import { CommunityService } from 'src/community/services';
|
||||
import {
|
||||
SpaceModelProductAllocationRepoitory,
|
||||
SpaceModelRepository,
|
||||
SubspaceModelProductAllocationRepoitory,
|
||||
SubspaceModelRepository,
|
||||
} from '@app/common/modules/space-model';
|
||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||
import { NewTagRepository } from '@app/common/modules/tag/repositories/tag-repository';
|
||||
import { ProductRepository } from '@app/common/modules/product/repositories';
|
||||
import { SpaceModelProductAllocationService } from 'src/space-model/services/space-model-product-allocation.service';
|
||||
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
||||
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
|
||||
import { SceneService } from 'src/scene/services';
|
||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||
import { SubspaceModelProductAllocationService } from 'src/space-model/services/subspace/subspace-model-product-allocation.service';
|
||||
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
|
||||
import {
|
||||
SceneIconRepository,
|
||||
SceneRepository,
|
||||
} from '@app/common/modules/scene/repositories';
|
||||
import { AutomationRepository } from '@app/common/modules/automation/repositories';
|
||||
import { PowerClampService } from '@app/common/helper/services/power.clamp.service';
|
||||
import {
|
||||
PowerClampDailyRepository,
|
||||
PowerClampHourlyRepository,
|
||||
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 { TagService } from 'src/tags/services';
|
||||
import { ProjectController } from './controllers';
|
||||
import { ProjectUserController } from './controllers/project-user.controller';
|
||||
import { CreateOrphanSpaceHandler } from './handler';
|
||||
import { ProjectService } from './services';
|
||||
import { ProjectUserService } from './services/project-user.service';
|
||||
|
||||
const CommandHandlers = [CreateOrphanSpaceHandler];
|
||||
|
||||
@ -111,7 +111,6 @@ const CommandHandlers = [CreateOrphanSpaceHandler];
|
||||
DeviceStatusFirebaseService,
|
||||
SceneService,
|
||||
TuyaService,
|
||||
TagRepository,
|
||||
SubspaceModelRepository,
|
||||
SubspaceModelProductAllocationService,
|
||||
SpaceModelProductAllocationRepoitory,
|
||||
@ -126,6 +125,7 @@ const CommandHandlers = [CreateOrphanSpaceHandler];
|
||||
PowerClampMonthlyRepository,
|
||||
SqlLoaderService,
|
||||
OccupancyService,
|
||||
AqiDataService,
|
||||
],
|
||||
exports: [ProjectService, CqrsModule],
|
||||
})
|
||||
|
@ -1,4 +1,17 @@
|
||||
import { ORPHAN_COMMUNITY_NAME } from '@app/common/constants/orphan-constant';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import { PageResponse } from '@app/common/dto/pagination.response.dto';
|
||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||
import {
|
||||
TypeORMCustomModel,
|
||||
TypeORMCustomModelFindAllQuery,
|
||||
} from '@app/common/models/typeOrmCustom.model';
|
||||
import { ProjectDto } from '@app/common/modules/project/dtos';
|
||||
import { ProjectEntity } from '@app/common/modules/project/entities';
|
||||
import { ProjectRepository } from '@app/common/modules/project/repositiories';
|
||||
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
|
||||
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||
import { format } from '@fast-csv/format';
|
||||
import {
|
||||
forwardRef,
|
||||
HttpException,
|
||||
@ -6,24 +19,11 @@ import {
|
||||
Inject,
|
||||
Injectable,
|
||||
} from '@nestjs/common';
|
||||
import { CreateProjectDto, GetProjectParam } from '../dto';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||
import { ProjectEntity } from '@app/common/modules/project/entities';
|
||||
import {
|
||||
TypeORMCustomModel,
|
||||
TypeORMCustomModelFindAllQuery,
|
||||
} from '@app/common/models/typeOrmCustom.model';
|
||||
import { ProjectDto } from '@app/common/modules/project/dtos';
|
||||
import { PageResponse } from '@app/common/dto/pagination.response.dto';
|
||||
import { CommandBus } from '@nestjs/cqrs';
|
||||
import { CreateOrphanSpaceCommand } from '../command/create-orphan-space-command';
|
||||
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||
import { format } from '@fast-csv/format';
|
||||
import { PassThrough } from 'stream';
|
||||
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
|
||||
import { SpaceService } from 'src/space/services';
|
||||
import { ORPHAN_COMMUNITY_NAME } from '@app/common/constants/orphan-constant';
|
||||
import { PassThrough } from 'stream';
|
||||
import { CreateOrphanSpaceCommand } from '../command/create-orphan-space-command';
|
||||
import { CreateProjectDto, GetProjectParam } from '../dto';
|
||||
|
||||
@Injectable()
|
||||
export class ProjectService {
|
||||
@ -236,11 +236,11 @@ export class ProjectService {
|
||||
'communities.spaces.parent',
|
||||
'communities.spaces.productAllocations',
|
||||
'communities.spaces.productAllocations.product',
|
||||
'communities.spaces.productAllocations.tags',
|
||||
'communities.spaces.productAllocations.tag',
|
||||
'communities.spaces.subspaces',
|
||||
'communities.spaces.subspaces.productAllocations',
|
||||
'communities.spaces.subspaces.productAllocations.product',
|
||||
'communities.spaces.subspaces.productAllocations.tags',
|
||||
'communities.spaces.subspaces.productAllocations.tag',
|
||||
],
|
||||
});
|
||||
|
||||
@ -303,52 +303,38 @@ export class ProjectService {
|
||||
if (subspace.disabled) continue;
|
||||
|
||||
for (const productAllocation of subspace.productAllocations || []) {
|
||||
for (const tag of productAllocation.tags || []) {
|
||||
csvStream.write({
|
||||
'Device ID': '',
|
||||
'Community Name': space.community?.name || '',
|
||||
'Space Name': space.spaceName,
|
||||
'Space Location': spaceLocation,
|
||||
'Subspace Name': subspace.subspaceName || '',
|
||||
Tag: tag.name,
|
||||
'Product Name': productAllocation.product.name || '',
|
||||
'Community UUID': space.community?.uuid || '',
|
||||
'Space UUID': space.uuid,
|
||||
'Subspace UUID': subspace.uuid,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const productAllocation of space.productAllocations || []) {
|
||||
for (const tag of productAllocation.tags || []) {
|
||||
csvStream.write({
|
||||
'Device ID': '',
|
||||
'Community Name': space.community?.name || '',
|
||||
'Space Name': space.spaceName,
|
||||
'Space Location': spaceLocation,
|
||||
'Subspace Name': '',
|
||||
Tag: tag.name,
|
||||
'Subspace Name': subspace.subspaceName || '',
|
||||
Tag: productAllocation.tag.name,
|
||||
'Product Name': productAllocation.product.name || '',
|
||||
'Community UUID': space.community?.uuid || '',
|
||||
'Space UUID': space.uuid,
|
||||
'Subspace UUID': '',
|
||||
'Subspace UUID': subspace.uuid,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
for (const productAllocation of space.productAllocations || []) {
|
||||
csvStream.write({
|
||||
'Device ID': '',
|
||||
'Community Name': space.community?.name || '',
|
||||
'Space Name': space.spaceName,
|
||||
'Space Location': spaceLocation,
|
||||
'Subspace Name': '',
|
||||
Tag: productAllocation.tag.name,
|
||||
'Product Name': productAllocation.product.name || '',
|
||||
'Community UUID': space.community?.uuid || '',
|
||||
'Space UUID': space.uuid,
|
||||
'Subspace UUID': '',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
csvStream.end();
|
||||
return stream;
|
||||
}
|
||||
|
||||
getSpaceLocation(space: SpaceEntity): string {
|
||||
const names = [];
|
||||
let current = space.parent;
|
||||
while (current) {
|
||||
names.unshift(current.spaceName);
|
||||
current = current.parent;
|
||||
}
|
||||
return names.join(' > ');
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
|
||||
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||
import {
|
||||
AddScheduleDto,
|
||||
EnableScheduleDto,
|
||||
@ -11,14 +11,14 @@ import {
|
||||
getDeviceScheduleInterface,
|
||||
} from '../interfaces/get.schedule.interface';
|
||||
|
||||
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||
import { ProductType } from '@app/common/constants/product-type.enum';
|
||||
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
||||
import { convertTimestampToDubaiTime } from '@app/common/helper/convertTimestampToDubaiTime';
|
||||
import {
|
||||
getEnabledDays,
|
||||
getScheduleStatus,
|
||||
} from '@app/common/helper/getScheduleStatus';
|
||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||
|
||||
@Injectable()
|
||||
export class ScheduleService {
|
||||
@ -57,7 +57,8 @@ export class ScheduleService {
|
||||
deviceDetails.productDevice.prodType !== ProductType.ONE_1TG &&
|
||||
deviceDetails.productDevice.prodType !== ProductType.TWO_2TG &&
|
||||
deviceDetails.productDevice.prodType !== ProductType.THREE_3TG &&
|
||||
deviceDetails.productDevice.prodType !== ProductType.GD
|
||||
deviceDetails.productDevice.prodType !== ProductType.GD &&
|
||||
deviceDetails.productDevice.prodType !== ProductType.CUR_2
|
||||
) {
|
||||
throw new HttpException(
|
||||
'This device is not supported for schedule',
|
||||
@ -115,7 +116,8 @@ export class ScheduleService {
|
||||
deviceDetails.productDevice.prodType !== ProductType.ONE_1TG &&
|
||||
deviceDetails.productDevice.prodType !== ProductType.TWO_2TG &&
|
||||
deviceDetails.productDevice.prodType !== ProductType.THREE_3TG &&
|
||||
deviceDetails.productDevice.prodType !== ProductType.GD
|
||||
deviceDetails.productDevice.prodType !== ProductType.GD &&
|
||||
deviceDetails.productDevice.prodType !== ProductType.CUR_2
|
||||
) {
|
||||
throw new HttpException(
|
||||
'This device is not supported for schedule',
|
||||
@ -169,7 +171,8 @@ export class ScheduleService {
|
||||
deviceDetails.productDevice.prodType !== ProductType.ONE_1TG &&
|
||||
deviceDetails.productDevice.prodType !== ProductType.TWO_2TG &&
|
||||
deviceDetails.productDevice.prodType !== ProductType.THREE_3TG &&
|
||||
deviceDetails.productDevice.prodType !== ProductType.GD
|
||||
deviceDetails.productDevice.prodType !== ProductType.GD &&
|
||||
deviceDetails.productDevice.prodType !== ProductType.CUR_2
|
||||
) {
|
||||
throw new HttpException(
|
||||
'This device is not supported for schedule',
|
||||
@ -237,7 +240,8 @@ export class ScheduleService {
|
||||
deviceDetails.productDevice.prodType !== ProductType.ONE_1TG &&
|
||||
deviceDetails.productDevice.prodType !== ProductType.TWO_2TG &&
|
||||
deviceDetails.productDevice.prodType !== ProductType.THREE_3TG &&
|
||||
deviceDetails.productDevice.prodType !== ProductType.GD
|
||||
deviceDetails.productDevice.prodType !== ProductType.GD &&
|
||||
deviceDetails.productDevice.prodType !== ProductType.CUR_2
|
||||
) {
|
||||
throw new HttpException(
|
||||
'This device is not supported for schedule',
|
||||
@ -323,7 +327,8 @@ export class ScheduleService {
|
||||
deviceDetails.productDevice.prodType !== ProductType.ONE_1TG &&
|
||||
deviceDetails.productDevice.prodType !== ProductType.TWO_2TG &&
|
||||
deviceDetails.productDevice.prodType !== ProductType.THREE_3TG &&
|
||||
deviceDetails.productDevice.prodType !== ProductType.GD
|
||||
deviceDetails.productDevice.prodType !== ProductType.GD &&
|
||||
deviceDetails.productDevice.prodType !== ProductType.CUR_2
|
||||
) {
|
||||
throw new HttpException(
|
||||
'This device is not supported for schedule',
|
||||
|
@ -1,7 +1,6 @@
|
||||
export * from './create-space-model.dto';
|
||||
export * from './link-space-model.dto';
|
||||
export * from './project-param.dto';
|
||||
export * from './update-space-model.dto';
|
||||
export * from './space-model-param';
|
||||
export * from './subspaces-model-dtos';
|
||||
export * from './tag-model-dtos';
|
||||
export * from './link-space-model.dto';
|
||||
export * from './update-space-model.dto';
|
||||
|
@ -1,14 +1,14 @@
|
||||
import { ModifyAction } from '@app/common/constants/modify-action.enum';
|
||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { Type } from 'class-transformer';
|
||||
import {
|
||||
IsString,
|
||||
IsOptional,
|
||||
IsArray,
|
||||
ValidateNested,
|
||||
IsEnum,
|
||||
IsOptional,
|
||||
IsString,
|
||||
ValidateNested,
|
||||
} from 'class-validator';
|
||||
import { ModifyTagModelDto } from '../tag-model-dtos';
|
||||
import { ModifyAction } from '@app/common/constants/modify-action.enum';
|
||||
import { ModifyTagDto } from 'src/space/dtos/tag/modify-tag.dto';
|
||||
|
||||
export class ModifySubspaceModelDto {
|
||||
@ApiProperty({
|
||||
@ -37,11 +37,11 @@ export class ModifySubspaceModelDto {
|
||||
@ApiPropertyOptional({
|
||||
description:
|
||||
'List of tag modifications (add/update/delete) for the subspace',
|
||||
type: [ModifyTagModelDto],
|
||||
type: [ModifyTagDto],
|
||||
})
|
||||
@IsOptional()
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => ModifyTagModelDto)
|
||||
tags?: ModifyTagModelDto[];
|
||||
@Type(() => ModifyTagDto)
|
||||
tags?: ModifyTagDto[];
|
||||
}
|
||||
|
@ -1,28 +0,0 @@
|
||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { IsNotEmpty, IsOptional, IsString } from 'class-validator';
|
||||
|
||||
export class CreateTagModelDto {
|
||||
@ApiProperty({
|
||||
description: 'Tag associated with the space or subspace models',
|
||||
example: 'Temperature Control',
|
||||
})
|
||||
@IsNotEmpty()
|
||||
@IsString()
|
||||
tag: string;
|
||||
|
||||
@ApiPropertyOptional({
|
||||
description: 'UUID of the tag model (required for update/delete)',
|
||||
example: '123e4567-e89b-12d3-a456-426614174000',
|
||||
})
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
uuid?: string;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'ID of the product associated with the tag',
|
||||
example: '123e4567-e89b-12d3-a456-426614174000',
|
||||
})
|
||||
@IsNotEmpty()
|
||||
@IsString()
|
||||
productUuid: string;
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
export * from './create-tag-model.dto';
|
||||
export * from './update-tag-model.dto';
|
||||
export * from './modify-tag-model.dto';
|
@ -1,46 +0,0 @@
|
||||
import { ModifyAction } from '@app/common/constants/modify-action.enum';
|
||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { IsString, IsOptional, IsEnum, IsUUID } from 'class-validator';
|
||||
|
||||
export class ModifyTagModelDto {
|
||||
@ApiProperty({
|
||||
description: 'Action to perform: add, update, or delete',
|
||||
example: ModifyAction.ADD,
|
||||
})
|
||||
@IsEnum(ModifyAction)
|
||||
action: ModifyAction;
|
||||
|
||||
@ApiPropertyOptional({
|
||||
description: 'UUID of the new tag',
|
||||
example: '123e4567-e89b-12d3-a456-426614174000',
|
||||
})
|
||||
@IsOptional()
|
||||
@IsUUID()
|
||||
newTagUuid: string;
|
||||
|
||||
@ApiPropertyOptional({
|
||||
description:
|
||||
'UUID of an existing tag (required for update/delete, optional for add)',
|
||||
example: 'a1b2c3d4-5678-90ef-abcd-1234567890ef',
|
||||
})
|
||||
@IsOptional()
|
||||
@IsUUID()
|
||||
tagUuid?: string;
|
||||
|
||||
@ApiPropertyOptional({
|
||||
description: 'Name of the tag (required for add/update)',
|
||||
example: 'Temperature Sensor',
|
||||
})
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
name?: string;
|
||||
|
||||
@ApiPropertyOptional({
|
||||
description:
|
||||
'UUID of the product associated with the tag (required for add)',
|
||||
example: 'c789a91e-549a-4753-9006-02f89e8170e0',
|
||||
})
|
||||
@IsOptional()
|
||||
@IsUUID()
|
||||
productUuid?: string;
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsNotEmpty, IsOptional, IsString, IsUUID } from 'class-validator';
|
||||
|
||||
export class UpdateTagModelDto {
|
||||
@ApiProperty({
|
||||
description: 'UUID of the tag to be updated',
|
||||
example: '123e4567-e89b-12d3-a456-426614174000',
|
||||
})
|
||||
@IsNotEmpty()
|
||||
@IsUUID()
|
||||
uuid: string;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'Updated name of the tag',
|
||||
example: 'Updated Tag Name',
|
||||
required: false,
|
||||
})
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
tag?: string;
|
||||
}
|
@ -1,48 +1,13 @@
|
||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { IsArray, IsOptional, IsString, ValidateNested } from 'class-validator';
|
||||
import { CreateSubspaceModelDto } from './subspaces-model-dtos/create-subspace-model.dto';
|
||||
import { Type } from 'class-transformer';
|
||||
import { IsArray, IsOptional, IsString, ValidateNested } from 'class-validator';
|
||||
import {
|
||||
DeleteSubspaceModelDto,
|
||||
ModifySubspaceModelDto,
|
||||
UpdateSubspaceModelDto,
|
||||
} from './subspaces-model-dtos';
|
||||
import { ModifyTagModelDto } from './tag-model-dtos';
|
||||
|
||||
export class ModifySubspacesModelDto {
|
||||
@ApiProperty({
|
||||
description: 'List of subspaces to add',
|
||||
type: [CreateSubspaceModelDto],
|
||||
required: false,
|
||||
})
|
||||
@IsOptional()
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => CreateSubspaceModelDto)
|
||||
add?: CreateSubspaceModelDto[];
|
||||
|
||||
@ApiProperty({
|
||||
description: 'List of subspaces to add',
|
||||
type: [CreateSubspaceModelDto],
|
||||
required: false,
|
||||
})
|
||||
@IsOptional()
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => UpdateSubspaceModelDto)
|
||||
update?: UpdateSubspaceModelDto[];
|
||||
|
||||
@ApiProperty({
|
||||
description: 'List of subspaces to delete',
|
||||
type: [DeleteSubspaceModelDto],
|
||||
required: false,
|
||||
})
|
||||
@IsOptional()
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => DeleteSubspaceModelDto)
|
||||
delete?: DeleteSubspaceModelDto[];
|
||||
}
|
||||
import { CreateSubspaceModelDto } from './subspaces-model-dtos/create-subspace-model.dto';
|
||||
import { ModifyTagDto } from 'src/space/dtos/tag/modify-tag.dto';
|
||||
|
||||
export class UpdateSpaceModelDto {
|
||||
@ApiProperty({
|
||||
@ -66,11 +31,11 @@ export class UpdateSpaceModelDto {
|
||||
@ApiPropertyOptional({
|
||||
description:
|
||||
'List of tag modifications (add/update/delete) for the space model',
|
||||
type: [ModifyTagModelDto],
|
||||
type: [ModifyTagDto],
|
||||
})
|
||||
@IsOptional()
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => ModifyTagModelDto)
|
||||
tags?: ModifyTagModelDto[];
|
||||
@Type(() => ModifyTagDto)
|
||||
tags?: ModifyTagDto[];
|
||||
}
|
||||
|
@ -1,11 +1,11 @@
|
||||
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
|
||||
import { PropogateUpdateSpaceModelProductAllocationCommand } from '../commands';
|
||||
import { SpaceProductAllocationRepository } from '@app/common/modules/space';
|
||||
import { SubspaceModelProductAllocationRepoitory } from '@app/common/modules/space-model';
|
||||
import {
|
||||
SubspaceRepository,
|
||||
SubspaceProductAllocationRepository,
|
||||
SubspaceRepository,
|
||||
} from '@app/common/modules/space/repositories/subspace.repository';
|
||||
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
|
||||
import { PropogateUpdateSpaceModelProductAllocationCommand } from '../commands';
|
||||
|
||||
@CommandHandler(PropogateUpdateSpaceModelProductAllocationCommand)
|
||||
export class PropogateUpdateSpaceModelProductAllocationHandler
|
||||
@ -31,89 +31,89 @@ export class PropogateUpdateSpaceModelProductAllocationHandler
|
||||
|
||||
console.log(`Processing ${updatedAllocations.length} allocations...`);
|
||||
|
||||
for (const allocation of updatedAllocations) {
|
||||
try {
|
||||
if (allocation.allocation) {
|
||||
const spaceAllocations = await this.spaceProductRepository.find({
|
||||
where: { uuid: allocation.allocation.uuid },
|
||||
relations: ['tags'],
|
||||
});
|
||||
// for (const allocation of updatedAllocations) {
|
||||
// try {
|
||||
// if (allocation.allocation) {
|
||||
// const spaceAllocations = await this.spaceProductRepository.find({
|
||||
// where: { uuid: allocation.allocation.uuid },
|
||||
// relations: ['tags'],
|
||||
// });
|
||||
|
||||
if (!spaceAllocations || spaceAllocations.length === 0) {
|
||||
console.warn(
|
||||
`No space allocations found for UUID: ${allocation.allocation.uuid}`,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
// if (!spaceAllocations || spaceAllocations.length === 0) {
|
||||
// console.warn(
|
||||
// `No space allocations found for UUID: ${allocation.allocation.uuid}`,
|
||||
// );
|
||||
// continue;
|
||||
// }
|
||||
|
||||
if (allocation.tagsAdded?.length) {
|
||||
for (const spaceAllocation of spaceAllocations) {
|
||||
spaceAllocation.tags.push(...allocation.tagsAdded);
|
||||
}
|
||||
await this.spaceProductRepository.save(spaceAllocations);
|
||||
console.log(
|
||||
`Added tags to ${spaceAllocations.length} space allocations.`,
|
||||
);
|
||||
}
|
||||
// if (allocation.tagsAdded?.length) {
|
||||
// for (const spaceAllocation of spaceAllocations) {
|
||||
// spaceAllocation.tags.push(...allocation.tagsAdded);
|
||||
// }
|
||||
// await this.spaceProductRepository.save(spaceAllocations);
|
||||
// console.log(
|
||||
// `Added tags to ${spaceAllocations.length} space allocations.`,
|
||||
// );
|
||||
// }
|
||||
|
||||
if (allocation.tagsRemoved?.length) {
|
||||
const tagsToRemoveUUIDs = new Set(
|
||||
allocation.tagsRemoved.map((tag) => tag.uuid),
|
||||
);
|
||||
// if (allocation.tagsRemoved?.length) {
|
||||
// const tagsToRemoveUUIDs = new Set(
|
||||
// allocation.tagsRemoved.map((tag) => tag.uuid),
|
||||
// );
|
||||
|
||||
for (const spaceAllocation of spaceAllocations) {
|
||||
spaceAllocation.tags = spaceAllocation.tags.filter(
|
||||
(tag) => !tagsToRemoveUUIDs.has(tag.uuid),
|
||||
);
|
||||
}
|
||||
await this.spaceProductRepository.save(spaceAllocations);
|
||||
console.log(
|
||||
`Removed tags from ${spaceAllocations.length} space allocations.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
// for (const spaceAllocation of spaceAllocations) {
|
||||
// spaceAllocation.tags = spaceAllocation.tags.filter(
|
||||
// (tag) => !tagsToRemoveUUIDs.has(tag.uuid),
|
||||
// );
|
||||
// }
|
||||
// await this.spaceProductRepository.save(spaceAllocations);
|
||||
// console.log(
|
||||
// `Removed tags from ${spaceAllocations.length} space allocations.`,
|
||||
// );
|
||||
// }
|
||||
// }
|
||||
|
||||
if (allocation.deletedAllocation) {
|
||||
const spaceAllocations = await this.spaceProductRepository.find({
|
||||
where: { uuid: allocation.deletedAllocation.uuid },
|
||||
relations: ['tags'],
|
||||
});
|
||||
// if (allocation.deletedAllocation) {
|
||||
// const spaceAllocations = await this.spaceProductRepository.find({
|
||||
// where: { uuid: allocation.deletedAllocation.uuid },
|
||||
// relations: ['tags'],
|
||||
// });
|
||||
|
||||
if (!spaceAllocations || spaceAllocations.length === 0) {
|
||||
console.warn(
|
||||
`No space allocations found to delete for UUID: ${allocation.deletedAllocation.uuid}`,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
// if (!spaceAllocations || spaceAllocations.length === 0) {
|
||||
// console.warn(
|
||||
// `No space allocations found to delete for UUID: ${allocation.deletedAllocation.uuid}`,
|
||||
// );
|
||||
// continue;
|
||||
// }
|
||||
|
||||
await this.spaceProductRepository.remove(spaceAllocations);
|
||||
console.log(
|
||||
`Deleted ${spaceAllocations.length} space allocations.`,
|
||||
);
|
||||
}
|
||||
// await this.spaceProductRepository.remove(spaceAllocations);
|
||||
// console.log(
|
||||
// `Deleted ${spaceAllocations.length} space allocations.`,
|
||||
// );
|
||||
// }
|
||||
|
||||
if (allocation.newAllocation) {
|
||||
const newAllocations = spaces.map((space) =>
|
||||
this.spaceProductRepository.create({
|
||||
space,
|
||||
product: allocation.newAllocation.product,
|
||||
tags: allocation.newAllocation.tags,
|
||||
inheritedFromModel: allocation.newAllocation,
|
||||
}),
|
||||
);
|
||||
// if (allocation.newAllocation) {
|
||||
// const newAllocations = spaces.map((space) =>
|
||||
// this.spaceProductRepository.create({
|
||||
// space,
|
||||
// product: allocation.newAllocation.product,
|
||||
// tag: allocation.newAllocation.tag,
|
||||
// inheritedFromModel: allocation.newAllocation,
|
||||
// }),
|
||||
// );
|
||||
|
||||
await this.spaceProductRepository.save(newAllocations);
|
||||
console.log(
|
||||
`Created ${newAllocations.length} new space allocations.`,
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`Error processing allocation update: ${JSON.stringify(allocation)}`,
|
||||
error,
|
||||
);
|
||||
}
|
||||
}
|
||||
// await this.spaceProductRepository.save(newAllocations);
|
||||
// console.log(
|
||||
// `Created ${newAllocations.length} new space allocations.`,
|
||||
// );
|
||||
// }
|
||||
// } catch (error) {
|
||||
// console.error(
|
||||
// `Error processing allocation update: ${JSON.stringify(allocation)}`,
|
||||
// error,
|
||||
// );
|
||||
// }
|
||||
// }
|
||||
|
||||
console.log('Finished processing all allocations.');
|
||||
} catch (error) {
|
||||
|
@ -1,15 +1,14 @@
|
||||
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
|
||||
import { PropogateUpdateSpaceModelCommand } from '../commands';
|
||||
import { ModifyAction } from '@app/common/constants/modify-action.enum';
|
||||
import { SpaceProductAllocationRepository } from '@app/common/modules/space';
|
||||
import { SubspaceModelProductAllocationRepoitory } from '@app/common/modules/space-model';
|
||||
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
|
||||
import {
|
||||
SubspaceProductAllocationRepository,
|
||||
SubspaceRepository,
|
||||
} from '@app/common/modules/space/repositories/subspace.repository';
|
||||
import { SubspaceModelProductAllocationRepoitory } from '@app/common/modules/space-model';
|
||||
import { In } from 'typeorm';
|
||||
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
|
||||
import { PropogateUpdateSpaceModelCommand } from '../commands';
|
||||
import { ISingleSubspaceModel } from '../interfaces';
|
||||
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
|
||||
import { ModifyAction } from '@app/common/constants/modify-action.enum';
|
||||
import { IUpdatedAllocations } from '../interfaces/subspace-product-allocation-update-result.interface';
|
||||
|
||||
@CommandHandler(PropogateUpdateSpaceModelCommand)
|
||||
@ -72,48 +71,48 @@ export class PropogateUpdateSpaceModelHandler
|
||||
for (const allocation of allocations) {
|
||||
if (!allocation) continue;
|
||||
|
||||
if (allocation.allocation) {
|
||||
try {
|
||||
const subspaceAllocations =
|
||||
await this.subspaceProductRepository.find({
|
||||
where: {
|
||||
inheritedFromModel: { uuid: allocation.allocation.uuid },
|
||||
},
|
||||
relations: ['tags'],
|
||||
});
|
||||
// if (allocation.allocation) {
|
||||
// try {
|
||||
// const subspaceAllocations =
|
||||
// await this.subspaceProductRepository.find({
|
||||
// where: {
|
||||
// inheritedFromModel: { uuid: allocation.allocation.uuid },
|
||||
// },
|
||||
// relations: ['tags'],
|
||||
// });
|
||||
|
||||
if (!subspaceAllocations || subspaceAllocations.length === 0)
|
||||
continue;
|
||||
// if (!subspaceAllocations || subspaceAllocations.length === 0)
|
||||
// continue;
|
||||
|
||||
if (allocation.tagsAdded?.length) {
|
||||
for (const subspaceAllocation of subspaceAllocations) {
|
||||
subspaceAllocation.tags.push(...allocation.tagsAdded);
|
||||
}
|
||||
await this.subspaceProductRepository.save(subspaceAllocations);
|
||||
console.log(
|
||||
`Added tags to ${subspaceAllocations.length} subspace allocations.`,
|
||||
);
|
||||
}
|
||||
// if (allocation.tagsAdded?.length) {
|
||||
// for (const subspaceAllocation of subspaceAllocations) {
|
||||
// subspaceAllocation.tags.push(...allocation.tagsAdded);
|
||||
// }
|
||||
// await this.subspaceProductRepository.save(subspaceAllocations);
|
||||
// console.log(
|
||||
// `Added tags to ${subspaceAllocations.length} subspace allocations.`,
|
||||
// );
|
||||
// }
|
||||
|
||||
if (allocation.tagsRemoved?.length) {
|
||||
const tagsToRemoveUUIDs = allocation.tagsRemoved.map(
|
||||
(tag) => tag.uuid,
|
||||
);
|
||||
// if (allocation.tagsRemoved?.length) {
|
||||
// const tagsToRemoveUUIDs = allocation.tagsRemoved.map(
|
||||
// (tag) => tag.uuid,
|
||||
// );
|
||||
|
||||
for (const subspaceAllocation of subspaceAllocations) {
|
||||
subspaceAllocation.tags = subspaceAllocation.tags.filter(
|
||||
(tag) => !tagsToRemoveUUIDs.includes(tag.uuid),
|
||||
);
|
||||
}
|
||||
await this.subspaceProductRepository.save(subspaceAllocations);
|
||||
console.log(
|
||||
`Removed tags from ${subspaceAllocations.length} subspace allocations.`,
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error processing allocation update:', error);
|
||||
}
|
||||
}
|
||||
// for (const subspaceAllocation of subspaceAllocations) {
|
||||
// subspaceAllocation.tags = subspaceAllocation.tags.filter(
|
||||
// (tag) => !tagsToRemoveUUIDs.includes(tag.uuid),
|
||||
// );
|
||||
// }
|
||||
// await this.subspaceProductRepository.save(subspaceAllocations);
|
||||
// console.log(
|
||||
// `Removed tags from ${subspaceAllocations.length} subspace allocations.`,
|
||||
// );
|
||||
// }
|
||||
// } catch (error) {
|
||||
// console.error('Error processing allocation update:', error);
|
||||
// }
|
||||
// }
|
||||
|
||||
if (allocation.newAllocation) {
|
||||
try {
|
||||
@ -127,7 +126,7 @@ export class PropogateUpdateSpaceModelHandler
|
||||
const newAllocations = subspaces.map((subspace) =>
|
||||
this.subspaceProductRepository.create({
|
||||
product: allocation.newAllocation.product,
|
||||
tags: allocation.newAllocation.tags,
|
||||
tag: allocation.newAllocation.tag,
|
||||
subspace,
|
||||
inheritedFromModel: allocation.newAllocation,
|
||||
}),
|
||||
@ -198,7 +197,7 @@ export class PropogateUpdateSpaceModelHandler
|
||||
const subspaceAllocation = this.subspaceProductRepository.create({
|
||||
subspace: subspace,
|
||||
product: allocation.product,
|
||||
tags: allocation.tags,
|
||||
tag: allocation.tag,
|
||||
inheritedFromModel: allocation,
|
||||
});
|
||||
await this.subspaceProductRepository.save(subspaceAllocation);
|
||||
@ -211,67 +210,59 @@ export class PropogateUpdateSpaceModelHandler
|
||||
subspaceModel: ISingleSubspaceModel,
|
||||
spaces: SpaceEntity[],
|
||||
) {
|
||||
const subspaces = await this.subspaceRepository.find({
|
||||
where: {
|
||||
subSpaceModel: { uuid: subspaceModel.subspaceModel.uuid },
|
||||
disabled: false,
|
||||
},
|
||||
relations: [
|
||||
'productAllocations',
|
||||
'productAllocations.product',
|
||||
'productAllocations.tags',
|
||||
],
|
||||
});
|
||||
|
||||
if (!subspaces.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
const allocationUuidsToRemove = subspaces.flatMap((subspace) =>
|
||||
subspace.productAllocations.map((allocation) => allocation.uuid),
|
||||
);
|
||||
|
||||
if (allocationUuidsToRemove.length) {
|
||||
await this.subspaceProductRepository.delete(allocationUuidsToRemove);
|
||||
}
|
||||
|
||||
await this.subspaceRepository.update(
|
||||
{ uuid: In(subspaces.map((s) => s.uuid)) },
|
||||
{ disabled: true },
|
||||
);
|
||||
|
||||
const relocatedAllocations = subspaceModel.relocatedAllocations || [];
|
||||
|
||||
if (!relocatedAllocations.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const space of spaces) {
|
||||
for (const { allocation, tags = [] } of relocatedAllocations) {
|
||||
const spaceAllocation = await this.spaceProductRepository.findOne({
|
||||
where: {
|
||||
inheritedFromModel: { uuid: allocation.uuid },
|
||||
space: { uuid: space.uuid },
|
||||
},
|
||||
relations: ['tags'],
|
||||
});
|
||||
|
||||
if (spaceAllocation) {
|
||||
if (tags.length) {
|
||||
spaceAllocation.tags.push(...tags);
|
||||
await this.spaceProductRepository.save(spaceAllocation);
|
||||
}
|
||||
} else {
|
||||
const newSpaceAllocation = this.spaceProductRepository.create({
|
||||
space,
|
||||
inheritedFromModel: allocation,
|
||||
tags: allocation.tags,
|
||||
product: allocation.product,
|
||||
});
|
||||
await this.spaceProductRepository.save(newSpaceAllocation);
|
||||
}
|
||||
}
|
||||
}
|
||||
// const subspaces = await this.subspaceRepository.find({
|
||||
// where: {
|
||||
// subSpaceModel: { uuid: subspaceModel.subspaceModel.uuid },
|
||||
// disabled: false,
|
||||
// },
|
||||
// relations: [
|
||||
// 'productAllocations',
|
||||
// 'productAllocations.product',
|
||||
// 'productAllocations.tags',
|
||||
// ],
|
||||
// });
|
||||
// if (!subspaces.length) {
|
||||
// return;
|
||||
// }
|
||||
// const allocationUuidsToRemove = subspaces.flatMap((subspace) =>
|
||||
// subspace.productAllocations.map((allocation) => allocation.uuid),
|
||||
// );
|
||||
// if (allocationUuidsToRemove.length) {
|
||||
// await this.subspaceProductRepository.delete(allocationUuidsToRemove);
|
||||
// }
|
||||
// await this.subspaceRepository.update(
|
||||
// { uuid: In(subspaces.map((s) => s.uuid)) },
|
||||
// { disabled: true },
|
||||
// );
|
||||
// const relocatedAllocations = subspaceModel.relocatedAllocations || [];
|
||||
// if (!relocatedAllocations.length) {
|
||||
// return;
|
||||
// }
|
||||
// for (const space of spaces) {
|
||||
// for (const { allocation, tags = [] } of relocatedAllocations) {
|
||||
// const spaceAllocation = await this.spaceProductRepository.findOne({
|
||||
// where: {
|
||||
// inheritedFromModel: { uuid: allocation.uuid },
|
||||
// space: { uuid: space.uuid },
|
||||
// },
|
||||
// relations: ['tags'],
|
||||
// });
|
||||
// if (spaceAllocation) {
|
||||
// if (tags.length) {
|
||||
// spaceAllocation.tags.push(...tags);
|
||||
// await this.spaceProductRepository.save(spaceAllocation);
|
||||
// }
|
||||
// } else {
|
||||
// const newSpaceAllocation = this.spaceProductRepository.create({
|
||||
// space,
|
||||
// inheritedFromModel: allocation,
|
||||
// tag: allocation.tag,
|
||||
// product: allocation.product,
|
||||
// });
|
||||
// await this.spaceProductRepository.save(newSpaceAllocation);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
async updateSubspaceModel(subspaceModel: ISingleSubspaceModel) {
|
||||
|
@ -1,4 +1,3 @@
|
||||
export * from './update-subspace.interface';
|
||||
export * from './modify-subspace.interface';
|
||||
export * from './single-subspace.interface';
|
||||
export * from './space-product-allocation.interface';
|
||||
export * from './update-subspace.interface';
|
||||
|
@ -1,24 +0,0 @@
|
||||
import { SubspaceModelEntity, TagModel } from '@app/common/modules/space-model';
|
||||
|
||||
export interface ModifyspaceModelPayload {
|
||||
modifiedSubspaceModels?: ModifySubspaceModelPayload;
|
||||
modifiedTags?: ModifiedTagsModelPayload;
|
||||
}
|
||||
|
||||
export interface ModifySubspaceModelPayload {
|
||||
addedSubspaceModels?: SubspaceModelEntity[];
|
||||
updatedSubspaceModels?: UpdatedSubspaceModelPayload[];
|
||||
deletedSubspaceModels?: string[];
|
||||
}
|
||||
|
||||
export interface UpdatedSubspaceModelPayload {
|
||||
subspaceName?: string;
|
||||
modifiedTags?: ModifiedTagsModelPayload;
|
||||
subspaceModelUuid: string;
|
||||
}
|
||||
|
||||
export interface ModifiedTagsModelPayload {
|
||||
added?: TagModel[];
|
||||
updated?: TagModel[];
|
||||
deleted?: string[];
|
||||
}
|
@ -1,11 +1,11 @@
|
||||
import { ModifyAction } from '@app/common/constants/modify-action.enum';
|
||||
import {
|
||||
SpaceModelProductAllocationEntity,
|
||||
SubspaceModelEntity,
|
||||
} from '@app/common/modules/space-model';
|
||||
import { ModifyTagModelDto } from '../dtos';
|
||||
import { ModifyAction } from '@app/common/constants/modify-action.enum';
|
||||
import { NewTagEntity } from '@app/common/modules/tag';
|
||||
import { IUpdatedAllocations } from './subspace-product-allocation-update-result.interface';
|
||||
import { ModifyTagDto } from 'src/space/dtos/tag/modify-tag.dto';
|
||||
|
||||
export interface IRelocatedAllocation {
|
||||
allocation: SpaceModelProductAllocationEntity;
|
||||
@ -14,7 +14,7 @@ export interface IRelocatedAllocation {
|
||||
export interface ISingleSubspaceModel {
|
||||
subspaceModel: SubspaceModelEntity;
|
||||
action: ModifyAction;
|
||||
tags?: ModifyTagModelDto[];
|
||||
tags?: ModifyTagDto[];
|
||||
relocatedAllocations?: IRelocatedAllocation[];
|
||||
}
|
||||
|
||||
|
@ -1,20 +1,19 @@
|
||||
import { In, QueryRunner } from 'typeorm';
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { In, QueryRunner } from 'typeorm';
|
||||
|
||||
import { ProductEntity } from '@app/common/modules/product/entities';
|
||||
import { ProjectEntity } from '@app/common/modules/project/entities';
|
||||
import { SpaceRepository } from '@app/common/modules/space';
|
||||
import {
|
||||
SpaceModelEntity,
|
||||
SpaceModelProductAllocationEntity,
|
||||
SpaceModelProductAllocationRepoitory,
|
||||
SubspaceModelProductAllocationEntity,
|
||||
} from '@app/common/modules/space-model';
|
||||
import { TagService as NewTagService } from 'src/tags/services';
|
||||
import { ProcessTagDto } from 'src/tags/dtos';
|
||||
import { ModifySubspaceModelDto, ModifyTagModelDto } from '../dtos';
|
||||
import { ModifyAction } from '@app/common/constants/modify-action.enum';
|
||||
import { NewTagEntity } from '@app/common/modules/tag';
|
||||
import { ProductEntity } from '@app/common/modules/product/entities';
|
||||
import { SpaceRepository } from '@app/common/modules/space';
|
||||
import { ProjectEntity } from '@app/common/modules/project/entities';
|
||||
import { ModifyTagDto } from 'src/space/dtos/tag/modify-tag.dto';
|
||||
import { ProcessTagDto } from 'src/tags/dtos';
|
||||
import { TagService as NewTagService } from 'src/tags/services';
|
||||
import { ModifySubspaceModelDto } from '../dtos';
|
||||
import { IUpdatedSpaceAllocations } from '../interfaces';
|
||||
|
||||
@Injectable()
|
||||
@ -32,225 +31,220 @@ export class SpaceModelProductAllocationService {
|
||||
queryRunner?: QueryRunner,
|
||||
modifySubspaceModels?: ModifySubspaceModelDto[],
|
||||
): Promise<IUpdatedSpaceAllocations[]> {
|
||||
try {
|
||||
if (!tags.length) return [];
|
||||
// try {
|
||||
if (!tags.length) return [];
|
||||
|
||||
const allocationUpdates: IUpdatedSpaceAllocations[] = [];
|
||||
const allocationUpdates: IUpdatedSpaceAllocations[] = [];
|
||||
return allocationUpdates;
|
||||
// const processedTags = await this.tagService.processTags(
|
||||
// tags,
|
||||
// projectUuid,
|
||||
// queryRunner,
|
||||
// );
|
||||
|
||||
const processedTags = await this.tagService.processTags(
|
||||
tags,
|
||||
projectUuid,
|
||||
queryRunner,
|
||||
);
|
||||
// const productAllocations: SpaceModelProductAllocationEntity[] = [];
|
||||
// const existingAllocations = new Map<
|
||||
// string,
|
||||
// SpaceModelProductAllocationEntity
|
||||
// >();
|
||||
|
||||
const productAllocations: SpaceModelProductAllocationEntity[] = [];
|
||||
const existingAllocations = new Map<
|
||||
string,
|
||||
SpaceModelProductAllocationEntity
|
||||
>();
|
||||
// for (const tag of processedTags) {
|
||||
// let isTagNeeded = true;
|
||||
|
||||
for (const tag of processedTags) {
|
||||
let isTagNeeded = true;
|
||||
// if (modifySubspaceModels) {
|
||||
// const relatedSubspaces = await queryRunner.manager.find(
|
||||
// SubspaceModelProductAllocationEntity,
|
||||
// {
|
||||
// where: {
|
||||
// product: { uuid: tag.product.uuid },
|
||||
// subspaceModel: { spaceModel: { uuid: spaceModel.uuid } },
|
||||
// tags: { uuid: tag.uuid },
|
||||
// },
|
||||
// relations: ['subspaceModel', 'tags'],
|
||||
// },
|
||||
// );
|
||||
|
||||
if (modifySubspaceModels) {
|
||||
const relatedSubspaces = await queryRunner.manager.find(
|
||||
SubspaceModelProductAllocationEntity,
|
||||
{
|
||||
where: {
|
||||
product: { uuid: tag.product.uuid },
|
||||
subspaceModel: { spaceModel: { uuid: spaceModel.uuid } },
|
||||
tags: { uuid: tag.uuid },
|
||||
},
|
||||
relations: ['subspaceModel', 'tags'],
|
||||
},
|
||||
);
|
||||
// for (const subspaceWithTag of relatedSubspaces) {
|
||||
// const modifyingSubspace = modifySubspaceModels.find(
|
||||
// (subspace) =>
|
||||
// subspace.action === ModifyAction.UPDATE &&
|
||||
// subspace.uuid === subspaceWithTag.subspaceModel.uuid,
|
||||
// );
|
||||
|
||||
for (const subspaceWithTag of relatedSubspaces) {
|
||||
const modifyingSubspace = modifySubspaceModels.find(
|
||||
(subspace) =>
|
||||
subspace.action === ModifyAction.UPDATE &&
|
||||
subspace.uuid === subspaceWithTag.subspaceModel.uuid,
|
||||
);
|
||||
// if (
|
||||
// modifyingSubspace &&
|
||||
// modifyingSubspace.tags &&
|
||||
// modifyingSubspace.tags.some(
|
||||
// (subspaceTag) =>
|
||||
// subspaceTag.action === ModifyAction.DELETE &&
|
||||
// subspaceTag.tagUuid === tag.uuid,
|
||||
// )
|
||||
// ) {
|
||||
// isTagNeeded = true;
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
if (
|
||||
modifyingSubspace &&
|
||||
modifyingSubspace.tags &&
|
||||
modifyingSubspace.tags.some(
|
||||
(subspaceTag) =>
|
||||
subspaceTag.action === ModifyAction.DELETE &&
|
||||
subspaceTag.tagUuid === tag.uuid,
|
||||
)
|
||||
) {
|
||||
isTagNeeded = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// if (isTagNeeded) {
|
||||
// const hasTags = await this.validateTagWithinSpaceModel(
|
||||
// queryRunner,
|
||||
// tag,
|
||||
// spaceModel,
|
||||
// );
|
||||
|
||||
if (isTagNeeded) {
|
||||
const hasTags = await this.validateTagWithinSpaceModel(
|
||||
queryRunner,
|
||||
tag,
|
||||
spaceModel,
|
||||
);
|
||||
// if (hasTags) continue;
|
||||
|
||||
if (hasTags) continue;
|
||||
// let allocation = existingAllocations.get(tag.product.uuid);
|
||||
// if (!allocation) {
|
||||
// allocation = await this.getAllocationByProduct(
|
||||
// tag.product,
|
||||
// spaceModel,
|
||||
// queryRunner,
|
||||
// );
|
||||
// if (allocation) {
|
||||
// existingAllocations.set(tag.product.uuid, allocation);
|
||||
// }
|
||||
// }
|
||||
|
||||
let allocation = existingAllocations.get(tag.product.uuid);
|
||||
if (!allocation) {
|
||||
allocation = await this.getAllocationByProduct(
|
||||
tag.product,
|
||||
spaceModel,
|
||||
queryRunner,
|
||||
);
|
||||
if (allocation) {
|
||||
existingAllocations.set(tag.product.uuid, allocation);
|
||||
}
|
||||
}
|
||||
// if (!allocation) {
|
||||
// allocation = this.createNewAllocation(spaceModel, tag, queryRunner);
|
||||
// productAllocations.push(allocation);
|
||||
// allocationUpdates.push({
|
||||
// newAllocation: allocation,
|
||||
// });
|
||||
// } else if (!allocation.tags.some((t) => t.uuid === tag.uuid)) {
|
||||
// allocation.tags.push(tag);
|
||||
// allocationUpdates.push({
|
||||
// allocation: allocation,
|
||||
// tagsAdded: [tag],
|
||||
// });
|
||||
// await this.saveAllocation(allocation, queryRunner);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
if (!allocation) {
|
||||
allocation = this.createNewAllocation(spaceModel, tag, queryRunner);
|
||||
productAllocations.push(allocation);
|
||||
allocationUpdates.push({
|
||||
newAllocation: allocation,
|
||||
});
|
||||
} else if (!allocation.tags.some((t) => t.uuid === tag.uuid)) {
|
||||
allocation.tags.push(tag);
|
||||
allocationUpdates.push({
|
||||
allocation: allocation,
|
||||
tagsAdded: [tag],
|
||||
});
|
||||
await this.saveAllocation(allocation, queryRunner);
|
||||
}
|
||||
}
|
||||
}
|
||||
// if (productAllocations.length > 0) {
|
||||
// await this.saveAllocations(productAllocations, queryRunner);
|
||||
// }
|
||||
|
||||
if (productAllocations.length > 0) {
|
||||
await this.saveAllocations(productAllocations, queryRunner);
|
||||
}
|
||||
|
||||
return allocationUpdates;
|
||||
} catch (error) {
|
||||
throw this.handleError(error, 'Failed to create product allocations');
|
||||
}
|
||||
// return allocationUpdates;
|
||||
// } catch (error) {
|
||||
// throw this.handleError(error, 'Failed to create product allocations');
|
||||
// }
|
||||
}
|
||||
|
||||
async updateProductAllocations(
|
||||
dtos: ModifyTagModelDto[],
|
||||
dtos: ModifyTagDto[],
|
||||
project: ProjectEntity,
|
||||
spaceModel: SpaceModelEntity,
|
||||
queryRunner: QueryRunner,
|
||||
modifySubspaceModels?: ModifySubspaceModelDto[],
|
||||
): Promise<IUpdatedSpaceAllocations[]> {
|
||||
try {
|
||||
const addDtos = dtos.filter((dto) => dto.action === ModifyAction.ADD);
|
||||
const deleteDtos = dtos.filter(
|
||||
(dto) => dto.action === ModifyAction.DELETE,
|
||||
);
|
||||
|
||||
const addTagDtos: ProcessTagDto[] = addDtos.map((dto) => ({
|
||||
name: dto.name,
|
||||
productUuid: dto.productUuid,
|
||||
uuid: dto.newTagUuid,
|
||||
}));
|
||||
|
||||
// Process added tags
|
||||
const processedTags = await this.tagService.processTags(
|
||||
addTagDtos,
|
||||
project.uuid,
|
||||
queryRunner,
|
||||
);
|
||||
|
||||
const addTagUuidMap = new Map<string, ModifyTagModelDto>();
|
||||
processedTags.forEach((tag, index) => {
|
||||
addTagUuidMap.set(tag.uuid, addDtos[index]);
|
||||
});
|
||||
|
||||
const addTagUuids = new Set(processedTags.map((tag) => tag.uuid));
|
||||
const deleteTagUuids = new Set(deleteDtos.map((dto) => dto.tagUuid));
|
||||
|
||||
const tagsToIgnore = new Set(
|
||||
[...addTagUuids].filter((uuid) => deleteTagUuids.has(uuid)),
|
||||
);
|
||||
|
||||
// Filter out tags that are added and deleted in the same request
|
||||
const filteredDtos = dtos.filter(
|
||||
(dto) =>
|
||||
!(
|
||||
tagsToIgnore.has(dto.tagUuid) ||
|
||||
(dto.action === ModifyAction.ADD &&
|
||||
tagsToIgnore.has(
|
||||
[...addTagUuidMap.keys()].find(
|
||||
(uuid) => addTagUuidMap.get(uuid) === dto,
|
||||
),
|
||||
))
|
||||
),
|
||||
);
|
||||
|
||||
// Process add and delete actions concurrently
|
||||
const [updatedAllocations, deletedAllocations] = await Promise.all([
|
||||
this.processAddActions(
|
||||
filteredDtos,
|
||||
project.uuid,
|
||||
spaceModel,
|
||||
queryRunner,
|
||||
modifySubspaceModels,
|
||||
),
|
||||
this.processDeleteActions(filteredDtos, queryRunner, spaceModel),
|
||||
]);
|
||||
|
||||
// Combine results and return
|
||||
return [...updatedAllocations, ...deletedAllocations];
|
||||
} catch (error) {
|
||||
throw this.handleError(error, 'Error while updating product allocations');
|
||||
}
|
||||
const allocationUpdates: IUpdatedSpaceAllocations[] = [];
|
||||
return allocationUpdates;
|
||||
// try {
|
||||
// const addDtos = dtos.filter((dto) => dto.action === ModifyAction.ADD);
|
||||
// const deleteDtos = dtos.filter(
|
||||
// (dto) => dto.action === ModifyAction.DELETE,
|
||||
// );
|
||||
// const addTagDtos: ProcessTagDto[] = addDtos.map((dto) => ({
|
||||
// name: dto.name,
|
||||
// productUuid: dto.productUuid,
|
||||
// uuid: dto.newTagUuid,
|
||||
// }));
|
||||
// // Process added tags
|
||||
// const processedTags = await this.tagService.processTags(
|
||||
// addTagDtos,
|
||||
// project.uuid,
|
||||
// queryRunner,
|
||||
// );
|
||||
// const addTagUuidMap = new Map<string, ModifyTagModelDto>();
|
||||
// processedTags.forEach((tag, index) => {
|
||||
// addTagUuidMap.set(tag.uuid, addDtos[index]);
|
||||
// });
|
||||
// const addTagUuids = new Set(processedTags.map((tag) => tag.uuid));
|
||||
// const deleteTagUuids = new Set(deleteDtos.map((dto) => dto.tagUuid));
|
||||
// const tagsToIgnore = new Set(
|
||||
// [...addTagUuids].filter((uuid) => deleteTagUuids.has(uuid)),
|
||||
// );
|
||||
// // Filter out tags that are added and deleted in the same request
|
||||
// const filteredDtos = dtos.filter(
|
||||
// (dto) =>
|
||||
// !(
|
||||
// tagsToIgnore.has(dto.tagUuid) ||
|
||||
// (dto.action === ModifyAction.ADD &&
|
||||
// tagsToIgnore.has(
|
||||
// [...addTagUuidMap.keys()].find(
|
||||
// (uuid) => addTagUuidMap.get(uuid) === dto,
|
||||
// ),
|
||||
// ))
|
||||
// ),
|
||||
// );
|
||||
// // Process add and delete actions concurrently
|
||||
// const [updatedAllocations, deletedAllocations] = await Promise.all([
|
||||
// this.processAddActions(
|
||||
// filteredDtos,
|
||||
// project.uuid,
|
||||
// spaceModel,
|
||||
// queryRunner,
|
||||
// modifySubspaceModels,
|
||||
// ),
|
||||
// this.processDeleteActions(filteredDtos, queryRunner, spaceModel),
|
||||
// ]);
|
||||
// // Combine results and return
|
||||
// return [...updatedAllocations, ...deletedAllocations];
|
||||
// } catch (error) {
|
||||
// throw this.handleError(error, 'Error while updating product allocations');
|
||||
// }
|
||||
}
|
||||
|
||||
private async processAddActions(
|
||||
dtos: ModifyTagModelDto[],
|
||||
dtos: ModifyTagDto[],
|
||||
projectUuid: string,
|
||||
spaceModel: SpaceModelEntity,
|
||||
queryRunner: QueryRunner,
|
||||
modifySubspaceModels?: ModifySubspaceModelDto[],
|
||||
): Promise<IUpdatedSpaceAllocations[]> {
|
||||
let allocationUpdates: IUpdatedSpaceAllocations[] = [];
|
||||
|
||||
const addDtos: ProcessTagDto[] = dtos
|
||||
.filter((dto) => dto.action === ModifyAction.ADD)
|
||||
.map((dto) => ({
|
||||
name: dto.name,
|
||||
productUuid: dto.productUuid,
|
||||
uuid: dto.newTagUuid,
|
||||
}));
|
||||
|
||||
if (addDtos.length > 0) {
|
||||
allocationUpdates = await this.createProductAllocations(
|
||||
projectUuid,
|
||||
spaceModel,
|
||||
addDtos,
|
||||
queryRunner,
|
||||
modifySubspaceModels,
|
||||
);
|
||||
}
|
||||
const allocationUpdates: IUpdatedSpaceAllocations[] = [];
|
||||
return allocationUpdates;
|
||||
// const addDtos: ProcessTagDto[] = dtos
|
||||
// .filter((dto) => dto.action === ModifyAction.ADD)
|
||||
// .map((dto) => ({
|
||||
// name: dto.name,
|
||||
// productUuid: dto.productUuid,
|
||||
// uuid: dto.newTagUuid,
|
||||
// }));
|
||||
|
||||
// if (addDtos.length > 0) {
|
||||
// allocationUpdates = await this.createProductAllocations(
|
||||
// projectUuid,
|
||||
// spaceModel,
|
||||
// addDtos,
|
||||
// queryRunner,
|
||||
// modifySubspaceModels,
|
||||
// );
|
||||
// }
|
||||
// return allocationUpdates;
|
||||
}
|
||||
|
||||
private createNewAllocation(
|
||||
spaceModel: SpaceModelEntity,
|
||||
tag: NewTagEntity,
|
||||
queryRunner?: QueryRunner,
|
||||
): SpaceModelProductAllocationEntity {
|
||||
return queryRunner
|
||||
? queryRunner.manager.create(SpaceModelProductAllocationEntity, {
|
||||
spaceModel,
|
||||
product: tag.product,
|
||||
tags: [tag],
|
||||
})
|
||||
: this.spaceModelProductAllocationRepository.create({
|
||||
spaceModel,
|
||||
product: tag.product,
|
||||
tags: [tag],
|
||||
});
|
||||
) {
|
||||
// : SpaceModelProductAllocationEntity
|
||||
// return queryRunner
|
||||
// ? queryRunner.manager.create(SpaceModelProductAllocationEntity, {
|
||||
// spaceModel,
|
||||
// product: tag.product,
|
||||
// tags: [tag],
|
||||
// })
|
||||
// : this.spaceModelProductAllocationRepository.create({
|
||||
// spaceModel,
|
||||
// product: tag.product,
|
||||
// tags: [tag],
|
||||
// });
|
||||
}
|
||||
|
||||
private async getAllocationByProduct(
|
||||
@ -309,7 +303,7 @@ export class SpaceModelProductAllocationService {
|
||||
}
|
||||
|
||||
private async processDeleteActions(
|
||||
dtos: ModifyTagModelDto[],
|
||||
dtos: ModifyTagDto[],
|
||||
queryRunner: QueryRunner,
|
||||
spaceModel: SpaceModelEntity,
|
||||
): Promise<IUpdatedSpaceAllocations[]> {
|
||||
@ -317,10 +311,10 @@ export class SpaceModelProductAllocationService {
|
||||
if (!dtos || dtos.length === 0) {
|
||||
return;
|
||||
}
|
||||
let allocationUpdateToPropagate: IUpdatedSpaceAllocations[] = [];
|
||||
const allocationUpdateToPropagate: IUpdatedSpaceAllocations[] = [];
|
||||
|
||||
const tagUuidsToDelete = dtos
|
||||
.filter((dto) => dto.action === ModifyAction.DELETE && dto.tagUuid)
|
||||
// .filter((dto) => dto.action === ModifyAction.DELETE && dto.tagUuid)
|
||||
.map((dto) => dto.tagUuid);
|
||||
|
||||
if (tagUuidsToDelete.length === 0) return [];
|
||||
@ -329,7 +323,7 @@ export class SpaceModelProductAllocationService {
|
||||
SpaceModelProductAllocationEntity,
|
||||
{
|
||||
where: {
|
||||
tags: { uuid: In(tagUuidsToDelete) },
|
||||
tag: In(tagUuidsToDelete),
|
||||
spaceModel: {
|
||||
uuid: spaceModel.uuid,
|
||||
},
|
||||
@ -348,31 +342,28 @@ export class SpaceModelProductAllocationService {
|
||||
const allocationUpdates: SpaceModelProductAllocationEntity[] = [];
|
||||
|
||||
for (const allocation of allocationsToUpdate) {
|
||||
const updatedTags = allocation.tags.filter(
|
||||
(tag) => !tagUuidsToDelete.includes(tag.uuid),
|
||||
);
|
||||
|
||||
const deletedTags = allocation.tags.filter((tag) =>
|
||||
tagUuidsToDelete.includes(tag.uuid),
|
||||
);
|
||||
|
||||
if (updatedTags.length === allocation.tags.length) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (updatedTags.length === 0) {
|
||||
deletedAllocations.push(allocation);
|
||||
allocationUpdateToPropagate.push({
|
||||
deletedAllocation: allocation,
|
||||
});
|
||||
} else {
|
||||
allocation.tags = updatedTags;
|
||||
allocationUpdates.push(allocation);
|
||||
allocationUpdateToPropagate.push({
|
||||
allocation: allocation,
|
||||
tagsRemoved: deletedTags,
|
||||
});
|
||||
}
|
||||
// const updatedTags = allocation.tags.filter(
|
||||
// (tag) => !tagUuidsToDelete.includes(tag.uuid),
|
||||
// );
|
||||
// const deletedTags = allocation.tags.filter((tag) =>
|
||||
// tagUuidsToDelete.includes(tag.uuid),
|
||||
// );
|
||||
// if (updatedTags.length === allocation.tags.length) {
|
||||
// continue;
|
||||
// }
|
||||
// if (updatedTags.length === 0) {
|
||||
// deletedAllocations.push(allocation);
|
||||
// allocationUpdateToPropagate.push({
|
||||
// deletedAllocation: allocation,
|
||||
// });
|
||||
// } else {
|
||||
// allocation.tags = updatedTags;
|
||||
// allocationUpdates.push(allocation);
|
||||
// allocationUpdateToPropagate.push({
|
||||
// allocation: allocation,
|
||||
// tagsRemoved: deletedTags,
|
||||
// });
|
||||
// }
|
||||
}
|
||||
|
||||
if (allocationUpdates.length > 0) {
|
||||
@ -415,33 +406,34 @@ export class SpaceModelProductAllocationService {
|
||||
tag: NewTagEntity,
|
||||
spaceModel: SpaceModelEntity,
|
||||
): Promise<boolean> {
|
||||
const existingAllocationsForProduct = await queryRunner.manager.find(
|
||||
SpaceModelProductAllocationEntity,
|
||||
{
|
||||
where: {
|
||||
spaceModel: {
|
||||
uuid: spaceModel.uuid,
|
||||
},
|
||||
product: {
|
||||
uuid: tag.product.uuid,
|
||||
},
|
||||
},
|
||||
relations: ['tags'],
|
||||
},
|
||||
);
|
||||
return true;
|
||||
// const existingAllocationsForProduct = await queryRunner.manager.find(
|
||||
// SpaceModelProductAllocationEntity,
|
||||
// {
|
||||
// where: {
|
||||
// spaceModel: {
|
||||
// uuid: spaceModel.uuid,
|
||||
// },
|
||||
// product: {
|
||||
// uuid: tag.product.uuid,
|
||||
// },
|
||||
// },
|
||||
// relations: ['tags'],
|
||||
// },
|
||||
// );
|
||||
|
||||
const existingTagsForProduct = existingAllocationsForProduct.flatMap(
|
||||
(allocation) => allocation.tags,
|
||||
);
|
||||
// const existingTagsForProduct = existingAllocationsForProduct.flatMap(
|
||||
// (allocation) => allocation.tags,
|
||||
// );
|
||||
|
||||
const isDuplicateTag = existingTagsForProduct.some(
|
||||
(existingTag) => existingTag.uuid === tag.uuid,
|
||||
);
|
||||
// const isDuplicateTag = existingTagsForProduct.some(
|
||||
// (existingTag) => existingTag.uuid === tag.uuid,
|
||||
// );
|
||||
|
||||
if (isDuplicateTag) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
// if (isDuplicateTag) {
|
||||
// return true;
|
||||
// }
|
||||
// return false;
|
||||
}
|
||||
|
||||
async clearAllAllocations(spaceModelUuid: string, queryRunner: QueryRunner) {
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user