64 Commits

Author SHA1 Message Date
0fbb30bb4f feat: add google drive dam foundation 2026-05-08 11:36:30 -04:00
2eb54b9228 fix: normalize release commit timestamps
All checks were successful
deploy-socialize / image (push) Successful in 51s
deploy-socialize / deploy (push) Successful in 19s
2026-05-07 21:45:42 -04:00
9c011f1a1e feat: import release commits from repository api
All checks were successful
deploy-socialize / image (push) Successful in 1m12s
deploy-socialize / deploy (push) Successful in 19s
2026-05-07 21:38:57 -04:00
b6eb348605 feat: add release communications
All checks were successful
deploy-socialize / image (push) Successful in 1m12s
deploy-socialize / deploy (push) Successful in 19s
2026-05-07 21:04:29 -04:00
7a8a0a44bf feat: localize membership tier display
All checks were successful
deploy-socialize / image (push) Successful in 1m11s
deploy-socialize / deploy (push) Successful in 19s
2026-05-07 20:43:08 -04:00
6d92119c9c feat: add database backed membership tiers
All checks were successful
deploy-socialize / image (push) Successful in 1m9s
deploy-socialize / deploy (push) Successful in 19s
2026-05-07 20:29:53 -04:00
db16e79d9f feat: add organization onboarding
All checks were successful
deploy-socialize / image (push) Successful in 1m8s
deploy-socialize / deploy (push) Successful in 19s
2026-05-07 20:07:50 -04:00
4aaa1a7f90 refactor: use vuetify form controls 2026-05-07 19:38:51 -04:00
6ac05e1a10 refactor: simplify frontend theme setup 2026-05-07 16:35:47 -04:00
9768a37252 fix(frontend): align TypeScript with OpenAPI tooling
All checks were successful
deploy-socialize / image (push) Successful in 59s
deploy-socialize / deploy (push) Successful in 19s
2026-05-07 15:59:10 -04:00
98c76a7d88 chore: group database diagram tables by module
Some checks failed
deploy-socialize / image (push) Failing after 24s
deploy-socialize / deploy (push) Has been skipped
2026-05-07 15:51:47 -04:00
49e2ca1774 fix(backend): add missing domain foreign keys
Some checks failed
deploy-socialize / image (push) Failing after 44s
deploy-socialize / deploy (push) Has been skipped
2026-05-07 15:48:12 -04:00
e9fb1c5ee0 chore: add database diagram generator
Some checks failed
deploy-socialize / image (push) Failing after 26s
deploy-socialize / deploy (push) Has been skipped
2026-05-07 14:40:15 -04:00
57abe57bc7 fix: confirm email changes and enforce clean backend build
Some checks failed
deploy-socialize / deploy (push) Has been cancelled
deploy-socialize / image (push) Has been cancelled
2026-05-07 14:39:22 -04:00
9022fa7d93 fix(backend): make API types internal 2026-05-07 14:06:37 -04:00
d1621ecb36 refactor(backend): rename registration classes 2026-05-07 14:02:55 -04:00
6e417312f9 chore(backend): add explicit test data seed command
All checks were successful
deploy-socialize / image (push) Successful in 53s
deploy-socialize / deploy (push) Successful in 21s
2026-05-07 13:43:53 -04:00
918136aae2 fix(frontend): update router guard API
All checks were successful
deploy-socialize / image (push) Successful in 53s
deploy-socialize / deploy (push) Successful in 18s
2026-05-07 12:34:32 -04:00
0521d91240 fix(frontend): update favicon assets
All checks were successful
deploy-socialize / image (push) Successful in 1m27s
deploy-socialize / deploy (push) Successful in 19s
2026-05-07 12:31:55 -04:00
c18a223759 chore(backend): refresh initial migration timestamp 2026-05-07 12:31:55 -04:00
298c46de7c fix(frontend): remove BOM from development env 2026-05-07 12:31:55 -04:00
2d22fd6e04 chore(frontend): migrate Tailwind to Vite plugin 2026-05-07 12:31:55 -04:00
ef323c291f chore(cd): hardening of env settings
All checks were successful
deploy-socialize / image (push) Successful in 28s
deploy-socialize / deploy (push) Successful in 15s
2026-05-06 21:25:11 -04:00
4eb0fbc22b fix: avoid feedback screenshot concurrency save
All checks were successful
deploy-socialize / image (push) Successful in 33s
deploy-socialize / deploy (push) Successful in 19s
2026-05-06 20:14:22 -04:00
afe22949c5 ci: run deploy job on ubuntu runner
All checks were successful
deploy-socialize / image (push) Successful in 29s
deploy-socialize / deploy (push) Successful in 23s
2026-05-06 16:20:59 -04:00
ebb87b286f ci: checkout deploy compose artifact
Some checks failed
deploy-socialize / image (push) Successful in 32s
deploy-socialize / deploy (push) Failing after 2s
2026-05-06 16:18:40 -04:00
f1da3a44de ci: sync production compose file
Some checks failed
deploy-socialize / image (push) Successful in 29s
deploy-socialize / deploy (push) Failing after 7s
2026-05-06 16:16:55 -04:00
419dbf0185 ci: align compose database host
All checks were successful
deploy-socialize / image (push) Successful in 34s
deploy-socialize / deploy (push) Successful in 14s
2026-05-06 15:56:29 -04:00
909ae6f092 ci: export backend deployment environment
All checks were successful
deploy-socialize / image (push) Successful in 34s
deploy-socialize / deploy (push) Successful in 14s
2026-05-06 15:49:28 -04:00
a97ff2dc38 fix: add verification resend flow
All checks were successful
deploy-socialize / image (push) Successful in 1m21s
deploy-socialize / deploy (push) Successful in 14s
2026-05-06 15:43:25 -04:00
7a862a202a fix: normalize Resend API key configuration
All checks were successful
deploy-socialize / image (push) Successful in 57s
deploy-socialize / deploy (push) Successful in 23s
2026-05-06 15:36:49 -04:00
1ae3188d34 chore: configure preprod email secrets
All checks were successful
deploy-socialize / image (push) Successful in 52s
deploy-socialize / deploy (push) Successful in 13s
2026-05-06 15:24:17 -04:00
fb7811c469 ci: quote deploy environment secrets
All checks were successful
deploy-socialize / image (push) Successful in 27s
deploy-socialize / deploy (push) Successful in 13s
2026-05-06 15:08:53 -04:00
0a6d730ca0 chore: source compose database password from secrets
Some checks failed
deploy-socialize / image (push) Successful in 30s
deploy-socialize / deploy (push) Failing after 6s
2026-05-06 15:05:10 -04:00
d2d3bee975 ci: remove repository hygiene check 2026-05-06 14:51:43 -04:00
78de068cd1 chore: ignore AI agent local state 2026-05-06 14:50:06 -04:00
1965dc2c9e docs: remove archived legacy material
All checks were successful
deploy-socialize / image (push) Successful in 1m24s
deploy-socialize / deploy (push) Successful in 9s
2026-05-06 14:40:28 -04:00
f0d635ef21 chore: remove legacy Hutopy assets 2026-05-06 14:36:23 -04:00
d59d667796 chore: remove legacy deployment domains 2026-05-06 14:33:34 -04:00
5c0e40db7e feat: centralize frontend branding 2026-05-06 14:27:09 -04:00
dc9a980958 fix: frontend API base URL
All checks were successful
deploy-socialize / image (push) Successful in 1m26s
deploy-socialize / deploy (push) Successful in 8s
2026-05-06 10:56:59 -04:00
c40653b2b7 chore(ci): guards against tracked build artefacts
All checks were successful
deploy-socialize / deploy (push) Successful in 8s
deploy-socialize / image (push) Successful in 32s
2026-05-05 23:41:20 -04:00
f240d32ce6 chore(ci): use app Caddyfile in frontend image
All checks were successful
deploy-socialize / image (push) Successful in 55s
deploy-socialize / deploy (push) Successful in 8s
2026-05-05 23:37:25 -04:00
4775e35b3c Merge branch 'main' of sobina-git:jbourdon/social-media
All checks were successful
deploy-socialize / image (push) Successful in 2m7s
deploy-socialize / deploy (push) Successful in 32s
2026-05-05 23:26:59 -04:00
a7535d460d feat: refine content calendar experience 2026-05-05 23:25:58 -04:00
db344eebac chore(ci): remove CI test job
Some checks failed
deploy-socialize / image (push) Failing after 38s
deploy-socialize / deploy (push) Has been skipped
2026-05-05 23:13:12 -04:00
9699c4d55c chore(ci): split dotnet restore and test in CI
Some checks failed
deploy-socialize / test (push) Failing after 22s
deploy-socialize / image (push) Has been skipped
deploy-socialize / deploy (push) Has been skipped
2026-05-05 23:09:26 -04:00
c183626a7a chore(ci): use base64 encoded deploy SSH keys
Some checks failed
deploy-socialize / test (push) Failing after 22s
deploy-socialize / image (push) Has been skipped
deploy-socialize / deploy (push) Has been skipped
2026-05-05 23:04:12 -04:00
5db182dda9 chore(ci): use node runner image for checkout
Some checks failed
deploy-socialize / test (push) Failing after 17s
deploy-socialize / image (push) Has been skipped
deploy-socialize / deploy (push) Has been skipped
2026-05-05 23:01:24 -04:00
6296a91c3d chore(ci): run CI tests in isolated workspace
Some checks failed
deploy-socialize / test (push) Failing after 3s
deploy-socialize / image (push) Has been skipped
deploy-socialize / deploy (push) Has been skipped
2026-05-05 22:59:49 -04:00
91b7f96fdb chore(ci): run backend tests through dotnet SDK container
Some checks failed
deploy-socialize / test (push) Failing after 29s
deploy-socialize / deploy (push) Has been skipped
deploy-socialize / image (push) Has been skipped
2026-05-05 22:54:12 -04:00
88c4c23ce1 chore(ci): fix some dotnet compilation issue with glogging resx
Some checks failed
deploy-socialize / test (push) Failing after 1m33s
deploy-socialize / image (push) Has been skipped
deploy-socialize / deploy (push) Has been skipped
2026-05-05 22:50:42 -04:00
a96b3c897c chore(ci): fix gitea registry deploy workflow
Some checks failed
deploy-socialize / test (push) Failing after 28s
deploy-socialize / image (push) Has been skipped
deploy-socialize / deploy (push) Has been skipped
2026-05-05 22:41:12 -04:00
a437bfcfc3 chore(ci): update SEO
Some checks failed
deploy-socialize / test (push) Failing after 2s
deploy-socialize / image (push) Has been skipped
deploy-socialize / deploy (push) Has been skipped
2026-05-05 22:24:57 -04:00
b7b282a71a chore(ci): update configuration
Some checks failed
deploy-socialize / test (push) Failing after 3s
deploy-socialize / deploy (push) Has been skipped
deploy-socialize / image (push) Has been skipped
2026-05-05 22:23:19 -04:00
6083797eb1 add ci deployment workflow
Some checks failed
deploy-socialize / test (push) Failing after 48s
deploy-socialize / image (push) Has been skipped
deploy-socialize / deploy (push) Has been skipped
2026-05-05 21:54:16 -04:00
ecbd3daa1b Update frontend/Dockerfile
Some checks failed
Backend CI/CD / build_and_deploy (push) Waiting to run
Frontend CI/CD / build_and_deploy (push) Failing after 2m54s
2026-05-05 21:46:16 -04:00
b66c10b681 Add calendar integrations and collaboration updates
Some checks failed
Backend CI/CD / build_and_deploy (push) Has been cancelled
Frontend CI/CD / build_and_deploy (push) Has been cancelled
2026-05-05 15:25:53 -04:00
c49f03ec06 chore: add script to easy recreating/reseeding the database
Some checks failed
Backend CI/CD / build_and_deploy (push) Has been cancelled
Frontend CI/CD / build_and_deploy (push) Has been cancelled
2026-05-05 13:22:49 -04:00
23ae78f6e1 chore: hide some warnings about public/internal api 2026-05-05 13:21:48 -04:00
0d4188b64e Add multi-workspace selector scope 2026-05-05 13:20:44 -04:00
78a7517de7 feat: add alpha preview brand badge 2026-05-05 13:19:33 -04:00
244be555f9 Add real workspace channels 2026-05-05 13:06:57 -04:00
6e658b8215 docs: add calendar integration spec 2026-05-05 13:02:14 -04:00
518 changed files with 41980 additions and 7246 deletions

View File

@@ -0,0 +1,75 @@
name: deploy-socialize
on:
push:
branches:
- main
jobs:
image:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Docker CLI
run: apt-get update && apt-get install -y docker.io
- name: Login to Gitea container registry
env:
REGISTRY_USER: ${{ secrets.REGISTRY_USER }}
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }}
run: printf '%s' "$REGISTRY_PASSWORD" | docker login git.mapachotes.com -u "$REGISTRY_USER" --password-stdin
- name: Build images
run: |
docker build \
-t git.mapachotes.com/jbourdon/socialize-api:${{ gitea.sha }} \
-t git.mapachotes.com/jbourdon/socialize-api:latest \
-f backend/src/Socialize.Api/Dockerfile .
docker build \
--build-arg VITE_API_URL=/ \
-t git.mapachotes.com/jbourdon/socialize-web:${{ gitea.sha }} \
-t git.mapachotes.com/jbourdon/socialize-web:latest \
-f frontend/Dockerfile .
- name: Push images
run: |
docker push git.mapachotes.com/jbourdon/socialize-api:${{ gitea.sha }}
docker push git.mapachotes.com/jbourdon/socialize-api:latest
docker push git.mapachotes.com/jbourdon/socialize-web:${{ gitea.sha }}
docker push git.mapachotes.com/jbourdon/socialize-web:latest
deploy:
needs: image
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install SSH client
run: apt-get update && apt-get install -y openssh-client
- name: Deploy on sobina
env:
DEPLOY_HOST: ${{ secrets.DEPLOY_HOST }}
DEPLOY_USER: ${{ secrets.DEPLOY_USER }}
DEPLOY_SSH_PRIVATE_KEY_B64: ${{ secrets.DEPLOY_SSH_PRIVATE_KEY_B64 }}
SOCIALIZE_IMAGE_TAG: ${{ gitea.sha }}
run: |
: "${SOCIALIZE_IMAGE_TAG:?SOCIALIZE_IMAGE_TAG is required}"
mkdir -p ~/.ssh
printf '%s' "$DEPLOY_SSH_PRIVATE_KEY_B64" | base64 -d > ~/.ssh/deploy_key
chmod 600 ~/.ssh/deploy_key
write_env_value() {
key="$1"
value="$2"
escaped_value="$(printf '%s' "$value" | sed "s/'/'\\\\''/g")"
printf "%s='%s'\n" "$key" "$escaped_value"
}
deploy_env="$(mktemp)"
{
write_env_value SOCIALIZE_IMAGE_TAG "$SOCIALIZE_IMAGE_TAG"
} > "$deploy_env"
scp -i ~/.ssh/deploy_key -o StrictHostKeyChecking=accept-new "$deploy_env" "$DEPLOY_USER@$DEPLOY_HOST:/srv/prod/socialize/.deploy.env"
rm -f "$deploy_env"
scp -i ~/.ssh/deploy_key -o StrictHostKeyChecking=accept-new deploy/compose.yml "$DEPLOY_USER@$DEPLOY_HOST:/srv/prod/socialize/compose.yml"
ssh -i ~/.ssh/deploy_key -o StrictHostKeyChecking=accept-new "$DEPLOY_USER@$DEPLOY_HOST" \
'test -r /etc/socialize/socialize.env && cd /srv/prod/socialize && ./deploy.sh'

View File

@@ -1,39 +0,0 @@
name: Backend CI/CD
on:
push:
branches:
- main
env:
AZURE_WEBAPP_NAME: hutopy-backend-api
DOTNET_VERSION: '10.0.x'
jobs:
build_and_deploy:
runs-on: ubuntu-latest
environment: dev
steps:
# Checkout the repository
- uses: actions/checkout@v2
# Setup .NET Core
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: ${{ env.DOTNET_VERSION }}
# Run dotnet publish
- name: dotnet build and publish
run: |
cd backend
dotnet publish --configuration Release --artifacts-path ./publish/ Socialize.slnx
# Deploy to Azure WebApp
- name: Deploy to Azure WebApp
uses: azure/webapps-deploy@v2
with:
app-name: ${{ env.AZURE_WEBAPP_NAME }}
publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
package: './backend/publish/publish/Socialize.Api/release/'

View File

@@ -1,38 +0,0 @@
name: Frontend CI/CD
on:
push:
branches:
- main
env:
AZURE_SWA_NAME: hutopy-portal
jobs:
build_and_deploy:
runs-on: ubuntu-latest
steps:
# Checkout the repository
- uses: actions/checkout@v2
# Npm install
- name: npm install
run: |
cd frontend
npm install
# Npm run build
- name: npm run build
run: |
cd frontend
npm run build
# Deploy to Azure SWA
- name: Deploy to Azure SWA
uses: azure/static-web-apps-deploy@v1
with:
action: "upload"
app_location: 'frontend'
output_location: 'dist'
azure_static_web_apps_api_token: ${{ secrets.AZURE_SWA_TOKEN }}

13
.gitignore vendored
View File

@@ -22,6 +22,10 @@ Thumbs.db
# .NET # .NET
bin/ bin/
obj/ obj/
**/[Bb]in/
**/[Oo]bj/
**/[Bb]in[\\]*
**/[Oo]bj[\\]*
TestResults/ TestResults/
# Node # Node
@@ -30,6 +34,7 @@ dist/
.vite/ .vite/
# Local environment files # Local environment files
.env
*.local *.local
.env.local .env.local
.env.*.local .env.*.local
@@ -38,5 +43,11 @@ App_Data/
# Local SSL certificates # Local SSL certificates
*.pem *.pem
# Ai # AI agent local state
.agents
.agents/
.codex .codex
.codex/
# Generated local artifacts
.artifacts/

View File

@@ -76,6 +76,12 @@ http://localhost:8080
http://<this-machine-lan-ip>:8080 http://<this-machine-lan-ip>:8080
``` ```
For preprod deployment, configure the `POSTGRES_PASSWORD`, `RESEND_API_KEY`,
`RESEND_FROM_EMAIL`, and `JWT_SIGNING_KEY` Gitea secrets.
The deploy workflow writes the remote `.env` file and syncs `deploy/compose.yml`
before running the server deploy script.
Use the raw Resend API key value for `RESEND_API_KEY`, without a `Bearer ` prefix.
## Solution ## Solution
```bash ```bash
@@ -90,6 +96,24 @@ cd frontend
npm run build npm run build
``` ```
## Database Diagram
Start PostgreSQL, then generate a local schema diagram:
```bash
./scripts/generate-db-diagram.sh
```
The script writes an HTML viewer, SVG, PNG, and Graphviz source under:
```txt
.artifacts/db-diagrams/
```
Use `DATABASE_URL`, `PGPASSWORD`, or `~/.pgpass` to provide local database credentials.
When using the repository infrastructure script, the diagram script can read from the
running `socialize-postgres` container directly.
## Agentic Workflow ## Agentic Workflow
Start here: Start here:

View File

@@ -1,82 +0,0 @@
name: Build
on:
pull_request:
branches: [ main ]
paths-ignore:
- '.scripts/**'
- .gitignore
- CODE_OF_CONDUCT.md
- LICENSE
- README.md
workflow_call:
inputs:
build-artifacts:
type: boolean
required: true
default: false
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
name: Checkout code
- name: Cache NuGet packages
uses: actions/cache@v3
with:
path: ~/.nuget/packages
key: ${{ runner.os }}-nuget-${{ hashFiles('**/packages.lock.json') }}
restore-keys: |
${{ runner.os }}-nuget-
- name: Install .NET
uses: actions/setup-dotnet@v3
- name: Restore solution
run: dotnet restore
- name: Build solution
run: dotnet build --no-restore --configuration Release
- name: Test solution
run: dotnet test --no-build --configuration Release --filter "FullyQualifiedName!~AcceptanceTests"
- name: Publish website
if: ${{ inputs.build-artifacts == true }}
run: |
dotnet publish --configuration Release --runtime win-x86 --self-contained --output ./publish
cd publish
zip -r ./publish.zip .
working-directory: ./src/Web/
- name: Upload website artifact (website)
if: ${{ inputs.build-artifacts == true }}
uses: actions/upload-artifact@v3
with:
name: website
path: ./src/Web/publish/publish.zip
if-no-files-found: error
- name: Create EF Core migrations bundle
if: ${{ inputs.build-artifacts == true }}
run: |
dotnet new tool-manifest
dotnet tool install dotnet-ef
dotnet ef migrations bundle --configuration Release -p ./src/Infrastructure/ -s ./src/Web/ -o efbundle.exe
zip -r ./efbundle.zip efbundle.exe
env:
SkipNSwag: True
- name: Upload EF Core migrations bundle artifact (efbundle)
if: ${{ inputs.build-artifacts == true }}
uses: actions/upload-artifact@v3
with:
name: efbundle
path: ./efbundle.zip
if-no-files-found: error

View File

@@ -1,42 +0,0 @@
name: CICD
on:
push:
branches: [ main ]
paths-ignore:
- .gitignore
- CODE_OF_CONDUCT.md
- LICENSE
- README.md
permissions:
id-token: write
contents: read
jobs:
build:
uses: ./.github/workflows/build.yml
with:
build-artifacts: true
deploy-development:
uses: ./.github/workflows/deploy.yml
secrets: inherit
needs: [ build ]
with:
environmentName: Development
deploy-staging:
uses: ./.github/workflows/deploy.yml
secrets: inherit
needs: [ deploy-development ]
with:
environmentName: Staging
deploy-production:
uses: ./.github/workflows/deploy.yml
secrets: inherit
needs: [ deploy-staging ]
with:
environmentName: Production

View File

@@ -1,107 +0,0 @@
name: Deploy
on:
workflow_call:
inputs:
environmentName:
required: true
type: string
permissions:
id-token: write
contents: read
jobs:
validate:
runs-on: ubuntu-latest
environment: ${{ inputs.environmentName }}
steps:
- uses: actions/checkout@v3
name: Checkout code
- uses: azure/login@v1
name: Login to Azure
with:
client-id: ${{ vars.AZURE_CLIENT_ID }}
tenant-id: ${{ vars.AZURE_TENANT_ID }}
subscription-id: ${{ vars.AZURE_SUBSCRIPTION_ID }}
- if: inputs.environmentName == 'Development'
uses: azure/arm-deploy@v1
name: Run preflight validation
with:
deploymentName: ${{ github.run_number }}
resourceGroupName: ${{ vars.AZURE_RESOURCE_GROUP_NAME }}
template: ./.azure/bicep/main.bicep
parameters: >
environmentName=${{ inputs.environmentName }}
sqlAdministratorUsername=${{ vars.AZURE_SQL_ADMINISTRATOR_USERNAME }}
sqlAdministratorPassword=${{ secrets.AZURE_SQL_ADMINISTRATOR_PASSWORD }}
projectName=${{ vars.PROJECT_NAME }}
deploymentMode: Validate
- if: inputs.environmentName != 'Development'
uses: azure/arm-deploy@v1
name: Run what-if
with:
failOnStdErr: false
resourceGroupName: ${{ vars.AZURE_RESOURCE_GROUP_NAME }}
template: ./.azure/bicep/main.bicep
parameters: >
environmentName=${{ inputs.environmentName }}
sqlAdministratorUsername=${{ vars.AZURE_SQL_ADMINISTRATOR_USERNAME }}
sqlAdministratorPassword=${{ secrets.AZURE_SQL_ADMINISTRATOR_PASSWORD }}
projectName=${{ vars.PROJECT_NAME }}
additionalArguments: --what-if
deploy:
needs: [ validate ]
runs-on: ubuntu-latest
environment: ${{ inputs.environmentName }}
steps:
- uses: actions/checkout@v3
name: Checkout code
- uses: actions/download-artifact@v3
name: Download artifacts
- name: Install .NET
uses: actions/setup-dotnet@v3
- uses: azure/login@v1
name: Login to Azure
with:
client-id: ${{ vars.AZURE_CLIENT_ID }}
tenant-id: ${{ vars.AZURE_TENANT_ID }}
subscription-id: ${{ vars.AZURE_SUBSCRIPTION_ID }}
- uses: azure/arm-deploy@v1
id: deploy
name: Deploy infrastructure
with:
failOnStdErr: false
deploymentName: ${{ github.run_number }}
resourceGroupName: ${{ vars.AZURE_RESOURCE_GROUP_NAME }}
template: ./.azure/bicep/main.bicep
parameters: >
environmentName=${{ inputs.environmentName }}
sqlAdministratorUsername=${{ vars.AZURE_SQL_ADMINISTRATOR_USERNAME }}
sqlAdministratorPassword=${{ secrets.AZURE_SQL_ADMINISTRATOR_PASSWORD }}
projectName=${{ vars.PROJECT_NAME }}
- name: Initialise database
run: |
unzip -o ./efbundle/efbundle.zip
echo '{ "ConnectionStrings": { "DefaultConnection": "" } }' > appsettings.json
./efbundle.exe --connection "Server=${{ steps.deploy.outputs.sqlServerFullyQualifiedDomainName }};Initial Catalog=${{ steps.deploy.outputs.sqlDatabaseName }};Persist Security Info=False;User ID=${{ vars.AZURE_SQL_ADMINISTRATOR_USERNAME }};Password=${{ secrets.AZURE_SQL_ADMINISTRATOR_PASSWORD }};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" --verbose
- uses: azure/webapps-deploy@v2
name: Deploy website
with:
app-name: ${{ steps.deploy.outputs.appServiceAppName }}
package: website/publish.zip

View File

@@ -11,7 +11,7 @@ using Microsoft.IdentityModel.Tokens;
namespace Socialize; namespace Socialize;
public static class DependencyInjection internal static class ApplicationRegistration
{ {
public static IServiceCollection AddWebServices(this IServiceCollection services) public static IServiceCollection AddWebServices(this IServiceCollection services)
{ {
@@ -70,7 +70,6 @@ public static class DependencyInjection
{ {
authenticationBuilder.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, jwtBearerOptions => authenticationBuilder.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, jwtBearerOptions =>
{ {
jwtBearerOptions.Authority = "https://hutopy.com";
jwtBearerOptions.TokenValidationParameters = new TokenValidationParameters jwtBearerOptions.TokenValidationParameters = new TokenValidationParameters
{ {
ValidateIssuer = true, ValidateIssuer = true,
@@ -79,7 +78,7 @@ public static class DependencyInjection
ValidAudience = authJwt["Audience"], ValidAudience = authJwt["Audience"],
ValidateLifetime = true, ValidateLifetime = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(authJwt["Key"] ?? IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(authJwt["Key"] ??
throw new ArgumentNullException("The Jwt Key is missing."))) throw new InvalidOperationException("Authentication:Jwt:Key is required.")))
}; };
}); });
} }
@@ -90,9 +89,9 @@ public static class DependencyInjection
authenticationBuilder.AddGoogle(GoogleDefaults.AuthenticationScheme, options => authenticationBuilder.AddGoogle(GoogleDefaults.AuthenticationScheme, options =>
{ {
options.ClientId = authGoogle["ClientId"] ?? options.ClientId = authGoogle["ClientId"] ??
throw new ArgumentNullException("The Google ClientId is missing."); throw new InvalidOperationException("Authentication:Google:ClientId is required.");
options.ClientSecret = authGoogle["ClientSecret"] ?? options.ClientSecret = authGoogle["ClientSecret"] ??
throw new ArgumentNullException("The Google ClientSecret is missing."); throw new InvalidOperationException("Authentication:Google:ClientSecret is required.");
}); });
} }
@@ -102,9 +101,9 @@ public static class DependencyInjection
authenticationBuilder.AddFacebook(FacebookDefaults.AuthenticationScheme, options => authenticationBuilder.AddFacebook(FacebookDefaults.AuthenticationScheme, options =>
{ {
options.ClientId = authFacebook["ClientId"] ?? options.ClientId = authFacebook["ClientId"] ??
throw new ArgumentNullException("The Facebook ClientId is missing."); throw new InvalidOperationException("Authentication:Facebook:ClientId is required.");
options.ClientSecret = authFacebook["ClientSecret"] ?? options.ClientSecret = authFacebook["ClientSecret"] ??
throw new ArgumentNullException("The Facebook ClientSecret is missing."); throw new InvalidOperationException("Authentication:Facebook:ClientSecret is required.");
}); });
} }

View File

@@ -1,6 +1,6 @@
namespace Socialize.Api.Common.Domain; namespace Socialize.Api.Common.Domain;
public abstract class Entity internal abstract class Entity
{ {
public Guid Id { get; init; } public Guid Id { get; init; }
public Guid CreatedBy { get; init; } public Guid CreatedBy { get; init; }

View File

@@ -2,6 +2,7 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Socialize.Api.Modules.Approvals.Data; using Socialize.Api.Modules.Approvals.Data;
using Socialize.Api.Modules.Assets.Data; using Socialize.Api.Modules.Assets.Data;
using Socialize.Api.Modules.Channels.Data;
using Socialize.Api.Modules.Clients.Data; using Socialize.Api.Modules.Clients.Data;
using Socialize.Api.Modules.Comments.Data; using Socialize.Api.Modules.Comments.Data;
using Socialize.Api.Modules.ContentItems.Data; using Socialize.Api.Modules.ContentItems.Data;
@@ -9,23 +10,30 @@ using Socialize.Api.Modules.Feedback.Data;
using Socialize.Api.Modules.Identity.Data; using Socialize.Api.Modules.Identity.Data;
using Socialize.Api.Modules.Notifications.Data; using Socialize.Api.Modules.Notifications.Data;
using Socialize.Api.Modules.Campaigns.Data; using Socialize.Api.Modules.Campaigns.Data;
using Socialize.Api.Modules.CalendarIntegrations.Data;
using Socialize.Api.Modules.Organizations.Data; using Socialize.Api.Modules.Organizations.Data;
using Socialize.Api.Modules.ReleaseCommunications.Data;
using Socialize.Api.Modules.Workspaces.Data; using Socialize.Api.Modules.Workspaces.Data;
namespace Socialize.Api.Data; namespace Socialize.Api.Data;
public class AppDbContext( internal class AppDbContext(
DbContextOptions<AppDbContext> options) DbContextOptions<AppDbContext> options)
: IdentityDbContext<User, Role, Guid>(options) : IdentityDbContext<User, Role, Guid>(options)
{ {
public DbSet<Organization> Organizations => Set<Organization>(); public DbSet<Organization> Organizations => Set<Organization>();
public DbSet<OrganizationMembershipTier> OrganizationMembershipTiers => Set<OrganizationMembershipTier>();
public DbSet<OrganizationMembershipTierTranslation> OrganizationMembershipTierTranslations =>
Set<OrganizationMembershipTierTranslation>();
public DbSet<OrganizationMembership> OrganizationMemberships => Set<OrganizationMembership>(); public DbSet<OrganizationMembership> OrganizationMemberships => Set<OrganizationMembership>();
public DbSet<Workspace> Workspaces => Set<Workspace>(); public DbSet<Workspace> Workspaces => Set<Workspace>();
public DbSet<WorkspaceInvite> WorkspaceInvites => Set<WorkspaceInvite>(); public DbSet<WorkspaceInvite> WorkspaceInvites => Set<WorkspaceInvite>();
public DbSet<Channel> Channels => Set<Channel>();
public DbSet<Client> Clients => Set<Client>(); public DbSet<Client> Clients => Set<Client>();
public DbSet<Campaign> Campaigns => Set<Campaign>(); public DbSet<Campaign> Campaigns => Set<Campaign>();
public DbSet<ContentItem> ContentItems => Set<ContentItem>(); public DbSet<ContentItem> ContentItems => Set<ContentItem>();
public DbSet<ContentItemRevision> ContentItemRevisions => Set<ContentItemRevision>(); public DbSet<ContentItemRevision> ContentItemRevisions => Set<ContentItemRevision>();
public DbSet<ContentItemActivityEntry> ContentItemActivityEntries => Set<ContentItemActivityEntry>();
public DbSet<Asset> Assets => Set<Asset>(); public DbSet<Asset> Assets => Set<Asset>();
public DbSet<AssetRevision> AssetRevisions => Set<AssetRevision>(); public DbSet<AssetRevision> AssetRevisions => Set<AssetRevision>();
public DbSet<Comment> Comments => Set<Comment>(); public DbSet<Comment> Comments => Set<Comment>();
@@ -39,6 +47,14 @@ public class AppDbContext(
public DbSet<FeedbackScreenshot> FeedbackScreenshots => Set<FeedbackScreenshot>(); public DbSet<FeedbackScreenshot> FeedbackScreenshots => Set<FeedbackScreenshot>();
public DbSet<FeedbackComment> FeedbackComments => Set<FeedbackComment>(); public DbSet<FeedbackComment> FeedbackComments => Set<FeedbackComment>();
public DbSet<FeedbackActivityEntry> FeedbackActivityEntries => Set<FeedbackActivityEntry>(); public DbSet<FeedbackActivityEntry> FeedbackActivityEntries => Set<FeedbackActivityEntry>();
public DbSet<CalendarSource> CalendarSources => Set<CalendarSource>();
public DbSet<CalendarCatalogEntry> CalendarCatalogEntries => Set<CalendarCatalogEntry>();
public DbSet<CalendarEvent> CalendarEvents => Set<CalendarEvent>();
public DbSet<UserCalendarExportFeed> UserCalendarExportFeeds => Set<UserCalendarExportFeed>();
public DbSet<ReleaseUpdate> ReleaseUpdates => Set<ReleaseUpdate>();
public DbSet<ReleaseUpdateReadReceipt> ReleaseUpdateReadReceipts => Set<ReleaseUpdateReadReceipt>();
public DbSet<ReleaseCommit> ReleaseCommits => Set<ReleaseCommit>();
public DbSet<ReleaseUpdateEmailDigestReceipt> ReleaseUpdateEmailDigestReceipts => Set<ReleaseUpdateEmailDigestReceipt>();
protected override void OnModelCreating(ModelBuilder builder) protected override void OnModelCreating(ModelBuilder builder)
{ {
@@ -46,6 +62,7 @@ public class AppDbContext(
builder.ConfigureOrganizationsModule(); builder.ConfigureOrganizationsModule();
builder.ConfigureWorkspacesModule(); builder.ConfigureWorkspacesModule();
builder.ConfigureChannelsModule();
builder.ConfigureClientsModule(); builder.ConfigureClientsModule();
builder.ConfigureCampaignsModule(); builder.ConfigureCampaignsModule();
builder.ConfigureContentItemsModule(); builder.ConfigureContentItemsModule();
@@ -54,5 +71,7 @@ public class AppDbContext(
builder.ConfigureApprovalsModule(); builder.ConfigureApprovalsModule();
builder.ConfigureNotificationsModule(); builder.ConfigureNotificationsModule();
builder.ConfigureFeedbackModule(); builder.ConfigureFeedbackModule();
builder.ConfigureCalendarIntegrationsModule();
builder.ConfigureReleaseCommunicationsModule();
} }
} }

View File

@@ -1,5 +0,0 @@
<wpf:ResourceDictionary xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib"
xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xml:space="preserve">
<s:Boolean x:Key="/Default/UserDictionary/Words/=hutopy/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

View File

@@ -1,6 +1,6 @@
namespace Socialize.Api.Infrastructure.BlobStorage.Configuration; namespace Socialize.Api.Infrastructure.BlobStorage.Configuration;
public sealed class LocalBlobStorageOptions internal sealed class LocalBlobStorageOptions
{ {
public const string SectionName = "LocalBlobStorage"; public const string SectionName = "LocalBlobStorage";

View File

@@ -1,6 +1,6 @@
namespace Socialize.Api.Infrastructure.BlobStorage.Contracts; namespace Socialize.Api.Infrastructure.BlobStorage.Contracts;
public static class CommonFileNames internal static class CommonFileNames
{ {
public const string ProfilePicture = "profilePicture"; public const string ProfilePicture = "profilePicture";
public const string LogoPicture = "logoPicture"; public const string LogoPicture = "logoPicture";

View File

@@ -4,6 +4,7 @@ internal static class ContainerNames
{ {
public const string Users = "users"; public const string Users = "users";
public const string Clients = "clients"; public const string Clients = "clients";
public const string Organizations = "organizations";
public const string Workspaces = "workspaces"; public const string Workspaces = "workspaces";
public const string Creators = "creators"; public const string Creators = "creators";
public const string Feedback = "feedback"; public const string Feedback = "feedback";

View File

@@ -2,7 +2,7 @@
namespace Socialize.Api.Infrastructure.BlobStorage.Contracts; namespace Socialize.Api.Infrastructure.BlobStorage.Contracts;
public static class ContentTypes internal static class ContentTypes
{ {
private const string ImagePng = "image/png"; private const string ImagePng = "image/png";
private const string ImageJpeg = "image/jpeg"; private const string ImageJpeg = "image/jpeg";
@@ -39,6 +39,6 @@ public static class ContentTypes
// Check for HTML content by looking for "<!DOCTYPE html>" or "<html>" tags // Check for HTML content by looking for "<!DOCTYPE html>" or "<html>" tags
string content = Encoding.UTF8.GetString(buffer); string content = Encoding.UTF8.GetString(buffer);
return content.Contains("<!DOCTYPE html>"); return content.Contains("<!DOCTYPE html>", StringComparison.OrdinalIgnoreCase);
} }
} }

View File

@@ -1,6 +1,6 @@
namespace Socialize.Api.Infrastructure.BlobStorage.Contracts; namespace Socialize.Api.Infrastructure.BlobStorage.Contracts;
public interface IBlobStorage internal interface IBlobStorage
{ {
/// <summary> /// <summary>
/// Upload a file to blob storage. /// Upload a file to blob storage.

View File

@@ -1,6 +1,6 @@
namespace Socialize.Api.Infrastructure.BlobStorage.Contracts; namespace Socialize.Api.Infrastructure.BlobStorage.Contracts;
public static class SubDirectoryNames internal static class SubDirectoryNames
{ {
public const string Profile = "profile"; public const string Profile = "profile";
public const string Contents = "contents"; public const string Contents = "contents";

View File

@@ -4,7 +4,7 @@ using Socialize.Api.Infrastructure.BlobStorage.Contracts;
namespace Socialize.Api.Infrastructure.BlobStorage.Services; namespace Socialize.Api.Infrastructure.BlobStorage.Services;
public sealed class LocalBlobStorage( internal sealed class LocalBlobStorage(
IWebHostEnvironment environment, IWebHostEnvironment environment,
IHttpContextAccessor httpContextAccessor, IHttpContextAccessor httpContextAccessor,
IOptions<LocalBlobStorageOptions> options, IOptions<LocalBlobStorageOptions> options,
@@ -14,6 +14,14 @@ public sealed class LocalBlobStorage(
private const long MaxUploadSize = 10 * 1024 * 1024; private const long MaxUploadSize = 10 * 1024 * 1024;
private const string ContentTypeMetadataSuffix = ".content-type"; private const string ContentTypeMetadataSuffix = ".content-type";
private static readonly char[] PathSeparators = [Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar];
private static readonly Action<ILogger, string, string, string, string, Exception?> LogUploadedFile =
LoggerMessage.Define<string, string, string, string>(
LogLevel.Information,
new EventId(1, nameof(UploadFileAsync)),
"Blob storage: Uploaded [{BlobName}] to local container [{ContainerName}] with contentType [{ContentType}] and uri [{FileUri}]");
private readonly LocalBlobStorageOptions _options = options.Value; private readonly LocalBlobStorageOptions _options = options.Value;
public async Task<string> UploadFileAsync( public async Task<string> UploadFileAsync(
@@ -46,12 +54,7 @@ public sealed class LocalBlobStorage(
await File.WriteAllTextAsync(GetContentTypeMetadataPath(filePath), contentType, ct); await File.WriteAllTextAsync(GetContentTypeMetadataPath(filePath), contentType, ct);
string fileUri = BuildPublicUrl(relativePath); string fileUri = BuildPublicUrl(relativePath);
logger.LogInformation( LogUploadedFile(logger, blobName, containerName, contentType, fileUri, null);
"Blob storage: Uploaded [{BlobName}] to local container [{ContainerName}] with contentType [{ContentType}] and uri [{FileUri}]",
blobName,
containerName,
contentType,
fileUri);
return fileUri; return fileUri;
} }
@@ -106,7 +109,7 @@ public sealed class LocalBlobStorage(
throw new InvalidOperationException("Blob storage: Blob paths must be relative."); throw new InvalidOperationException("Blob storage: Blob paths must be relative.");
} }
string[] pathParts = [containerName, .. blobName.Split([Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar])]; string[] pathParts = [containerName, .. blobName.Split(PathSeparators)];
if (pathParts.Any(part => part is "" or "." or "..")) if (pathParts.Any(part => part is "" or "." or ".."))
{ {
throw new InvalidOperationException("Blob storage: Blob paths must not contain relative path segments."); throw new InvalidOperationException("Blob storage: Blob paths must not contain relative path segments.");
@@ -135,7 +138,7 @@ public sealed class LocalBlobStorage(
? "/api/storage" ? "/api/storage"
: requestPath.Trim(); : requestPath.Trim();
return normalized.StartsWith("/", StringComparison.Ordinal) return normalized.StartsWith('/')
? normalized.TrimEnd('/') ? normalized.TrimEnd('/')
: $"/{normalized.TrimEnd('/')}"; : $"/{normalized.TrimEnd('/')}";
} }

View File

@@ -1,6 +1,6 @@
namespace Socialize.Api.Infrastructure.Configuration; namespace Socialize.Api.Infrastructure.Configuration;
public class WebsiteOptions internal class WebsiteOptions
{ {
public const string SectionName = "Website"; public const string SectionName = "Website";

View File

@@ -1,8 +0,0 @@
namespace Socialize.Api.Infrastructure.Development;
public record DevelopmentSeedOptions
{
public const string SectionName = "DevelopmentSeed";
public bool Enabled { get; init; } = true;
}

View File

@@ -1,6 +1,6 @@
namespace Socialize.Api.Infrastructure.Emailer.Configuration; namespace Socialize.Api.Infrastructure.Emailer.Configuration;
public class EmailerOptions internal class EmailerOptions
{ {
public const string ConfigurationSection = "Emailer"; public const string ConfigurationSection = "Emailer";

View File

@@ -1,6 +1,6 @@
namespace Socialize.Api.Infrastructure.Emailer.Contracts; namespace Socialize.Api.Infrastructure.Emailer.Contracts;
public interface IEmailSender internal interface IEmailSender
{ {
Task SendEmailAsync(string email, string subject, string message); Task SendEmailAsync(string email, string subject, string message);
} }

View File

@@ -2,21 +2,19 @@ using Socialize.Api.Infrastructure.Emailer.Contracts;
namespace Socialize.Api.Infrastructure.Emailer.Services; namespace Socialize.Api.Infrastructure.Emailer.Services;
public class LoggerEmailSender(ILogger<IEmailSender> logger) internal class LoggerEmailSender(ILogger<IEmailSender> logger)
: IEmailSender : IEmailSender
{ {
public async Task SendEmailAsync(string email, string subject, string message) private static readonly Action<ILogger, string, string, string, string, Exception?> LogDevelopmentEmail =
LoggerMessage.Define<string, string, string, string>(
LogLevel.Information,
new EventId(1, nameof(SendEmailAsync)),
"Development email to {Email} with subject {Subject}:{NewLine}{Message}");
public Task SendEmailAsync(string email, string subject, string message)
{ {
try LogDevelopmentEmail(logger, email, subject, Environment.NewLine, message, null);
{
logger.LogInformation("Sending email to {Email} with subject: {Subject}", email, subject); return Task.CompletedTask;
await Task.Delay(1000);
logger.LogInformation("Email sent successfully to {Email}", email);
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to send email to {Email}", email);
throw;
}
} }
} }

View File

@@ -5,7 +5,7 @@ using PostmarkDotNet;
namespace Socialize.Api.Infrastructure.Emailer.Services; namespace Socialize.Api.Infrastructure.Emailer.Services;
public class PostmarkEmailSender : IEmailSender internal class PostmarkEmailSender : IEmailSender
{ {
private readonly PostmarkClient _client; private readonly PostmarkClient _client;
private readonly EmailerOptions _options; private readonly EmailerOptions _options;

View File

@@ -7,7 +7,7 @@ using Microsoft.Extensions.Options;
namespace Socialize.Api.Infrastructure.Emailer.Services; namespace Socialize.Api.Infrastructure.Emailer.Services;
public class ResendEmailSender : IEmailSender internal class ResendEmailSender : IEmailSender
{ {
private static readonly Uri EndpointUri = new("https://api.resend.com/emails"); private static readonly Uri EndpointUri = new("https://api.resend.com/emails");
private readonly HttpClient _httpClient; private readonly HttpClient _httpClient;
@@ -20,21 +20,36 @@ public class ResendEmailSender : IEmailSender
_httpClient = httpClientFactory.CreateClient(); _httpClient = httpClientFactory.CreateClient();
_options = options.Value; _options = options.Value;
string apiKey = NormalizeApiKey(_options.ApiKey);
string fromEmail = _options.FromEmail?.Trim() ?? string.Empty;
if (string.IsNullOrWhiteSpace(apiKey))
{
throw new InvalidOperationException("Emailer:ApiKey is required when using Resend email delivery.");
}
if (string.IsNullOrWhiteSpace(fromEmail))
{
throw new InvalidOperationException("Emailer:FromEmail is required when using Resend email delivery.");
}
_options.ApiKey = apiKey;
_options.FromEmail = fromEmail;
_httpClient.DefaultRequestHeaders.Authorization = _httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", _options.ApiKey); new AuthenticationHeaderValue("Bearer", apiKey);
_httpClient.DefaultRequestHeaders.Accept.Add( _httpClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json")); new MediaTypeWithQualityHeaderValue("application/json"));
} }
public async Task SendEmailAsync(string toEmail, string subject, string htmlMessage) public async Task SendEmailAsync(string email, string subject, string message)
{ {
var payload = new { from = _options.FromEmail, to = toEmail, subject, html = htmlMessage }; var payload = new { from = _options.FromEmail, to = email, subject, html = message };
string json = JsonSerializer.Serialize(payload); string json = JsonSerializer.Serialize(payload);
StringContent content = new(json, Encoding.UTF8, "application/json"); using StringContent content = new(json, Encoding.UTF8, "application/json");
using HttpResponseMessage response = await _httpClient.PostAsync(EndpointUri, content);
HttpResponseMessage response = await _httpClient.PostAsync(EndpointUri, content);
if (!response.IsSuccessStatusCode) if (!response.IsSuccessStatusCode)
{ {
@@ -43,4 +58,16 @@ public class ResendEmailSender : IEmailSender
$"Resend email failed: {response.StatusCode} - {body}"); $"Resend email failed: {response.StatusCode} - {body}");
} }
} }
private static string NormalizeApiKey(string? apiKey)
{
string normalized = apiKey?.Trim().Trim('"', '\'') ?? string.Empty;
const string bearerPrefix = "Bearer ";
if (normalized.StartsWith(bearerPrefix, StringComparison.OrdinalIgnoreCase))
{
normalized = normalized[bearerPrefix.Length..].Trim();
}
return normalized;
}
} }

View File

@@ -9,7 +9,7 @@ using Socialize.Api.Infrastructure.Payments.Stripe.Configuration;
namespace Socialize.Api.Infrastructure; namespace Socialize.Api.Infrastructure;
public static class DependencyInjection internal static class InfrastructureRegistration
{ {
public static WebApplicationBuilder AddInfrastructureModule( public static WebApplicationBuilder AddInfrastructureModule(
this WebApplicationBuilder builder) this WebApplicationBuilder builder)
@@ -26,8 +26,14 @@ public static class DependencyInjection
builder.Services.Configure<EmailerOptions>( builder.Services.Configure<EmailerOptions>(
builder.Configuration.GetSection(EmailerOptions.ConfigurationSection)); builder.Configuration.GetSection(EmailerOptions.ConfigurationSection));
if (builder.Environment.IsDevelopment())
{
builder.Services.AddTransient<IEmailSender, LoggerEmailSender>();
}
else
{
builder.Services.AddTransient<IEmailSender, ResendEmailSender>(); builder.Services.AddTransient<IEmailSender, ResendEmailSender>();
//builder.Services.AddTransient<IEmailSender, EmailSender>(); }
builder.Services.AddHttpClient(); builder.Services.AddHttpClient();

View File

@@ -2,7 +2,7 @@ using System.ComponentModel.DataAnnotations;
namespace Socialize.Api.Infrastructure.Payments.Stripe.Configuration; namespace Socialize.Api.Infrastructure.Payments.Stripe.Configuration;
public class StripeOptions internal class StripeOptions
{ {
public const string ConfigurationSection = "Stripe"; public const string ConfigurationSection = "Stripe";

View File

@@ -4,52 +4,52 @@ using Socialize.Api.Modules.Organizations.Services;
namespace Socialize.Api.Infrastructure.Security; namespace Socialize.Api.Infrastructure.Security;
public sealed class AccessScopeService( internal sealed class AccessScopeService(
OrganizationAccessService organizationAccessService) OrganizationAccessService organizationAccessService)
{ {
public bool IsManager(ClaimsPrincipal user) public static bool IsManager(ClaimsPrincipal user)
{ {
return user.IsInRole(KnownRoles.Administrator) || user.IsInRole(KnownRoles.Manager); return user.IsInRole(KnownRoles.Administrator) || user.IsInRole(KnownRoles.Manager);
} }
public bool IsProvider(ClaimsPrincipal user) public static bool IsProvider(ClaimsPrincipal user)
{ {
return user.IsInRole(KnownRoles.Provider); return user.IsInRole(KnownRoles.Provider);
} }
public bool IsClient(ClaimsPrincipal user) public static bool IsClient(ClaimsPrincipal user)
{ {
return user.IsInRole(KnownRoles.Client); return user.IsInRole(KnownRoles.Client);
} }
public bool CanAccessWorkspace(ClaimsPrincipal user, Guid workspaceId) public static bool CanAccessWorkspace(ClaimsPrincipal user, Guid workspaceId)
{ {
return IsManager(user) || user.GetWorkspaceScopeIds().Contains(workspaceId); return IsManager(user) || user.GetWorkspaceScopeIds().Contains(workspaceId);
} }
public bool CanManageWorkspace(ClaimsPrincipal user, Guid workspaceId) public static bool CanManageWorkspace(ClaimsPrincipal user, Guid workspaceId)
{ {
return IsManager(user) && CanAccessWorkspace(user, workspaceId); return IsManager(user) && CanAccessWorkspace(user, workspaceId);
} }
public bool CanAccessClient(ClaimsPrincipal user, Guid workspaceId, Guid clientId) public static bool CanAccessClient(ClaimsPrincipal user, Guid workspaceId, Guid clientId)
{ {
return IsManager(user) return IsManager(user)
|| (CanAccessWorkspace(user, workspaceId) && user.GetClientScopeIds().Contains(clientId)); || (CanAccessWorkspace(user, workspaceId) && user.GetClientScopeIds().Contains(clientId));
} }
public bool CanAccessCampaign(ClaimsPrincipal user, Guid workspaceId, Guid clientId, Guid campaignId) public static bool CanAccessCampaign(ClaimsPrincipal user, Guid workspaceId, Guid clientId, Guid campaignId)
{ {
return IsManager(user) return IsManager(user)
|| (CanAccessClient(user, workspaceId, clientId) && user.GetCampaignScopeIds().Contains(campaignId)); || (CanAccessClient(user, workspaceId, clientId) && user.GetCampaignScopeIds().Contains(campaignId));
} }
public bool CanContributeToCampaign(ClaimsPrincipal user, Guid workspaceId, Guid clientId, Guid campaignId) public static bool CanContributeToCampaign(ClaimsPrincipal user, Guid workspaceId, Guid clientId, Guid campaignId)
{ {
return IsManager(user) || (IsProvider(user) && CanAccessCampaign(user, workspaceId, clientId, campaignId)); return IsManager(user) || (IsProvider(user) && CanAccessCampaign(user, workspaceId, clientId, campaignId));
} }
public bool CanReviewContent(ClaimsPrincipal user, Guid workspaceId, Guid clientId, Guid campaignId) public static bool CanReviewContent(ClaimsPrincipal user, Guid workspaceId, Guid clientId, Guid campaignId)
{ {
return IsManager(user) return IsManager(user)
|| IsProvider(user) && CanAccessCampaign(user, workspaceId, clientId, campaignId) || IsProvider(user) && CanAccessCampaign(user, workspaceId, clientId, campaignId)

View File

@@ -1,8 +1,9 @@
using System.Security.Claims; using System.Globalization;
using System.Security.Claims;
namespace Socialize.Api.Infrastructure.Security; namespace Socialize.Api.Infrastructure.Security;
public static class ClaimsPrincipalExtensions internal static class ClaimsPrincipalExtensions
{ {
public static IReadOnlyCollection<Guid> GetScopeIds(this ClaimsPrincipal claims, string key) public static IReadOnlyCollection<Guid> GetScopeIds(this ClaimsPrincipal claims, string key)
{ {
@@ -81,11 +82,11 @@ public static class ClaimsPrincipalExtensions
if (claim is null) if (claim is null)
{ {
throw new MissingClaimException(key); throw MissingClaimException.ForClaim(key);
} }
return typeof(TValue) == typeof(Guid) return typeof(TValue) == typeof(Guid)
? Guid.Parse(claim.Value) ? Guid.Parse(claim.Value)
: Convert.ChangeType(claim.Value, typeof(TValue)); : Convert.ChangeType(claim.Value, typeof(TValue), CultureInfo.InvariantCulture);
} }
} }

View File

@@ -5,7 +5,7 @@ using Microsoft.IdentityModel.Tokens;
namespace Socialize.Api.Infrastructure.Security; namespace Socialize.Api.Infrastructure.Security;
public static class JwtTokenHelper internal static class JwtTokenHelper
{ {
public static string GenerateJwtToken( public static string GenerateJwtToken(
TimeSpan expiresIn, TimeSpan expiresIn,

View File

@@ -1,6 +1,6 @@
namespace Socialize.Api.Infrastructure.Security; namespace Socialize.Api.Infrastructure.Security;
public static class KnownClaims internal static class KnownClaims
{ {
public const string Alias = "alias"; public const string Alias = "alias";
public const string PortraitUrl = "portraitUrl"; public const string PortraitUrl = "portraitUrl";

View File

@@ -1,5 +1,23 @@
namespace Socialize.Api.Infrastructure.Security; namespace Socialize.Api.Infrastructure.Security;
public class MissingClaimException( public class MissingClaimException : Exception
string claimName) {
: Exception($"Claim '{claimName}' is missing."); public MissingClaimException()
{
}
public MissingClaimException(string message)
: base(message)
{
}
public MissingClaimException(string message, Exception innerException)
: base(message, innerException)
{
}
internal static MissingClaimException ForClaim(string claimName)
{
return new MissingClaimException($"Claim '{claimName}' is missing.");
}
}

View File

@@ -4,15 +4,13 @@ using System.Text;
namespace Socialize.Api.Infrastructure.Security; namespace Socialize.Api.Infrastructure.Security;
// If we need to add special characters we can alternate between 2 pools. // If we need to add special characters we can alternate between 2 pools.
public static class PasswordGenerator internal static class PasswordGenerator
{ {
private const string LowerLetters = "abcdefghijklmnopqrstuvwxyz"; private const string LowerLetters = "abcdefghijklmnopqrstuvwxyz";
private const string UpperLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; private const string UpperLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private const string Numbers = "0123456789"; private const string Numbers = "0123456789";
private const string SpecialCharacters = "!@#$%^&*()_+-=[];',./`~{}|:\"<>?"; private const string SpecialCharacters = "!@#$%^&*()_+-=[];',./`~{}|:\"<>?";
private static readonly Random Random = new();
public static string Next( public static string Next(
int length = 15, int length = 15,
bool requireNumber = true, bool requireNumber = true,
@@ -23,7 +21,7 @@ public static class PasswordGenerator
// Create pools based on the requirements // Create pools based on the requirements
StringBuilder characterPool = new(); StringBuilder characterPool = new();
if (requireNumber) if (requireLowercase)
{ {
characterPool.Append(LowerLetters); characterPool.Append(LowerLetters);
} }
@@ -51,22 +49,22 @@ public static class PasswordGenerator
if (requireLowercase) if (requireLowercase)
{ {
password[index++] = LowerLetters[Random.Next(LowerLetters.Length)]; password[index++] = LowerLetters[RandomNumberGenerator.GetInt32(LowerLetters.Length)];
} }
if (requireCapital) if (requireCapital)
{ {
password[index++] = UpperLetters[Random.Next(UpperLetters.Length)]; password[index++] = UpperLetters[RandomNumberGenerator.GetInt32(UpperLetters.Length)];
} }
if (requireNumber) if (requireNumber)
{ {
password[index++] = Numbers[Random.Next(Numbers.Length)]; password[index++] = Numbers[RandomNumberGenerator.GetInt32(Numbers.Length)];
} }
if (requireSpecialCharacter) if (requireSpecialCharacter)
{ {
password[index++] = SpecialCharacters[Random.Next(SpecialCharacters.Length)]; password[index++] = SpecialCharacters[RandomNumberGenerator.GetInt32(SpecialCharacters.Length)];
} }
// Fill the rest with the password // Fill the rest with the password
@@ -85,7 +83,7 @@ public static class PasswordGenerator
{ {
for (int i = array.Length - 1; i > 0; i--) for (int i = array.Length - 1; i > 0; i--)
{ {
int j = Random.Next(i + 1); int j = RandomNumberGenerator.GetInt32(i + 1);
(array[i], array[j]) = (array[j], array[i]); // Swap elements (array[i], array[j]) = (array[j], array[i]); // Swap elements
} }
} }

View File

@@ -2,7 +2,7 @@ using System.Security.Cryptography;
namespace Socialize.Api.Infrastructure.Security; namespace Socialize.Api.Infrastructure.Security;
public static class RefreshTokenGenerator internal static class RefreshTokenGenerator
{ {
public static string Next() public static string Next()
{ {

View File

@@ -6,6 +6,7 @@ using Socialize.Api.Modules.Identity.Contracts;
using Socialize.Api.Modules.Identity.Data; using Socialize.Api.Modules.Identity.Data;
using Socialize.Api.Modules.Assets.Data; using Socialize.Api.Modules.Assets.Data;
using Socialize.Api.Modules.Approvals.Data; using Socialize.Api.Modules.Approvals.Data;
using Socialize.Api.Modules.Channels.Data;
using Socialize.Api.Modules.Comments.Data; using Socialize.Api.Modules.Comments.Data;
using Socialize.Api.Modules.ContentItems.Data; using Socialize.Api.Modules.ContentItems.Data;
using Socialize.Api.Modules.Clients.Data; using Socialize.Api.Modules.Clients.Data;
@@ -14,19 +15,25 @@ using Socialize.Api.Modules.Campaigns.Data;
using Socialize.Api.Modules.Organizations.Data; using Socialize.Api.Modules.Organizations.Data;
using Socialize.Api.Modules.Organizations.Services; using Socialize.Api.Modules.Organizations.Services;
using Socialize.Api.Modules.Workspaces.Data; using Socialize.Api.Modules.Workspaces.Data;
using Socialize.Api.Modules.Workspaces.Services;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
namespace Socialize.Api.Infrastructure.Development; namespace Socialize.Api.Infrastructure.TestData;
public static class DevelopmentSeedExtensions #pragma warning disable S1075 // Test data intentionally uses representative external URLs.
internal static class TestDataSeedExtensions
{ {
private static readonly Guid OrganizationId = Guid.Parse("99999999-9999-9999-9999-999999999999"); private static readonly Guid OrganizationId = Guid.Parse("99999999-9999-9999-9999-999999999999");
private static readonly Guid WorkspaceId = Guid.Parse("11111111-1111-1111-1111-111111111111"); private static readonly Guid WorkspaceId = Guid.Parse("11111111-1111-1111-1111-111111111111");
private static readonly Guid AtlasWorkspaceId = Guid.Parse("11111111-1111-1111-1111-222222222222");
private static readonly Guid ScopedClientId = Guid.Parse("22222222-2222-2222-2222-222222222222"); private static readonly Guid ScopedClientId = Guid.Parse("22222222-2222-2222-2222-222222222222");
private static readonly Guid HiddenClientId = Guid.Parse("22222222-2222-2222-2222-333333333333"); private static readonly Guid HiddenClientId = Guid.Parse("22222222-2222-2222-2222-333333333333");
private static readonly Guid ScopedCampaignId = Guid.Parse("33333333-3333-3333-3333-333333333333"); private static readonly Guid ScopedCampaignId = Guid.Parse("33333333-3333-3333-3333-333333333333");
private static readonly Guid HiddenCampaignId = Guid.Parse("33333333-3333-3333-3333-444444444444"); private static readonly Guid HiddenCampaignId = Guid.Parse("33333333-3333-3333-3333-444444444444");
private static readonly Guid LumaInstagramChannelId = Guid.Parse("33333333-3333-3333-3333-000000000001");
private static readonly Guid LumaTikTokChannelId = Guid.Parse("33333333-3333-3333-3333-000000000002");
private static readonly Guid AtlasInstagramChannelId = Guid.Parse("33333333-3333-3333-3333-000000000003");
private static readonly Guid ScopedContentItemId = Guid.Parse("44444444-4444-4444-4444-444444444444"); private static readonly Guid ScopedContentItemId = Guid.Parse("44444444-4444-4444-4444-444444444444");
private static readonly Guid HiddenContentItemId = Guid.Parse("44444444-4444-4444-4444-555555555555"); private static readonly Guid HiddenContentItemId = Guid.Parse("44444444-4444-4444-4444-555555555555");
private static readonly Guid ScopedAssetId = Guid.Parse("55555555-5555-5555-5555-555555555555"); private static readonly Guid ScopedAssetId = Guid.Parse("55555555-5555-5555-5555-555555555555");
@@ -34,23 +41,11 @@ public static class DevelopmentSeedExtensions
private static readonly Guid ClientCommentId = Guid.Parse("77777777-7777-7777-7777-777777777777"); private static readonly Guid ClientCommentId = Guid.Parse("77777777-7777-7777-7777-777777777777");
private static readonly Guid NotificationId = Guid.Parse("88888888-8888-8888-8888-888888888888"); private static readonly Guid NotificationId = Guid.Parse("88888888-8888-8888-8888-888888888888");
public static async Task<IApplicationBuilder> UseDevelopmentSeedAsync( public static async Task<IServiceProvider> SeedTestDataAsync(
this IApplicationBuilder app, this IServiceProvider services,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
IHostEnvironment environment = app.ApplicationServices.GetRequiredService<IHostEnvironment>(); using IServiceScope scope = services.CreateScope();
if (!environment.IsDevelopment())
{
return app;
}
using IServiceScope scope = app.ApplicationServices.CreateScope();
IOptions<DevelopmentSeedOptions> options = scope.ServiceProvider.GetRequiredService<IOptions<DevelopmentSeedOptions>>();
if (!options.Value.Enabled)
{
return app;
}
UserManager userManager = scope.ServiceProvider.GetRequiredService<UserManager>(); UserManager userManager = scope.ServiceProvider.GetRequiredService<UserManager>();
AppDbContext dbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>(); AppDbContext dbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
@@ -59,7 +54,7 @@ public static class DevelopmentSeedExtensions
id: Guid.Parse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"), id: Guid.Parse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"),
username: "manager", username: "manager",
email: "manager@socialize.local", email: "manager@socialize.local",
password: "manager", password: "Manager1!",
alias: "Northstar Manager", alias: "Northstar Manager",
firstname: "Morgan", firstname: "Morgan",
lastname: "Reid", lastname: "Reid",
@@ -75,7 +70,7 @@ public static class DevelopmentSeedExtensions
id: Guid.Parse("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"), id: Guid.Parse("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"),
username: "client", username: "client",
email: "client@socialize.local", email: "client@socialize.local",
password: "client", password: "Client1!",
alias: "Sofia Martin", alias: "Sofia Martin",
firstname: "Sofia", firstname: "Sofia",
lastname: "Martin", lastname: "Martin",
@@ -92,7 +87,7 @@ public static class DevelopmentSeedExtensions
id: Guid.Parse("cccccccc-cccc-cccc-cccc-cccccccccccc"), id: Guid.Parse("cccccccc-cccc-cccc-cccc-cccccccccccc"),
username: "provider", username: "provider",
email: "provider@socialize.local", email: "provider@socialize.local",
password: "provider", password: "Provider1!",
alias: "Alex Studio", alias: "Alex Studio",
firstname: "Alex", firstname: "Alex",
lastname: "Studio", lastname: "Studio",
@@ -110,7 +105,7 @@ public static class DevelopmentSeedExtensions
id: Guid.Parse("dddddddd-dddd-dddd-dddd-dddddddddddd"), id: Guid.Parse("dddddddd-dddd-dddd-dddd-dddddddddddd"),
username: "dev", username: "dev",
email: "dev@socialize.local", email: "dev@socialize.local",
password: "dev", password: "Developer1!",
alias: "Socialize Dev", alias: "Socialize Dev",
firstname: "Jo", firstname: "Jo",
lastname: "Bumble", lastname: "Bumble",
@@ -133,7 +128,7 @@ public static class DevelopmentSeedExtensions
dbContext, dbContext,
cancellationToken); cancellationToken);
return app; return services;
} }
private static async Task<User> EnsureUserAsync( private static async Task<User> EnsureUserAsync(
@@ -170,7 +165,7 @@ public static class DevelopmentSeedExtensions
if (!createResult.Succeeded) if (!createResult.Succeeded)
{ {
throw new InvalidOperationException( throw new InvalidOperationException(
$"Failed to seed development user '{username}': {string.Join(", ", createResult.Errors.Select(error => error.Description))}"); $"Failed to seed test user '{username}': {string.Join(", ", createResult.Errors.Select(error => error.Description))}");
} }
} }
@@ -190,7 +185,7 @@ public static class DevelopmentSeedExtensions
if (!passwordResetResult.Succeeded) if (!passwordResetResult.Succeeded)
{ {
throw new InvalidOperationException( throw new InvalidOperationException(
$"Failed to set development password for '{username}': {string.Join(", ", passwordResetResult.Errors.Select(error => error.Description))}"); $"Failed to set test password for '{username}': {string.Join(", ", passwordResetResult.Errors.Select(error => error.Description))}");
} }
} }
@@ -217,13 +212,7 @@ public static class DevelopmentSeedExtensions
await userManager.RemoveClaimAsync(user, claim); await userManager.RemoveClaimAsync(user, claim);
} }
string persona = roles.Contains(KnownRoles.Manager, StringComparer.Ordinal) string persona = GetPersona(roles);
? KnownRoles.Manager
: roles.Contains(KnownRoles.Client, StringComparer.Ordinal)
? KnownRoles.Client
: roles.Contains(KnownRoles.Provider, StringComparer.Ordinal)
? KnownRoles.Provider
: KnownRoles.WorkspaceMember;
foreach (Claim claim in claims.Concat([new Claim(KnownClaims.Persona, persona)])) foreach (Claim claim in claims.Concat([new Claim(KnownClaims.Persona, persona)]))
{ {
@@ -233,6 +222,26 @@ public static class DevelopmentSeedExtensions
return user; return user;
} }
private static string GetPersona(IReadOnlyCollection<string> roles)
{
if (roles.Contains(KnownRoles.Manager, StringComparer.Ordinal))
{
return KnownRoles.Manager;
}
if (roles.Contains(KnownRoles.Client, StringComparer.Ordinal))
{
return KnownRoles.Client;
}
if (roles.Contains(KnownRoles.Provider, StringComparer.Ordinal))
{
return KnownRoles.Provider;
}
return KnownRoles.WorkspaceMember;
}
private static async Task EnsureOrganizationDataAsync( private static async Task EnsureOrganizationDataAsync(
Guid managerUserId, Guid managerUserId,
Guid developerUserId, Guid developerUserId,
@@ -252,7 +261,12 @@ public static class DevelopmentSeedExtensions
dbContext.Organizations.Add(organization); dbContext.Organizations.Add(organization);
} }
organization.Name = "Northstar Collective"; organization.Name = "Northstar Agency";
organization.IsGoogleDriveDamEnabled = true;
organization.GoogleDriveRootFolderId = "dev-socialize-dam-root";
organization.GoogleDriveRootFolderName = "Socialize DAM";
organization.GoogleDriveRootFolderUrl = "https://drive.google.com/drive/folders/dev-socialize-dam-root";
organization.MembershipTierId = OrganizationMembershipTierSeed.AgencyId;
organization.OwnerUserId = managerUserId; organization.OwnerUserId = managerUserId;
await UpsertOrganizationMembershipAsync( await UpsertOrganizationMembershipAsync(
@@ -309,32 +323,31 @@ public static class DevelopmentSeedExtensions
AppDbContext dbContext, AppDbContext dbContext,
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
Workspace? workspace = await dbContext.Workspaces await UpsertWorkspaceAsync(
.SingleOrDefaultAsync(candidate => candidate.Id == WorkspaceId, cancellationToken); dbContext,
if (workspace is null) WorkspaceId,
{ OrganizationId,
workspace = new Workspace managerUserId,
{ "Luma Coffee",
Id = WorkspaceId, "America/Montreal",
Name = string.Empty, "/images/seed/luma-coffee-logo.svg",
TimeZone = string.Empty, cancellationToken);
CreatedAt = DateTimeOffset.UtcNow, await UpsertWorkspaceAsync(
}; dbContext,
dbContext.Workspaces.Add(workspace); AtlasWorkspaceId,
} OrganizationId,
managerUserId,
workspace.Name = "Northstar Studio"; "Atlas Bakery",
workspace.OrganizationId = OrganizationId; "America/Montreal",
workspace.OwnerUserId = managerUserId; "/images/seed/atlas-bakery-logo.svg",
workspace.TimeZone = "America/Montreal"; cancellationToken);
await dbContext.SaveChangesAsync(cancellationToken);
await UpsertClientAsync( await UpsertClientAsync(
dbContext, dbContext,
ScopedClientId, ScopedClientId,
"Luma Coffee", "Luma Coffee",
"Active", "Active",
"https://images.unsplash.com/photo-1511920170033-f8396924c348?auto=format&fit=crop&w=200&q=80", "/images/seed/luma-coffee-logo.svg",
"Sofia Martin", "Sofia Martin",
"client@socialize.local", "client@socialize.local",
WorkspaceId, WorkspaceId,
@@ -344,10 +357,10 @@ public static class DevelopmentSeedExtensions
HiddenClientId, HiddenClientId,
"Atlas Bakery", "Atlas Bakery",
"Active", "Active",
"https://images.unsplash.com/photo-1509440159596-0249088772ff?auto=format&fit=crop&w=200&q=80", "/images/seed/atlas-bakery-logo.svg",
"Nina Cole", "Nina Cole",
"nina@atlasbakery.test", "nina@atlasbakery.test",
WorkspaceId, AtlasWorkspaceId,
cancellationToken); cancellationToken);
await UpsertCampaignAsync( await UpsertCampaignAsync(
@@ -365,7 +378,7 @@ public static class DevelopmentSeedExtensions
await UpsertCampaignAsync( await UpsertCampaignAsync(
dbContext, dbContext,
HiddenCampaignId, HiddenCampaignId,
WorkspaceId, AtlasWorkspaceId,
HiddenClientId, HiddenClientId,
"Summer Retention", "Summer Retention",
"Planned", "Planned",
@@ -375,6 +388,34 @@ public static class DevelopmentSeedExtensions
"Sequence email and paid social updates together.", "Sequence email and paid social updates together.",
cancellationToken); cancellationToken);
await UpsertChannelAsync(
dbContext,
LumaInstagramChannelId,
WorkspaceId,
"Luma Coffee Instagram",
"Instagram",
"@lumacoffee",
null,
cancellationToken);
await UpsertChannelAsync(
dbContext,
LumaTikTokChannelId,
WorkspaceId,
"Luma Coffee TikTok",
"TikTok",
"@lumacoffee",
null,
cancellationToken);
await UpsertChannelAsync(
dbContext,
AtlasInstagramChannelId,
AtlasWorkspaceId,
"Atlas Bakery Instagram",
"Instagram",
"@atlasbakery",
null,
cancellationToken);
await UpsertContentItemAsync( await UpsertContentItemAsync(
dbContext, dbContext,
ScopedContentItemId, ScopedContentItemId,
@@ -383,7 +424,7 @@ public static class DevelopmentSeedExtensions
ScopedCampaignId, ScopedCampaignId,
"Spring launch hero video", "Spring launch hero video",
"Fresh seasonal menu launch across Instagram and TikTok.", "Fresh seasonal menu launch across Instagram and TikTok.",
"Instagram Reel, TikTok", "Luma Coffee Instagram, Luma Coffee TikTok",
"In approval", "In approval",
DateTimeOffset.UtcNow.AddDays(3), DateTimeOffset.UtcNow.AddDays(3),
"v3", "v3",
@@ -392,22 +433,22 @@ public static class DevelopmentSeedExtensions
await UpsertContentItemAsync( await UpsertContentItemAsync(
dbContext, dbContext,
HiddenContentItemId, HiddenContentItemId,
WorkspaceId, AtlasWorkspaceId,
HiddenClientId, HiddenClientId,
HiddenCampaignId, HiddenCampaignId,
"Bakery loyalty carousel", "Bakery loyalty carousel",
"Reward regular customers with a four-card retention carousel.", "Reward regular customers with a four-card retention carousel.",
"Instagram Carousel", "Atlas Bakery Instagram",
"Draft", "Draft",
DateTimeOffset.UtcNow.AddDays(10), DateTimeOffset.UtcNow.AddDays(10),
"v1", "v1",
1, 1,
cancellationToken); cancellationToken);
await EnsureRevisionAsync(dbContext, Guid.Parse("44444444-4444-4444-4444-000000000001"), ScopedContentItemId, 1, "v1", "Spring launch hero video", "Initial draft for the seasonal menu launch.", "Instagram Reel, TikTok", "Initial concept draft.", providerUserId, DateTimeOffset.UtcNow.AddDays(-5), cancellationToken); await EnsureRevisionAsync(dbContext, Guid.Parse("44444444-4444-4444-4444-000000000001"), ScopedContentItemId, 1, "v1", "Spring launch hero video", "Initial draft for the seasonal menu launch.", "Luma Coffee Instagram, Luma Coffee TikTok", "Initial concept draft.", providerUserId, DateTimeOffset.UtcNow.AddDays(-5), cancellationToken);
await EnsureRevisionAsync(dbContext, Guid.Parse("44444444-4444-4444-4444-000000000002"), ScopedContentItemId, 2, "v2", "Spring launch hero video", "Updated hook and transitions after internal review.", "Instagram Reel, TikTok", "Addressed internal pacing feedback.", providerUserId, DateTimeOffset.UtcNow.AddDays(-3), cancellationToken); await EnsureRevisionAsync(dbContext, Guid.Parse("44444444-4444-4444-4444-000000000002"), ScopedContentItemId, 2, "v2", "Spring launch hero video", "Updated hook and transitions after internal review.", "Luma Coffee Instagram, Luma Coffee TikTok", "Addressed internal pacing feedback.", providerUserId, DateTimeOffset.UtcNow.AddDays(-3), cancellationToken);
await EnsureRevisionAsync(dbContext, Guid.Parse("44444444-4444-4444-4444-000000000003"), ScopedContentItemId, 3, "v3", "Spring launch hero video", "Fresh seasonal menu launch across Instagram and TikTok.", "Instagram Reel, TikTok", "Client-facing draft after copy cleanup.", providerUserId, DateTimeOffset.UtcNow.AddDays(-1), cancellationToken); await EnsureRevisionAsync(dbContext, Guid.Parse("44444444-4444-4444-4444-000000000003"), ScopedContentItemId, 3, "v3", "Spring launch hero video", "Fresh seasonal menu launch across Instagram and TikTok.", "Luma Coffee Instagram, Luma Coffee TikTok", "Client-facing draft after copy cleanup.", providerUserId, DateTimeOffset.UtcNow.AddDays(-1), cancellationToken);
await EnsureRevisionAsync(dbContext, Guid.Parse("44444444-4444-4444-4444-000000000004"), HiddenContentItemId, 1, "v1", "Bakery loyalty carousel", "Reward regular customers with a four-card retention carousel.", "Instagram Carousel", "First draft.", managerUserId, DateTimeOffset.UtcNow.AddDays(-2), cancellationToken); await EnsureRevisionAsync(dbContext, Guid.Parse("44444444-4444-4444-4444-000000000004"), HiddenContentItemId, 1, "v1", "Bakery loyalty carousel", "Reward regular customers with a four-card retention carousel.", "Atlas Bakery Instagram", "First draft.", managerUserId, DateTimeOffset.UtcNow.AddDays(-2), cancellationToken);
Asset? asset = await dbContext.Assets.SingleOrDefaultAsync(candidate => candidate.Id == ScopedAssetId, cancellationToken); Asset? asset = await dbContext.Assets.SingleOrDefaultAsync(candidate => candidate.Id == ScopedAssetId, cancellationToken);
if (asset is null) if (asset is null)
@@ -429,6 +470,7 @@ public static class DevelopmentSeedExtensions
asset.DisplayName = "Spring launch cut"; asset.DisplayName = "Spring launch cut";
asset.GoogleDriveFileId = "dev-socialize-demo"; asset.GoogleDriveFileId = "dev-socialize-demo";
asset.GoogleDriveLink = "https://drive.google.com/file/d/dev-socialize-demo/view"; asset.GoogleDriveLink = "https://drive.google.com/file/d/dev-socialize-demo/view";
asset.GoogleDriveWorkspaceFolderPath = "Socialize DAM/luma-coffee";
asset.PreviewUrl = "https://drive.google.com/thumbnail?id=dev-socialize-demo"; asset.PreviewUrl = "https://drive.google.com/thumbnail?id=dev-socialize-demo";
asset.CurrentRevisionNumber = 2; asset.CurrentRevisionNumber = 2;
await dbContext.SaveChangesAsync(cancellationToken); await dbContext.SaveChangesAsync(cancellationToken);
@@ -455,8 +497,6 @@ public static class DevelopmentSeedExtensions
comment.AuthorDisplayName = "Sofia Martin"; comment.AuthorDisplayName = "Sofia Martin";
comment.AuthorEmail = "client@socialize.local"; comment.AuthorEmail = "client@socialize.local";
comment.Body = "Please tighten the opening three seconds and make the launch CTA more explicit."; comment.Body = "Please tighten the opening three seconds and make the launch CTA more explicit.";
comment.IsResolved = false;
comment.ResolvedAt = null;
await dbContext.SaveChangesAsync(cancellationToken); await dbContext.SaveChangesAsync(cancellationToken);
ApprovalRequest? approvalRequest = await dbContext.ApprovalRequests.SingleOrDefaultAsync(candidate => candidate.Id == ScopedApprovalRequestId, cancellationToken); ApprovalRequest? approvalRequest = await dbContext.ApprovalRequests.SingleOrDefaultAsync(candidate => candidate.Id == ScopedApprovalRequestId, cancellationToken);
@@ -535,6 +575,45 @@ public static class DevelopmentSeedExtensions
await dbContext.SaveChangesAsync(cancellationToken); await dbContext.SaveChangesAsync(cancellationToken);
} }
private static async Task UpsertWorkspaceAsync(
AppDbContext dbContext,
Guid id,
Guid organizationId,
Guid ownerUserId,
string name,
string timeZone,
string logoUrl,
CancellationToken cancellationToken)
{
Workspace? workspace = await dbContext.Workspaces
.SingleOrDefaultAsync(candidate => candidate.Id == id, cancellationToken);
if (workspace is null)
{
workspace = new Workspace
{
Id = id,
Name = string.Empty,
Slug = string.Empty,
TimeZone = string.Empty,
CreatedAt = DateTimeOffset.UtcNow,
};
dbContext.Workspaces.Add(workspace);
}
workspace.Name = name;
workspace.Slug = await WorkspaceSlugGenerator.CreateUniqueAsync(
dbContext,
organizationId,
string.IsNullOrWhiteSpace(workspace.Slug) ? name : workspace.Slug,
workspace.Id,
cancellationToken);
workspace.OrganizationId = organizationId;
workspace.OwnerUserId = ownerUserId;
workspace.TimeZone = timeZone;
workspace.LogoUrl = logoUrl;
await dbContext.SaveChangesAsync(cancellationToken);
}
private static async Task UpsertClientAsync( private static async Task UpsertClientAsync(
AppDbContext dbContext, AppDbContext dbContext,
Guid id, Guid id,
@@ -604,6 +683,37 @@ public static class DevelopmentSeedExtensions
await dbContext.SaveChangesAsync(cancellationToken); await dbContext.SaveChangesAsync(cancellationToken);
} }
private static async Task UpsertChannelAsync(
AppDbContext dbContext,
Guid id,
Guid workspaceId,
string name,
string network,
string? handle,
string? externalUrl,
CancellationToken cancellationToken)
{
Channel? channel = await dbContext.Channels.SingleOrDefaultAsync(candidate => candidate.Id == id, cancellationToken);
if (channel is null)
{
channel = new Channel
{
Id = id,
Name = string.Empty,
Network = string.Empty,
CreatedAt = DateTimeOffset.UtcNow,
};
dbContext.Channels.Add(channel);
}
channel.WorkspaceId = workspaceId;
channel.Name = name;
channel.Network = network;
channel.Handle = handle;
channel.ExternalUrl = externalUrl;
await dbContext.SaveChangesAsync(cancellationToken);
}
private static async Task UpsertContentItemAsync( private static async Task UpsertContentItemAsync(
AppDbContext dbContext, AppDbContext dbContext,
Guid id, Guid id,

View File

@@ -2,7 +2,7 @@ using System.Text.RegularExpressions;
namespace Socialize.Api.Infrastructure.YouTube; namespace Socialize.Api.Infrastructure.YouTube;
public static class YouTubeUrlHelper internal static class YouTubeUrlHelper
{ {
private static readonly Regex VideoIdRegex = new( private static readonly Regex VideoIdRegex = new(
@"(?:youtube\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^""&?/\s]{11})", @"(?:youtube\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^""&?/\s]{11})",

View File

@@ -12,7 +12,7 @@ using Socialize.Api.Data;
namespace Socialize.Api.Migrations namespace Socialize.Api.Migrations
{ {
[DbContext(typeof(AppDbContext))] [DbContext(typeof(AppDbContext))]
[Migration("20260505013232_Initial")] [Migration("20260507143849_Initial")]
partial class Initial partial class Initial
{ {
/// <inheritdoc /> /// <inheritdoc />
@@ -441,6 +441,326 @@ namespace Socialize.Api.Migrations
b.ToTable("AssetRevisions", (string)null); b.ToTable("AssetRevisions", (string)null);
}); });
modelBuilder.Entity("Socialize.Api.Modules.CalendarIntegrations.Data.CalendarCatalogEntry", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<string>("Category")
.IsRequired()
.HasMaxLength(64)
.HasColumnType("character varying(64)");
b.Property<string>("Country")
.HasMaxLength(2)
.HasColumnType("character varying(2)");
b.Property<DateTimeOffset>("CreatedAt")
.ValueGeneratedOnAdd()
.HasColumnType("timestamp with time zone")
.HasDefaultValueSql("CURRENT_TIMESTAMP");
b.Property<string>("CultureOrReligion")
.HasMaxLength(128)
.HasColumnType("character varying(128)");
b.Property<string>("DefaultColor")
.IsRequired()
.HasMaxLength(16)
.HasColumnType("character varying(16)");
b.Property<string>("Description")
.IsRequired()
.HasMaxLength(1024)
.HasColumnType("character varying(1024)");
b.Property<string>("Language")
.IsRequired()
.HasMaxLength(16)
.HasColumnType("character varying(16)");
b.Property<string>("ProviderName")
.IsRequired()
.HasMaxLength(128)
.HasColumnType("character varying(128)");
b.Property<string>("Region")
.HasMaxLength(128)
.HasColumnType("character varying(128)");
b.Property<string>("SourceUrl")
.IsRequired()
.HasMaxLength(2048)
.HasColumnType("character varying(2048)");
b.Property<string>("Title")
.IsRequired()
.HasMaxLength(256)
.HasColumnType("character varying(256)");
b.Property<string>("TrustLevel")
.IsRequired()
.HasMaxLength(64)
.HasColumnType("character varying(64)");
b.HasKey("Id");
b.HasIndex("Category");
b.HasIndex("Country");
b.HasIndex("ProviderName");
b.ToTable("CalendarCatalogEntries", (string)null);
b.HasData(
new
{
Id = new Guid("10000000-0000-0000-0000-000000000001"),
Category = "public-holiday",
Country = "US",
CreatedAt = new DateTimeOffset(new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), new TimeSpan(0, 0, 0, 0, 0)),
DefaultColor = "#2F80ED",
Description = "Federal public holiday calendar for the United States.",
Language = "en",
ProviderName = "Nager.Date",
SourceUrl = "https://date.nager.at/api/v3/PublicHolidays/2026/US",
Title = "United States Public Holidays",
TrustLevel = "Verified"
},
new
{
Id = new Guid("10000000-0000-0000-0000-000000000002"),
Category = "public-holiday",
Country = "CA",
CreatedAt = new DateTimeOffset(new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), new TimeSpan(0, 0, 0, 0, 0)),
DefaultColor = "#2F80ED",
Description = "Public holiday calendar for Canada.",
Language = "en",
ProviderName = "Nager.Date",
SourceUrl = "https://date.nager.at/api/v3/PublicHolidays/2026/CA",
Title = "Canada Public Holidays",
TrustLevel = "Verified"
},
new
{
Id = new Guid("10000000-0000-0000-0000-000000000003"),
Category = "marketing-moment",
CreatedAt = new DateTimeOffset(new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), new TimeSpan(0, 0, 0, 0, 0)),
DefaultColor = "#9B51E0",
Description = "Common retail, awareness, and social planning moments.",
Language = "en",
ProviderName = "Socialize",
SourceUrl = "https://example.com/socialize/marketing-moments.ics",
Title = "Common Marketing Moments",
TrustLevel = "Maintained"
});
});
modelBuilder.Entity("Socialize.Api.Modules.CalendarIntegrations.Data.CalendarEvent", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<Guid>("CalendarSourceId")
.HasColumnType("uuid");
b.Property<string>("Description")
.HasMaxLength(4000)
.HasColumnType("character varying(4000)");
b.Property<DateOnly>("EndDate")
.HasColumnType("date");
b.Property<DateTime?>("EndLocalDateTime")
.HasColumnType("timestamp with time zone");
b.Property<DateTimeOffset?>("EndUtc")
.HasColumnType("timestamp with time zone");
b.Property<DateTimeOffset>("ImportedAt")
.HasColumnType("timestamp with time zone");
b.Property<bool>("IsAllDay")
.HasColumnType("boolean");
b.Property<bool>("IsFloatingTime")
.HasColumnType("boolean");
b.Property<string>("Location")
.HasMaxLength(512)
.HasColumnType("character varying(512)");
b.Property<string>("RecurrenceId")
.HasMaxLength(512)
.HasColumnType("character varying(512)");
b.Property<string>("SourceEventUid")
.IsRequired()
.HasMaxLength(512)
.HasColumnType("character varying(512)");
b.Property<DateTimeOffset?>("SourceLastModifiedAt")
.HasColumnType("timestamp with time zone");
b.Property<string>("SourceUrl")
.HasMaxLength(2048)
.HasColumnType("character varying(2048)");
b.Property<DateOnly>("StartDate")
.HasColumnType("date");
b.Property<DateTime?>("StartLocalDateTime")
.HasColumnType("timestamp with time zone");
b.Property<DateTimeOffset?>("StartUtc")
.HasColumnType("timestamp with time zone");
b.Property<string>("TimeZoneId")
.HasMaxLength(128)
.HasColumnType("character varying(128)");
b.Property<string>("Title")
.IsRequired()
.HasMaxLength(512)
.HasColumnType("character varying(512)");
b.HasKey("Id");
b.HasIndex("CalendarSourceId");
b.HasIndex("CalendarSourceId", "SourceEventUid", "StartDate")
.IsUnique();
b.ToTable("CalendarEvents", (string)null);
});
modelBuilder.Entity("Socialize.Api.Modules.CalendarIntegrations.Data.CalendarSource", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<string>("CatalogSourceReference")
.HasMaxLength(256)
.HasColumnType("character varying(256)");
b.Property<string>("Category")
.IsRequired()
.HasMaxLength(64)
.HasColumnType("character varying(64)");
b.Property<string>("Color")
.IsRequired()
.HasMaxLength(16)
.HasColumnType("character varying(16)");
b.Property<DateTimeOffset>("CreatedAt")
.ValueGeneratedOnAdd()
.HasColumnType("timestamp with time zone")
.HasDefaultValueSql("CURRENT_TIMESTAMP");
b.Property<string>("DisplayTitle")
.IsRequired()
.HasMaxLength(256)
.HasColumnType("character varying(256)");
b.Property<string>("InheritanceMode")
.HasMaxLength(32)
.HasColumnType("character varying(32)");
b.Property<bool>("IsEnabled")
.HasColumnType("boolean");
b.Property<DateTimeOffset?>("LastAttemptedSyncAt")
.HasColumnType("timestamp with time zone");
b.Property<DateTimeOffset?>("LastSuccessfulSyncAt")
.HasColumnType("timestamp with time zone");
b.Property<string>("LastSyncError")
.HasMaxLength(2048)
.HasColumnType("character varying(2048)");
b.Property<Guid?>("OrganizationId")
.HasColumnType("uuid");
b.Property<string>("Scope")
.IsRequired()
.HasMaxLength(32)
.HasColumnType("character varying(32)");
b.Property<string>("SourceUrl")
.HasMaxLength(2048)
.HasColumnType("character varying(2048)");
b.Property<DateTimeOffset>("UpdatedAt")
.ValueGeneratedOnAdd()
.HasColumnType("timestamp with time zone")
.HasDefaultValueSql("CURRENT_TIMESTAMP");
b.Property<Guid?>("UserId")
.HasColumnType("uuid");
b.Property<Guid?>("WorkspaceId")
.HasColumnType("uuid");
b.HasKey("Id");
b.HasIndex("OrganizationId");
b.HasIndex("Scope");
b.HasIndex("UserId");
b.HasIndex("WorkspaceId");
b.ToTable("CalendarSources", (string)null);
});
modelBuilder.Entity("Socialize.Api.Modules.CalendarIntegrations.Data.UserCalendarExportFeed", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<DateTimeOffset>("CreatedAt")
.ValueGeneratedOnAdd()
.HasColumnType("timestamp with time zone")
.HasDefaultValueSql("CURRENT_TIMESTAMP");
b.Property<DateTimeOffset?>("RevokedAt")
.HasColumnType("timestamp with time zone");
b.Property<string>("Token")
.HasMaxLength(96)
.HasColumnType("character varying(96)");
b.Property<string>("TokenHash")
.HasMaxLength(64)
.HasColumnType("character varying(64)");
b.Property<DateTimeOffset>("UpdatedAt")
.ValueGeneratedOnAdd()
.HasColumnType("timestamp with time zone")
.HasDefaultValueSql("CURRENT_TIMESTAMP");
b.Property<Guid>("UserId")
.HasColumnType("uuid");
b.HasKey("Id");
b.HasIndex("TokenHash")
.IsUnique();
b.HasIndex("UserId")
.IsUnique();
b.ToTable("UserCalendarExportFeeds", (string)null);
});
modelBuilder.Entity("Socialize.Api.Modules.Campaigns.Data.Campaign", b => modelBuilder.Entity("Socialize.Api.Modules.Campaigns.Data.Campaign", b =>
{ {
b.Property<Guid>("Id") b.Property<Guid>("Id")
@@ -494,6 +814,48 @@ namespace Socialize.Api.Migrations
b.ToTable("Campaigns", (string)null); b.ToTable("Campaigns", (string)null);
}); });
modelBuilder.Entity("Socialize.Api.Modules.Channels.Data.Channel", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<DateTimeOffset>("CreatedAt")
.ValueGeneratedOnAdd()
.HasColumnType("timestamp with time zone")
.HasDefaultValueSql("CURRENT_TIMESTAMP");
b.Property<string>("ExternalUrl")
.HasMaxLength(2048)
.HasColumnType("character varying(2048)");
b.Property<string>("Handle")
.HasMaxLength(256)
.HasColumnType("character varying(256)");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(256)
.HasColumnType("character varying(256)");
b.Property<string>("Network")
.IsRequired()
.HasMaxLength(64)
.HasColumnType("character varying(64)");
b.Property<Guid>("WorkspaceId")
.HasColumnType("uuid");
b.HasKey("Id");
b.HasIndex("WorkspaceId");
b.HasIndex("WorkspaceId", "Network", "Name")
.IsUnique();
b.ToTable("Channels", (string)null);
});
modelBuilder.Entity("Socialize.Api.Modules.Clients.Data.Client", b => modelBuilder.Entity("Socialize.Api.Modules.Clients.Data.Client", b =>
{ {
b.Property<Guid>("Id") b.Property<Guid>("Id")
@@ -550,6 +912,29 @@ namespace Socialize.Api.Migrations
.ValueGeneratedOnAdd() .ValueGeneratedOnAdd()
.HasColumnType("uuid"); .HasColumnType("uuid");
b.Property<string>("AttachmentBlobContainerName")
.HasMaxLength(128)
.HasColumnType("character varying(128)");
b.Property<string>("AttachmentBlobName")
.HasMaxLength(512)
.HasColumnType("character varying(512)");
b.Property<string>("AttachmentBlobUrl")
.HasMaxLength(1024)
.HasColumnType("character varying(1024)");
b.Property<string>("AttachmentContentType")
.HasMaxLength(128)
.HasColumnType("character varying(128)");
b.Property<string>("AttachmentFileName")
.HasMaxLength(256)
.HasColumnType("character varying(256)");
b.Property<long?>("AttachmentSizeBytes")
.HasColumnType("bigint");
b.Property<string>("AuthorDisplayName") b.Property<string>("AuthorDisplayName")
.IsRequired() .IsRequired()
.HasMaxLength(256) .HasMaxLength(256)
@@ -576,15 +961,9 @@ namespace Socialize.Api.Migrations
.HasColumnType("timestamp with time zone") .HasColumnType("timestamp with time zone")
.HasDefaultValueSql("CURRENT_TIMESTAMP"); .HasDefaultValueSql("CURRENT_TIMESTAMP");
b.Property<bool>("IsResolved")
.HasColumnType("boolean");
b.Property<Guid?>("ParentCommentId") b.Property<Guid?>("ParentCommentId")
.HasColumnType("uuid"); .HasColumnType("uuid");
b.Property<DateTimeOffset?>("ResolvedAt")
.HasColumnType("timestamp with time zone");
b.Property<Guid>("WorkspaceId") b.Property<Guid>("WorkspaceId")
.HasColumnType("uuid"); .HasColumnType("uuid");
@@ -665,6 +1044,62 @@ namespace Socialize.Api.Migrations
b.ToTable("ContentItems", (string)null); b.ToTable("ContentItems", (string)null);
}); });
modelBuilder.Entity("Socialize.Api.Modules.ContentItems.Data.ContentItemActivityEntry", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<string>("ActorEmail")
.HasMaxLength(256)
.HasColumnType("character varying(256)");
b.Property<Guid?>("ActorUserId")
.HasColumnType("uuid");
b.Property<Guid>("ContentItemId")
.HasColumnType("uuid");
b.Property<DateTimeOffset>("CreatedAt")
.ValueGeneratedOnAdd()
.HasColumnType("timestamp with time zone")
.HasDefaultValueSql("CURRENT_TIMESTAMP");
b.Property<Guid>("EntityId")
.HasColumnType("uuid");
b.Property<string>("EntityType")
.IsRequired()
.HasMaxLength(128)
.HasColumnType("character varying(128)");
b.Property<string>("EventType")
.IsRequired()
.HasMaxLength(128)
.HasColumnType("character varying(128)");
b.Property<string>("MetadataJson")
.HasColumnType("jsonb");
b.Property<string>("Summary")
.IsRequired()
.HasMaxLength(1024)
.HasColumnType("character varying(1024)");
b.Property<Guid>("WorkspaceId")
.HasColumnType("uuid");
b.HasKey("Id");
b.HasIndex("ContentItemId");
b.HasIndex("WorkspaceId");
b.HasIndex("ContentItemId", "CreatedAt");
b.ToTable("ContentItemActivityEntries", (string)null);
});
modelBuilder.Entity("Socialize.Api.Modules.ContentItems.Data.ContentItemRevision", b => modelBuilder.Entity("Socialize.Api.Modules.ContentItems.Data.ContentItemRevision", b =>
{ {
b.Property<Guid>("Id") b.Property<Guid>("Id")
@@ -1217,6 +1652,10 @@ namespace Socialize.Api.Migrations
.HasColumnType("timestamp with time zone") .HasColumnType("timestamp with time zone")
.HasDefaultValueSql("CURRENT_TIMESTAMP"); .HasDefaultValueSql("CURRENT_TIMESTAMP");
b.Property<string>("LogoUrl")
.HasMaxLength(2048)
.HasColumnType("character varying(2048)");
b.Property<string>("Name") b.Property<string>("Name")
.IsRequired() .IsRequired()
.HasMaxLength(256) .HasMaxLength(256)
@@ -1420,6 +1859,15 @@ namespace Socialize.Api.Migrations
.IsRequired(); .IsRequired();
}); });
modelBuilder.Entity("Socialize.Api.Modules.CalendarIntegrations.Data.CalendarEvent", b =>
{
b.HasOne("Socialize.Api.Modules.CalendarIntegrations.Data.CalendarSource", null)
.WithMany()
.HasForeignKey("CalendarSourceId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Socialize.Api.Modules.Feedback.Data.FeedbackActivityEntry", b => modelBuilder.Entity("Socialize.Api.Modules.Feedback.Data.FeedbackActivityEntry", b =>
{ {
b.HasOne("Socialize.Api.Modules.Feedback.Data.FeedbackReport", "FeedbackReport") b.HasOne("Socialize.Api.Modules.Feedback.Data.FeedbackReport", "FeedbackReport")

View File

@@ -4,10 +4,13 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable #nullable disable
#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional
#pragma warning disable CA1861 // Generated migration seed arrays are not runtime hot paths.
namespace Socialize.Api.Migrations namespace Socialize.Api.Migrations
{ {
/// <inheritdoc /> /// <inheritdoc />
public partial class Initial : Migration internal partial class Initial : Migration
{ {
/// <inheritdoc /> /// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder) protected override void Up(MigrationBuilder migrationBuilder)
@@ -162,6 +165,56 @@ namespace Socialize.Api.Migrations
table.PrimaryKey("PK_Assets", x => x.Id); table.PrimaryKey("PK_Assets", x => x.Id);
}); });
migrationBuilder.CreateTable(
name: "CalendarCatalogEntries",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
Title = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: false),
Description = table.Column<string>(type: "character varying(1024)", maxLength: 1024, nullable: false),
Country = table.Column<string>(type: "character varying(2)", maxLength: 2, nullable: true),
Region = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: true),
Language = table.Column<string>(type: "character varying(16)", maxLength: 16, nullable: false),
Category = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
CultureOrReligion = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: true),
ProviderName = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: false),
SourceUrl = table.Column<string>(type: "character varying(2048)", maxLength: 2048, nullable: false),
TrustLevel = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
DefaultColor = table.Column<string>(type: "character varying(16)", maxLength: 16, nullable: false),
CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP")
},
constraints: table =>
{
table.PrimaryKey("PK_CalendarCatalogEntries", x => x.Id);
});
migrationBuilder.CreateTable(
name: "CalendarSources",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
Scope = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
OrganizationId = table.Column<Guid>(type: "uuid", nullable: true),
WorkspaceId = table.Column<Guid>(type: "uuid", nullable: true),
UserId = table.Column<Guid>(type: "uuid", nullable: true),
SourceUrl = table.Column<string>(type: "character varying(2048)", maxLength: 2048, nullable: true),
CatalogSourceReference = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
DisplayTitle = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: false),
Color = table.Column<string>(type: "character varying(16)", maxLength: 16, nullable: false),
Category = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
IsEnabled = table.Column<bool>(type: "boolean", nullable: false),
InheritanceMode = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: true),
LastSuccessfulSyncAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
LastAttemptedSyncAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
LastSyncError = table.Column<string>(type: "character varying(2048)", maxLength: 2048, nullable: true),
CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP"),
UpdatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP")
},
constraints: table =>
{
table.PrimaryKey("PK_CalendarSources", x => x.Id);
});
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "Campaigns", name: "Campaigns",
columns: table => new columns: table => new
@@ -182,6 +235,23 @@ namespace Socialize.Api.Migrations
table.PrimaryKey("PK_Campaigns", x => x.Id); table.PrimaryKey("PK_Campaigns", x => x.Id);
}); });
migrationBuilder.CreateTable(
name: "Channels",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
WorkspaceId = table.Column<Guid>(type: "uuid", nullable: false),
Name = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: false),
Network = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
Handle = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
ExternalUrl = table.Column<string>(type: "character varying(2048)", maxLength: 2048, nullable: true),
CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP")
},
constraints: table =>
{
table.PrimaryKey("PK_Channels", x => x.Id);
});
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "Clients", name: "Clients",
columns: table => new columns: table => new
@@ -213,15 +283,40 @@ namespace Socialize.Api.Migrations
AuthorDisplayName = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: false), AuthorDisplayName = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: false),
AuthorEmail = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: false), AuthorEmail = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: false),
Body = table.Column<string>(type: "character varying(4000)", maxLength: 4000, nullable: false), Body = table.Column<string>(type: "character varying(4000)", maxLength: 4000, nullable: false),
IsResolved = table.Column<bool>(type: "boolean", nullable: false), AttachmentFileName = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP"), AttachmentContentType = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: true),
ResolvedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true) AttachmentSizeBytes = table.Column<long>(type: "bigint", nullable: true),
AttachmentBlobContainerName = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: true),
AttachmentBlobName = table.Column<string>(type: "character varying(512)", maxLength: 512, nullable: true),
AttachmentBlobUrl = table.Column<string>(type: "character varying(1024)", maxLength: 1024, nullable: true),
CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP")
}, },
constraints: table => constraints: table =>
{ {
table.PrimaryKey("PK_Comments", x => x.Id); table.PrimaryKey("PK_Comments", x => x.Id);
}); });
migrationBuilder.CreateTable(
name: "ContentItemActivityEntries",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
WorkspaceId = table.Column<Guid>(type: "uuid", nullable: false),
ContentItemId = table.Column<Guid>(type: "uuid", nullable: false),
EventType = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: false),
EntityType = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: false),
EntityId = table.Column<Guid>(type: "uuid", nullable: false),
Summary = table.Column<string>(type: "character varying(1024)", maxLength: 1024, nullable: false),
ActorUserId = table.Column<Guid>(type: "uuid", nullable: true),
ActorEmail = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
MetadataJson = table.Column<string>(type: "jsonb", nullable: true),
CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP")
},
constraints: table =>
{
table.PrimaryKey("PK_ContentItemActivityEntries", x => x.Id);
});
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "ContentItemRevisions", name: "ContentItemRevisions",
columns: table => new columns: table => new
@@ -329,6 +424,7 @@ namespace Socialize.Api.Migrations
{ {
Id = table.Column<Guid>(type: "uuid", nullable: false), Id = table.Column<Guid>(type: "uuid", nullable: false),
Name = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: false), Name = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: false),
LogoUrl = table.Column<string>(type: "character varying(2048)", maxLength: 2048, nullable: true),
OwnerUserId = table.Column<Guid>(type: "uuid", nullable: false), OwnerUserId = table.Column<Guid>(type: "uuid", nullable: false),
CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP") CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP")
}, },
@@ -337,6 +433,23 @@ namespace Socialize.Api.Migrations
table.PrimaryKey("PK_Organizations", x => x.Id); table.PrimaryKey("PK_Organizations", x => x.Id);
}); });
migrationBuilder.CreateTable(
name: "UserCalendarExportFeeds",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
UserId = table.Column<Guid>(type: "uuid", nullable: false),
Token = table.Column<string>(type: "character varying(96)", maxLength: 96, nullable: true),
TokenHash = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP"),
UpdatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP"),
RevokedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_UserCalendarExportFeeds", x => x.Id);
});
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "WorkspaceApprovalStepConfigurations", name: "WorkspaceApprovalStepConfigurations",
columns: table => new columns: table => new
@@ -478,6 +591,41 @@ namespace Socialize.Api.Migrations
onDelete: ReferentialAction.Cascade); onDelete: ReferentialAction.Cascade);
}); });
migrationBuilder.CreateTable(
name: "CalendarEvents",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
CalendarSourceId = table.Column<Guid>(type: "uuid", nullable: false),
SourceEventUid = table.Column<string>(type: "character varying(512)", maxLength: 512, nullable: false),
Title = table.Column<string>(type: "character varying(512)", maxLength: 512, nullable: false),
Description = table.Column<string>(type: "character varying(4000)", maxLength: 4000, nullable: true),
IsAllDay = table.Column<bool>(type: "boolean", nullable: false),
IsFloatingTime = table.Column<bool>(type: "boolean", nullable: false),
StartDate = table.Column<DateOnly>(type: "date", nullable: false),
EndDate = table.Column<DateOnly>(type: "date", nullable: false),
StartLocalDateTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
EndLocalDateTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
StartUtc = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
EndUtc = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
TimeZoneId = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: true),
RecurrenceId = table.Column<string>(type: "character varying(512)", maxLength: 512, nullable: true),
Location = table.Column<string>(type: "character varying(512)", maxLength: 512, nullable: true),
SourceUrl = table.Column<string>(type: "character varying(2048)", maxLength: 2048, nullable: true),
SourceLastModifiedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
ImportedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_CalendarEvents", x => x.Id);
table.ForeignKey(
name: "FK_CalendarEvents_CalendarSources_CalendarSourceId",
column: x => x.CalendarSourceId,
principalTable: "CalendarSources",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "FeedbackActivityEntries", name: "FeedbackActivityEntries",
columns: table => new columns: table => new
@@ -620,6 +768,16 @@ namespace Socialize.Api.Migrations
onDelete: ReferentialAction.Restrict); onDelete: ReferentialAction.Restrict);
}); });
migrationBuilder.InsertData(
table: "CalendarCatalogEntries",
columns: new[] { "Id", "Category", "Country", "CultureOrReligion", "DefaultColor", "Description", "Language", "ProviderName", "Region", "SourceUrl", "Title", "TrustLevel" },
values: new object[,]
{
{ new Guid("10000000-0000-0000-0000-000000000001"), "public-holiday", "US", null, "#2F80ED", "Federal public holiday calendar for the United States.", "en", "Nager.Date", null, "https://date.nager.at/api/v3/PublicHolidays/2026/US", "United States Public Holidays", "Verified" },
{ new Guid("10000000-0000-0000-0000-000000000002"), "public-holiday", "CA", null, "#2F80ED", "Public holiday calendar for Canada.", "en", "Nager.Date", null, "https://date.nager.at/api/v3/PublicHolidays/2026/CA", "Canada Public Holidays", "Verified" },
{ new Guid("10000000-0000-0000-0000-000000000003"), "marketing-moment", null, null, "#9B51E0", "Common retail, awareness, and social planning moments.", "en", "Socialize", null, "https://example.com/socialize/marketing-moments.ics", "Common Marketing Moments", "Maintained" }
});
migrationBuilder.CreateIndex( migrationBuilder.CreateIndex(
name: "IX_ApprovalDecisions_ApprovalRequestId", name: "IX_ApprovalDecisions_ApprovalRequestId",
table: "ApprovalDecisions", table: "ApprovalDecisions",
@@ -720,6 +878,52 @@ namespace Socialize.Api.Migrations
table: "Assets", table: "Assets",
column: "WorkspaceId"); column: "WorkspaceId");
migrationBuilder.CreateIndex(
name: "IX_CalendarCatalogEntries_Category",
table: "CalendarCatalogEntries",
column: "Category");
migrationBuilder.CreateIndex(
name: "IX_CalendarCatalogEntries_Country",
table: "CalendarCatalogEntries",
column: "Country");
migrationBuilder.CreateIndex(
name: "IX_CalendarCatalogEntries_ProviderName",
table: "CalendarCatalogEntries",
column: "ProviderName");
migrationBuilder.CreateIndex(
name: "IX_CalendarEvents_CalendarSourceId",
table: "CalendarEvents",
column: "CalendarSourceId");
migrationBuilder.CreateIndex(
name: "IX_CalendarEvents_CalendarSourceId_SourceEventUid_StartDate",
table: "CalendarEvents",
columns: new[] { "CalendarSourceId", "SourceEventUid", "StartDate" },
unique: true);
migrationBuilder.CreateIndex(
name: "IX_CalendarSources_OrganizationId",
table: "CalendarSources",
column: "OrganizationId");
migrationBuilder.CreateIndex(
name: "IX_CalendarSources_Scope",
table: "CalendarSources",
column: "Scope");
migrationBuilder.CreateIndex(
name: "IX_CalendarSources_UserId",
table: "CalendarSources",
column: "UserId");
migrationBuilder.CreateIndex(
name: "IX_CalendarSources_WorkspaceId",
table: "CalendarSources",
column: "WorkspaceId");
migrationBuilder.CreateIndex( migrationBuilder.CreateIndex(
name: "IX_Campaigns_ClientId", name: "IX_Campaigns_ClientId",
table: "Campaigns", table: "Campaigns",
@@ -736,6 +940,17 @@ namespace Socialize.Api.Migrations
table: "Campaigns", table: "Campaigns",
column: "WorkspaceId"); column: "WorkspaceId");
migrationBuilder.CreateIndex(
name: "IX_Channels_WorkspaceId",
table: "Channels",
column: "WorkspaceId");
migrationBuilder.CreateIndex(
name: "IX_Channels_WorkspaceId_Network_Name",
table: "Channels",
columns: new[] { "WorkspaceId", "Network", "Name" },
unique: true);
migrationBuilder.CreateIndex( migrationBuilder.CreateIndex(
name: "IX_Clients_WorkspaceId", name: "IX_Clients_WorkspaceId",
table: "Clients", table: "Clients",
@@ -762,6 +977,21 @@ namespace Socialize.Api.Migrations
table: "Comments", table: "Comments",
column: "WorkspaceId"); column: "WorkspaceId");
migrationBuilder.CreateIndex(
name: "IX_ContentItemActivityEntries_ContentItemId",
table: "ContentItemActivityEntries",
column: "ContentItemId");
migrationBuilder.CreateIndex(
name: "IX_ContentItemActivityEntries_ContentItemId_CreatedAt",
table: "ContentItemActivityEntries",
columns: new[] { "ContentItemId", "CreatedAt" });
migrationBuilder.CreateIndex(
name: "IX_ContentItemActivityEntries_WorkspaceId",
table: "ContentItemActivityEntries",
column: "WorkspaceId");
migrationBuilder.CreateIndex( migrationBuilder.CreateIndex(
name: "IX_ContentItemRevisions_ContentItemId", name: "IX_ContentItemRevisions_ContentItemId",
table: "ContentItemRevisions", table: "ContentItemRevisions",
@@ -901,6 +1131,18 @@ namespace Socialize.Api.Migrations
table: "Organizations", table: "Organizations",
column: "OwnerUserId"); column: "OwnerUserId");
migrationBuilder.CreateIndex(
name: "IX_UserCalendarExportFeeds_TokenHash",
table: "UserCalendarExportFeeds",
column: "TokenHash",
unique: true);
migrationBuilder.CreateIndex(
name: "IX_UserCalendarExportFeeds_UserId",
table: "UserCalendarExportFeeds",
column: "UserId",
unique: true);
migrationBuilder.CreateIndex( migrationBuilder.CreateIndex(
name: "IX_WorkspaceApprovalStepConfigurations_WorkspaceId", name: "IX_WorkspaceApprovalStepConfigurations_WorkspaceId",
table: "WorkspaceApprovalStepConfigurations", table: "WorkspaceApprovalStepConfigurations",
@@ -966,15 +1208,27 @@ namespace Socialize.Api.Migrations
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "Assets"); name: "Assets");
migrationBuilder.DropTable(
name: "CalendarCatalogEntries");
migrationBuilder.DropTable(
name: "CalendarEvents");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "Campaigns"); name: "Campaigns");
migrationBuilder.DropTable(
name: "Channels");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "Clients"); name: "Clients");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "Comments"); name: "Comments");
migrationBuilder.DropTable(
name: "ContentItemActivityEntries");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "ContentItemRevisions"); name: "ContentItemRevisions");
@@ -999,6 +1253,9 @@ namespace Socialize.Api.Migrations
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "OrganizationMemberships"); name: "OrganizationMemberships");
migrationBuilder.DropTable(
name: "UserCalendarExportFeeds");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "WorkspaceApprovalStepConfigurations"); name: "WorkspaceApprovalStepConfigurations");
@@ -1014,6 +1271,9 @@ namespace Socialize.Api.Migrations
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "AspNetUsers"); name: "AspNetUsers");
migrationBuilder.DropTable(
name: "CalendarSources");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "FeedbackReports"); name: "FeedbackReports");

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,405 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Socialize.Api.Migrations
{
/// <inheritdoc />
internal partial class AddMissingDomainForeignKeys : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateIndex(
name: "IX_FeedbackReports_CampaignId",
table: "FeedbackReports",
column: "CampaignId");
migrationBuilder.CreateIndex(
name: "IX_FeedbackReports_ClientId",
table: "FeedbackReports",
column: "ClientId");
migrationBuilder.CreateIndex(
name: "IX_FeedbackReports_ContentItemId",
table: "FeedbackReports",
column: "ContentItemId");
migrationBuilder.AddForeignKey(
name: "FK_ApprovalDecisions_ApprovalRequests_ApprovalRequestId",
table: "ApprovalDecisions",
column: "ApprovalRequestId",
principalTable: "ApprovalRequests",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "FK_ApprovalRequests_ApprovalWorkflowInstances_WorkflowInstance~",
table: "ApprovalRequests",
column: "WorkflowInstanceId",
principalTable: "ApprovalWorkflowInstances",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "FK_ApprovalRequests_ContentItems_ContentItemId",
table: "ApprovalRequests",
column: "ContentItemId",
principalTable: "ContentItems",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "FK_ApprovalRequests_Workspaces_WorkspaceId",
table: "ApprovalRequests",
column: "WorkspaceId",
principalTable: "Workspaces",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "FK_ApprovalWorkflowInstances_ContentItems_ContentItemId",
table: "ApprovalWorkflowInstances",
column: "ContentItemId",
principalTable: "ContentItems",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "FK_ApprovalWorkflowInstances_Workspaces_WorkspaceId",
table: "ApprovalWorkflowInstances",
column: "WorkspaceId",
principalTable: "Workspaces",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "FK_AssetRevisions_Assets_AssetId",
table: "AssetRevisions",
column: "AssetId",
principalTable: "Assets",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
migrationBuilder.AddForeignKey(
name: "FK_Assets_ContentItems_ContentItemId",
table: "Assets",
column: "ContentItemId",
principalTable: "ContentItems",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "FK_Assets_Workspaces_WorkspaceId",
table: "Assets",
column: "WorkspaceId",
principalTable: "Workspaces",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "FK_Campaigns_Clients_ClientId",
table: "Campaigns",
column: "ClientId",
principalTable: "Clients",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "FK_Campaigns_Workspaces_WorkspaceId",
table: "Campaigns",
column: "WorkspaceId",
principalTable: "Workspaces",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "FK_Channels_Workspaces_WorkspaceId",
table: "Channels",
column: "WorkspaceId",
principalTable: "Workspaces",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "FK_Clients_Workspaces_WorkspaceId",
table: "Clients",
column: "WorkspaceId",
principalTable: "Workspaces",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "FK_Comments_Comments_ParentCommentId",
table: "Comments",
column: "ParentCommentId",
principalTable: "Comments",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "FK_Comments_ContentItems_ContentItemId",
table: "Comments",
column: "ContentItemId",
principalTable: "ContentItems",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "FK_Comments_Workspaces_WorkspaceId",
table: "Comments",
column: "WorkspaceId",
principalTable: "Workspaces",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "FK_ContentItemActivityEntries_ContentItems_ContentItemId",
table: "ContentItemActivityEntries",
column: "ContentItemId",
principalTable: "ContentItems",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "FK_ContentItemActivityEntries_Workspaces_WorkspaceId",
table: "ContentItemActivityEntries",
column: "WorkspaceId",
principalTable: "Workspaces",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "FK_ContentItemRevisions_ContentItems_ContentItemId",
table: "ContentItemRevisions",
column: "ContentItemId",
principalTable: "ContentItems",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
migrationBuilder.AddForeignKey(
name: "FK_ContentItems_Campaigns_CampaignId",
table: "ContentItems",
column: "CampaignId",
principalTable: "Campaigns",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "FK_ContentItems_Clients_ClientId",
table: "ContentItems",
column: "ClientId",
principalTable: "Clients",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "FK_ContentItems_Workspaces_WorkspaceId",
table: "ContentItems",
column: "WorkspaceId",
principalTable: "Workspaces",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "FK_FeedbackReports_Campaigns_CampaignId",
table: "FeedbackReports",
column: "CampaignId",
principalTable: "Campaigns",
principalColumn: "Id",
onDelete: ReferentialAction.SetNull);
migrationBuilder.AddForeignKey(
name: "FK_FeedbackReports_Clients_ClientId",
table: "FeedbackReports",
column: "ClientId",
principalTable: "Clients",
principalColumn: "Id",
onDelete: ReferentialAction.SetNull);
migrationBuilder.AddForeignKey(
name: "FK_FeedbackReports_ContentItems_ContentItemId",
table: "FeedbackReports",
column: "ContentItemId",
principalTable: "ContentItems",
principalColumn: "Id",
onDelete: ReferentialAction.SetNull);
migrationBuilder.AddForeignKey(
name: "FK_FeedbackReports_Workspaces_WorkspaceId",
table: "FeedbackReports",
column: "WorkspaceId",
principalTable: "Workspaces",
principalColumn: "Id",
onDelete: ReferentialAction.SetNull);
migrationBuilder.AddForeignKey(
name: "FK_NotificationEvents_ContentItems_ContentItemId",
table: "NotificationEvents",
column: "ContentItemId",
principalTable: "ContentItems",
principalColumn: "Id",
onDelete: ReferentialAction.SetNull);
migrationBuilder.AddForeignKey(
name: "FK_NotificationEvents_Workspaces_WorkspaceId",
table: "NotificationEvents",
column: "WorkspaceId",
principalTable: "Workspaces",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "FK_WorkspaceApprovalStepConfigurations_Workspaces_WorkspaceId",
table: "WorkspaceApprovalStepConfigurations",
column: "WorkspaceId",
principalTable: "Workspaces",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "FK_WorkspaceInvites_Workspaces_WorkspaceId",
table: "WorkspaceInvites",
column: "WorkspaceId",
principalTable: "Workspaces",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_ApprovalDecisions_ApprovalRequests_ApprovalRequestId",
table: "ApprovalDecisions");
migrationBuilder.DropForeignKey(
name: "FK_ApprovalRequests_ApprovalWorkflowInstances_WorkflowInstance~",
table: "ApprovalRequests");
migrationBuilder.DropForeignKey(
name: "FK_ApprovalRequests_ContentItems_ContentItemId",
table: "ApprovalRequests");
migrationBuilder.DropForeignKey(
name: "FK_ApprovalRequests_Workspaces_WorkspaceId",
table: "ApprovalRequests");
migrationBuilder.DropForeignKey(
name: "FK_ApprovalWorkflowInstances_ContentItems_ContentItemId",
table: "ApprovalWorkflowInstances");
migrationBuilder.DropForeignKey(
name: "FK_ApprovalWorkflowInstances_Workspaces_WorkspaceId",
table: "ApprovalWorkflowInstances");
migrationBuilder.DropForeignKey(
name: "FK_AssetRevisions_Assets_AssetId",
table: "AssetRevisions");
migrationBuilder.DropForeignKey(
name: "FK_Assets_ContentItems_ContentItemId",
table: "Assets");
migrationBuilder.DropForeignKey(
name: "FK_Assets_Workspaces_WorkspaceId",
table: "Assets");
migrationBuilder.DropForeignKey(
name: "FK_Campaigns_Clients_ClientId",
table: "Campaigns");
migrationBuilder.DropForeignKey(
name: "FK_Campaigns_Workspaces_WorkspaceId",
table: "Campaigns");
migrationBuilder.DropForeignKey(
name: "FK_Channels_Workspaces_WorkspaceId",
table: "Channels");
migrationBuilder.DropForeignKey(
name: "FK_Clients_Workspaces_WorkspaceId",
table: "Clients");
migrationBuilder.DropForeignKey(
name: "FK_Comments_Comments_ParentCommentId",
table: "Comments");
migrationBuilder.DropForeignKey(
name: "FK_Comments_ContentItems_ContentItemId",
table: "Comments");
migrationBuilder.DropForeignKey(
name: "FK_Comments_Workspaces_WorkspaceId",
table: "Comments");
migrationBuilder.DropForeignKey(
name: "FK_ContentItemActivityEntries_ContentItems_ContentItemId",
table: "ContentItemActivityEntries");
migrationBuilder.DropForeignKey(
name: "FK_ContentItemActivityEntries_Workspaces_WorkspaceId",
table: "ContentItemActivityEntries");
migrationBuilder.DropForeignKey(
name: "FK_ContentItemRevisions_ContentItems_ContentItemId",
table: "ContentItemRevisions");
migrationBuilder.DropForeignKey(
name: "FK_ContentItems_Campaigns_CampaignId",
table: "ContentItems");
migrationBuilder.DropForeignKey(
name: "FK_ContentItems_Clients_ClientId",
table: "ContentItems");
migrationBuilder.DropForeignKey(
name: "FK_ContentItems_Workspaces_WorkspaceId",
table: "ContentItems");
migrationBuilder.DropForeignKey(
name: "FK_FeedbackReports_Campaigns_CampaignId",
table: "FeedbackReports");
migrationBuilder.DropForeignKey(
name: "FK_FeedbackReports_Clients_ClientId",
table: "FeedbackReports");
migrationBuilder.DropForeignKey(
name: "FK_FeedbackReports_ContentItems_ContentItemId",
table: "FeedbackReports");
migrationBuilder.DropForeignKey(
name: "FK_FeedbackReports_Workspaces_WorkspaceId",
table: "FeedbackReports");
migrationBuilder.DropForeignKey(
name: "FK_NotificationEvents_ContentItems_ContentItemId",
table: "NotificationEvents");
migrationBuilder.DropForeignKey(
name: "FK_NotificationEvents_Workspaces_WorkspaceId",
table: "NotificationEvents");
migrationBuilder.DropForeignKey(
name: "FK_WorkspaceApprovalStepConfigurations_Workspaces_WorkspaceId",
table: "WorkspaceApprovalStepConfigurations");
migrationBuilder.DropForeignKey(
name: "FK_WorkspaceInvites_Workspaces_WorkspaceId",
table: "WorkspaceInvites");
migrationBuilder.DropIndex(
name: "IX_FeedbackReports_CampaignId",
table: "FeedbackReports");
migrationBuilder.DropIndex(
name: "IX_FeedbackReports_ClientId",
table: "FeedbackReports");
migrationBuilder.DropIndex(
name: "IX_FeedbackReports_ContentItemId",
table: "FeedbackReports");
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,118 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional
namespace Socialize.Api.Migrations
{
/// <inheritdoc />
internal partial class AddOrganizationMembershipTiers : Migration
{
private static readonly string[] MembershipTierSeedColumns =
[
"Id",
"ActiveContentLimit",
"Description",
"ExternalReviewerLimit",
"IsCustom",
"Key",
"MemberLimit",
"MonthlyPriceCents",
"Name",
"SortOrder",
"WorkspaceLimit"
];
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
ArgumentNullException.ThrowIfNull(migrationBuilder);
migrationBuilder.AddColumn<Guid>(
name: "MembershipTierId",
table: "Organizations",
type: "uuid",
nullable: false,
defaultValue: new Guid("20000000-0000-0000-0000-000000000001"));
migrationBuilder.CreateTable(
name: "OrganizationMembershipTiers",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
Key = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
Name = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: false),
Description = table.Column<string>(type: "character varying(512)", maxLength: 512, nullable: false),
MonthlyPriceCents = table.Column<int>(type: "integer", nullable: true),
WorkspaceLimit = table.Column<int>(type: "integer", nullable: true),
ActiveContentLimit = table.Column<int>(type: "integer", nullable: true),
MemberLimit = table.Column<int>(type: "integer", nullable: true),
ExternalReviewerLimit = table.Column<int>(type: "integer", nullable: true),
IsCustom = table.Column<bool>(type: "boolean", nullable: false),
SortOrder = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_OrganizationMembershipTiers", x => x.Id);
});
migrationBuilder.InsertData(
table: "OrganizationMembershipTiers",
columns: MembershipTierSeedColumns,
values: new object[,]
{
{ new Guid("20000000-0000-0000-0000-000000000001"), 3, "For trying Socialize on one real approval workflow.", 1, false, "free", 2, 0, "Free", 10, 1 },
{ new Guid("20000000-0000-0000-0000-000000000002"), 25, "For solo operators managing recurring client reviews.", 10, false, "freelance", 5, 1900, "Freelance", 20, 3 },
{ new Guid("20000000-0000-0000-0000-000000000003"), 250, "For agencies that need repeatable client approval operations.", null, false, "agency", 25, 7900, "Agency", 30, 15 },
{ new Guid("20000000-0000-0000-0000-000000000004"), null, "For larger organizations with governance and access needs.", null, true, "enterprise", null, null, "Enterprise", 40, null }
});
migrationBuilder.CreateIndex(
name: "IX_Organizations_MembershipTierId",
table: "Organizations",
column: "MembershipTierId");
migrationBuilder.CreateIndex(
name: "IX_OrganizationMembershipTiers_Key",
table: "OrganizationMembershipTiers",
column: "Key",
unique: true);
migrationBuilder.CreateIndex(
name: "IX_OrganizationMembershipTiers_SortOrder",
table: "OrganizationMembershipTiers",
column: "SortOrder");
migrationBuilder.AddForeignKey(
name: "FK_Organizations_OrganizationMembershipTiers_MembershipTierId",
table: "Organizations",
column: "MembershipTierId",
principalTable: "OrganizationMembershipTiers",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
ArgumentNullException.ThrowIfNull(migrationBuilder);
migrationBuilder.DropForeignKey(
name: "FK_Organizations_OrganizationMembershipTiers_MembershipTierId",
table: "Organizations");
migrationBuilder.DropTable(
name: "OrganizationMembershipTiers");
migrationBuilder.DropIndex(
name: "IX_Organizations_MembershipTierId",
table: "Organizations");
migrationBuilder.DropColumn(
name: "MembershipTierId",
table: "Organizations");
}
}
}

View File

@@ -0,0 +1,137 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional
namespace Socialize.Api.Migrations
{
/// <inheritdoc />
internal partial class LocalizeOrganizationMembershipTiers : Migration
{
private static readonly string[] MembershipTierTranslationSeedColumns =
[
"Id",
"Culture",
"Description",
"MembershipTierId",
"Name"
];
private static readonly string[] MembershipTierColumnsToRestore =
[
"Description",
"Name"
];
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
ArgumentNullException.ThrowIfNull(migrationBuilder);
migrationBuilder.DropColumn(
name: "Description",
table: "OrganizationMembershipTiers");
migrationBuilder.DropColumn(
name: "Name",
table: "OrganizationMembershipTiers");
migrationBuilder.CreateTable(
name: "OrganizationMembershipTierTranslations",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
MembershipTierId = table.Column<Guid>(type: "uuid", nullable: false),
Culture = table.Column<string>(type: "character varying(16)", maxLength: 16, nullable: false),
Name = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: false),
Description = table.Column<string>(type: "character varying(512)", maxLength: 512, nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_OrganizationMembershipTierTranslations", x => x.Id);
table.ForeignKey(
name: "FK_OrganizationMembershipTierTranslations_OrganizationMembersh~",
column: x => x.MembershipTierId,
principalTable: "OrganizationMembershipTiers",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.InsertData(
table: "OrganizationMembershipTierTranslations",
columns: MembershipTierTranslationSeedColumns,
values: new object[,]
{
{ new Guid("20000000-0000-0001-0000-000000000001"), "en", "For trying Socialize on one real approval workflow.", new Guid("20000000-0000-0000-0000-000000000001"), "Free" },
{ new Guid("20000000-0000-0001-0000-000000000002"), "fr", "Pour essayer Socialize sur un vrai workflow d'approbation.", new Guid("20000000-0000-0000-0000-000000000001"), "Free" },
{ new Guid("20000000-0000-0001-0000-000000000003"), "en", "For solo operators managing recurring client reviews.", new Guid("20000000-0000-0000-0000-000000000002"), "Freelance" },
{ new Guid("20000000-0000-0001-0000-000000000004"), "fr", "Pour les independants qui gerent des revisions client recurrentes.", new Guid("20000000-0000-0000-0000-000000000002"), "Freelance" },
{ new Guid("20000000-0000-0001-0000-000000000005"), "en", "For agencies that need repeatable client approval operations.", new Guid("20000000-0000-0000-0000-000000000003"), "Agency" },
{ new Guid("20000000-0000-0001-0000-000000000006"), "fr", "Pour les agences qui veulent des operations d'approbation client repetables.", new Guid("20000000-0000-0000-0000-000000000003"), "Agency" },
{ new Guid("20000000-0000-0001-0000-000000000007"), "en", "For larger organizations with governance and access needs.", new Guid("20000000-0000-0000-0000-000000000004"), "Enterprise" },
{ new Guid("20000000-0000-0001-0000-000000000008"), "fr", "Pour les grandes organisations avec des besoins de gouvernance et d'acces.", new Guid("20000000-0000-0000-0000-000000000004"), "Enterprise" }
});
migrationBuilder.CreateIndex(
name: "IX_OrganizationMembershipTierTranslations_MembershipTierId_Cul~",
table: "OrganizationMembershipTierTranslations",
columns: ["MembershipTierId", "Culture"],
unique: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
ArgumentNullException.ThrowIfNull(migrationBuilder);
migrationBuilder.DropTable(
name: "OrganizationMembershipTierTranslations");
migrationBuilder.AddColumn<string>(
name: "Description",
table: "OrganizationMembershipTiers",
type: "character varying(512)",
maxLength: 512,
nullable: false,
defaultValue: "");
migrationBuilder.AddColumn<string>(
name: "Name",
table: "OrganizationMembershipTiers",
type: "character varying(128)",
maxLength: 128,
nullable: false,
defaultValue: "");
migrationBuilder.UpdateData(
table: "OrganizationMembershipTiers",
keyColumn: "Id",
keyValue: new Guid("20000000-0000-0000-0000-000000000001"),
columns: MembershipTierColumnsToRestore,
values: new object[] { "For trying Socialize on one real approval workflow.", "Free" });
migrationBuilder.UpdateData(
table: "OrganizationMembershipTiers",
keyColumn: "Id",
keyValue: new Guid("20000000-0000-0000-0000-000000000002"),
columns: MembershipTierColumnsToRestore,
values: new object[] { "For solo operators managing recurring client reviews.", "Freelance" });
migrationBuilder.UpdateData(
table: "OrganizationMembershipTiers",
keyColumn: "Id",
keyValue: new Guid("20000000-0000-0000-0000-000000000003"),
columns: MembershipTierColumnsToRestore,
values: new object[] { "For agencies that need repeatable client approval operations.", "Agency" });
migrationBuilder.UpdateData(
table: "OrganizationMembershipTiers",
keyColumn: "Id",
keyValue: new Guid("20000000-0000-0000-0000-000000000004"),
columns: MembershipTierColumnsToRestore,
values: new object[] { "For larger organizations with governance and access needs.", "Enterprise" });
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,197 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Socialize.Api.Migrations
{
/// <inheritdoc />
internal partial class AddReleaseCommunications : Migration
{
private static readonly string[] ReleaseUpdateReadReceiptUniqueIndexColumns =
[
"ReleaseUpdateId",
"UserId",
];
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<DateTimeOffset>(
name: "LastAuthenticatedAt",
table: "AspNetUsers",
type: "timestamp with time zone",
nullable: true);
migrationBuilder.CreateTable(
name: "ReleaseUpdateEmailDigestReceipts",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
UserId = table.Column<Guid>(type: "uuid", nullable: false),
SentAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP"),
UpdateCount = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ReleaseUpdateEmailDigestReceipts", x => x.Id);
});
migrationBuilder.CreateTable(
name: "ReleaseUpdates",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
Title = table.Column<string>(type: "character varying(160)", maxLength: 160, nullable: false),
Summary = table.Column<string>(type: "character varying(512)", maxLength: 512, nullable: false),
Body = table.Column<string>(type: "character varying(8000)", maxLength: 8000, nullable: true),
Category = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
Importance = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
Audience = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
Status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
DeploymentLabel = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: true),
BuildVersion = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: true),
CommitRange = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
CreatedByUserId = table.Column<Guid>(type: "uuid", nullable: false),
CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP"),
UpdatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
PublishedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
ArchivedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
ManualEmailSentByUserId = table.Column<Guid>(type: "uuid", nullable: true),
ManualEmailSentAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
ManualEmailAudience = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
ManualEmailRecipientCount = table.Column<int>(type: "integer", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_ReleaseUpdates", x => x.Id);
});
migrationBuilder.CreateTable(
name: "ReleaseCommits",
columns: table => new
{
Sha = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
ShortSha = table.Column<string>(type: "character varying(16)", maxLength: 16, nullable: false),
Subject = table.Column<string>(type: "character varying(512)", maxLength: 512, nullable: false),
AuthorName = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
AuthorEmail = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
AuthoredAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
CommittedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
SourceBranch = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
DeploymentLabel = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: true),
ExternalUrl = table.Column<string>(type: "character varying(2048)", maxLength: 2048, nullable: true),
CommunicationStatus = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
ReleaseUpdateId = table.Column<Guid>(type: "uuid", nullable: true),
ImportedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP"),
UpdatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ReleaseCommits", x => x.Sha);
table.ForeignKey(
name: "FK_ReleaseCommits_ReleaseUpdates_ReleaseUpdateId",
column: x => x.ReleaseUpdateId,
principalTable: "ReleaseUpdates",
principalColumn: "Id",
onDelete: ReferentialAction.SetNull);
});
migrationBuilder.CreateTable(
name: "ReleaseUpdateReadReceipts",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
ReleaseUpdateId = table.Column<Guid>(type: "uuid", nullable: false),
UserId = table.Column<Guid>(type: "uuid", nullable: false),
ReadAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP")
},
constraints: table =>
{
table.PrimaryKey("PK_ReleaseUpdateReadReceipts", x => x.Id);
table.ForeignKey(
name: "FK_ReleaseUpdateReadReceipts_ReleaseUpdates_ReleaseUpdateId",
column: x => x.ReleaseUpdateId,
principalTable: "ReleaseUpdates",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_ReleaseCommits_CommittedAt",
table: "ReleaseCommits",
column: "CommittedAt");
migrationBuilder.CreateIndex(
name: "IX_ReleaseCommits_CommunicationStatus",
table: "ReleaseCommits",
column: "CommunicationStatus");
migrationBuilder.CreateIndex(
name: "IX_ReleaseCommits_ReleaseUpdateId",
table: "ReleaseCommits",
column: "ReleaseUpdateId");
migrationBuilder.CreateIndex(
name: "IX_ReleaseUpdateEmailDigestReceipts_SentAt",
table: "ReleaseUpdateEmailDigestReceipts",
column: "SentAt");
migrationBuilder.CreateIndex(
name: "IX_ReleaseUpdateEmailDigestReceipts_UserId",
table: "ReleaseUpdateEmailDigestReceipts",
column: "UserId");
migrationBuilder.CreateIndex(
name: "IX_ReleaseUpdateReadReceipts_ReleaseUpdateId_UserId",
table: "ReleaseUpdateReadReceipts",
columns: ReleaseUpdateReadReceiptUniqueIndexColumns,
unique: true);
migrationBuilder.CreateIndex(
name: "IX_ReleaseUpdateReadReceipts_UserId",
table: "ReleaseUpdateReadReceipts",
column: "UserId");
migrationBuilder.CreateIndex(
name: "IX_ReleaseUpdates_Audience",
table: "ReleaseUpdates",
column: "Audience");
migrationBuilder.CreateIndex(
name: "IX_ReleaseUpdates_CreatedByUserId",
table: "ReleaseUpdates",
column: "CreatedByUserId");
migrationBuilder.CreateIndex(
name: "IX_ReleaseUpdates_PublishedAt",
table: "ReleaseUpdates",
column: "PublishedAt");
migrationBuilder.CreateIndex(
name: "IX_ReleaseUpdates_Status",
table: "ReleaseUpdates",
column: "Status");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "ReleaseCommits");
migrationBuilder.DropTable(
name: "ReleaseUpdateEmailDigestReceipts");
migrationBuilder.DropTable(
name: "ReleaseUpdateReadReceipts");
migrationBuilder.DropTable(
name: "ReleaseUpdates");
migrationBuilder.DropColumn(
name: "LastAuthenticatedAt",
table: "AspNetUsers");
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,139 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Socialize.Api.Migrations
{
/// <inheritdoc />
internal partial class AddGoogleDriveDamFoundation : Migration
{
private static readonly string[] WorkspaceOrganizationSlugIndexColumns =
[
"OrganizationId",
"Slug",
];
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
ArgumentNullException.ThrowIfNull(migrationBuilder);
migrationBuilder.AddColumn<string>(
name: "Slug",
table: "Workspaces",
type: "character varying(96)",
maxLength: 96,
nullable: false,
defaultValue: "");
migrationBuilder.Sql(
"""
WITH normalized AS (
SELECT
"Id",
"OrganizationId",
COALESCE(
NULLIF(
trim(both '-' from lower(regexp_replace(trim("Name"), '[^a-zA-Z0-9]+', '-', 'g'))),
''
),
'workspace'
) AS "BaseSlug"
FROM "Workspaces"
),
numbered AS (
SELECT
"Id",
"BaseSlug",
row_number() OVER (PARTITION BY "OrganizationId", "BaseSlug" ORDER BY "CreatedAt", "Id") AS "SlugIndex"
FROM normalized
)
UPDATE "Workspaces"
SET "Slug" = left(
CASE
WHEN numbered."SlugIndex" = 1 THEN numbered."BaseSlug"
ELSE numbered."BaseSlug" || '-' || numbered."SlugIndex"
END,
96
)
FROM numbered
WHERE "Workspaces"."Id" = numbered."Id";
""");
migrationBuilder.AddColumn<string>(
name: "GoogleDriveRootFolderId",
table: "Organizations",
type: "character varying(256)",
maxLength: 256,
nullable: true);
migrationBuilder.AddColumn<string>(
name: "GoogleDriveRootFolderName",
table: "Organizations",
type: "character varying(256)",
maxLength: 256,
nullable: true);
migrationBuilder.AddColumn<string>(
name: "GoogleDriveRootFolderUrl",
table: "Organizations",
type: "character varying(2048)",
maxLength: 2048,
nullable: true);
migrationBuilder.AddColumn<bool>(
name: "IsGoogleDriveDamEnabled",
table: "Organizations",
type: "boolean",
nullable: false,
defaultValue: false);
migrationBuilder.AddColumn<string>(
name: "GoogleDriveWorkspaceFolderPath",
table: "Assets",
type: "character varying(512)",
maxLength: 512,
nullable: true);
migrationBuilder.CreateIndex(
name: "IX_Workspaces_OrganizationId_Slug",
table: "Workspaces",
columns: WorkspaceOrganizationSlugIndexColumns,
unique: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
ArgumentNullException.ThrowIfNull(migrationBuilder);
migrationBuilder.DropIndex(
name: "IX_Workspaces_OrganizationId_Slug",
table: "Workspaces");
migrationBuilder.DropColumn(
name: "Slug",
table: "Workspaces");
migrationBuilder.DropColumn(
name: "GoogleDriveRootFolderId",
table: "Organizations");
migrationBuilder.DropColumn(
name: "GoogleDriveRootFolderName",
table: "Organizations");
migrationBuilder.DropColumn(
name: "GoogleDriveRootFolderUrl",
table: "Organizations");
migrationBuilder.DropColumn(
name: "IsGoogleDriveDamEnabled",
table: "Organizations");
migrationBuilder.DropColumn(
name: "GoogleDriveWorkspaceFolderPath",
table: "Assets");
}
}
}

View File

@@ -1,6 +1,6 @@
namespace Socialize.Api.Modules.Approvals.Data; namespace Socialize.Api.Modules.Approvals.Data;
public class ApprovalDecision internal class ApprovalDecision
{ {
public Guid Id { get; init; } public Guid Id { get; init; }
public Guid ApprovalRequestId { get; set; } public Guid ApprovalRequestId { get; set; }

View File

@@ -1,8 +1,10 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Socialize.Api.Modules.ContentItems.Data;
using Socialize.Api.Modules.Workspaces.Data;
namespace Socialize.Api.Modules.Approvals.Data; namespace Socialize.Api.Modules.Approvals.Data;
public static class ApprovalModelConfiguration internal static class ApprovalModelConfiguration
{ {
public static ModelBuilder ConfigureApprovalsModule(this ModelBuilder modelBuilder) public static ModelBuilder ConfigureApprovalsModule(this ModelBuilder modelBuilder)
{ {
@@ -20,6 +22,14 @@ public static class ApprovalModelConfiguration
workflowInstance.HasIndex(x => new { x.ContentItemId, x.State }) workflowInstance.HasIndex(x => new { x.ContentItemId, x.State })
.IsUnique() .IsUnique()
.HasFilter("\"State\" = 'Pending'"); .HasFilter("\"State\" = 'Pending'");
workflowInstance.HasOne<Workspace>()
.WithMany()
.HasForeignKey(x => x.WorkspaceId)
.OnDelete(DeleteBehavior.Restrict);
workflowInstance.HasOne<ContentItem>()
.WithMany()
.HasForeignKey(x => x.ContentItemId)
.OnDelete(DeleteBehavior.Restrict);
}); });
modelBuilder.Entity<ApprovalRequest>(approvalRequest => modelBuilder.Entity<ApprovalRequest>(approvalRequest =>
@@ -40,6 +50,18 @@ public static class ApprovalModelConfiguration
approvalRequest.HasIndex(x => x.ContentItemId); approvalRequest.HasIndex(x => x.ContentItemId);
approvalRequest.HasIndex(x => x.WorkflowInstanceId); approvalRequest.HasIndex(x => x.WorkflowInstanceId);
approvalRequest.HasIndex(x => x.ReviewerEmail); approvalRequest.HasIndex(x => x.ReviewerEmail);
approvalRequest.HasOne<Workspace>()
.WithMany()
.HasForeignKey(x => x.WorkspaceId)
.OnDelete(DeleteBehavior.Restrict);
approvalRequest.HasOne<ContentItem>()
.WithMany()
.HasForeignKey(x => x.ContentItemId)
.OnDelete(DeleteBehavior.Restrict);
approvalRequest.HasOne<ApprovalWorkflowInstance>()
.WithMany()
.HasForeignKey(x => x.WorkflowInstanceId)
.OnDelete(DeleteBehavior.Restrict);
}); });
modelBuilder.Entity<ApprovalDecision>(approvalDecision => modelBuilder.Entity<ApprovalDecision>(approvalDecision =>
@@ -54,6 +76,10 @@ public static class ApprovalModelConfiguration
.ValueGeneratedOnAdd() .ValueGeneratedOnAdd()
.HasDefaultValueSql("CURRENT_TIMESTAMP"); .HasDefaultValueSql("CURRENT_TIMESTAMP");
approvalDecision.HasIndex(x => x.ApprovalRequestId); approvalDecision.HasIndex(x => x.ApprovalRequestId);
approvalDecision.HasOne<ApprovalRequest>()
.WithMany()
.HasForeignKey(x => x.ApprovalRequestId)
.OnDelete(DeleteBehavior.Restrict);
}); });
modelBuilder.Entity<WorkspaceApprovalStepConfiguration>(approvalStep => modelBuilder.Entity<WorkspaceApprovalStepConfiguration>(approvalStep =>
@@ -69,6 +95,10 @@ public static class ApprovalModelConfiguration
.HasDefaultValueSql("CURRENT_TIMESTAMP"); .HasDefaultValueSql("CURRENT_TIMESTAMP");
approvalStep.HasIndex(x => x.WorkspaceId); approvalStep.HasIndex(x => x.WorkspaceId);
approvalStep.HasIndex(x => new { x.WorkspaceId, x.SortOrder }).IsUnique(); approvalStep.HasIndex(x => new { x.WorkspaceId, x.SortOrder }).IsUnique();
approvalStep.HasOne<Workspace>()
.WithMany()
.HasForeignKey(x => x.WorkspaceId)
.OnDelete(DeleteBehavior.Restrict);
}); });
return modelBuilder; return modelBuilder;

View File

@@ -1,6 +1,6 @@
namespace Socialize.Api.Modules.Approvals.Data; namespace Socialize.Api.Modules.Approvals.Data;
public class ApprovalRequest internal class ApprovalRequest
{ {
public Guid Id { get; init; } public Guid Id { get; init; }
public Guid WorkspaceId { get; set; } public Guid WorkspaceId { get; set; }

View File

@@ -1,6 +1,6 @@
namespace Socialize.Api.Modules.Approvals.Data; namespace Socialize.Api.Modules.Approvals.Data;
public class ApprovalWorkflowInstance internal class ApprovalWorkflowInstance
{ {
public Guid Id { get; init; } public Guid Id { get; init; }
public Guid WorkspaceId { get; set; } public Guid WorkspaceId { get; set; }

View File

@@ -1,6 +1,6 @@
namespace Socialize.Api.Modules.Approvals.Data; namespace Socialize.Api.Modules.Approvals.Data;
public class WorkspaceApprovalStepConfiguration internal class WorkspaceApprovalStepConfiguration
{ {
public Guid Id { get; init; } public Guid Id { get; init; }
public Guid WorkspaceId { get; set; } public Guid WorkspaceId { get; set; }

View File

@@ -7,9 +7,9 @@ using Socialize.Api.Infrastructure.Security;
namespace Socialize.Api.Modules.Approvals.Handlers; namespace Socialize.Api.Modules.Approvals.Handlers;
public record GetApprovalsRequest(Guid ContentItemId); internal record GetApprovalsRequest(Guid ContentItemId);
public record ApprovalDecisionDto( internal record ApprovalDecisionDto(
Guid Id, Guid Id,
Guid ApprovalRequestId, Guid ApprovalRequestId,
string Decision, string Decision,
@@ -20,7 +20,7 @@ public record ApprovalDecisionDto(
string? DecidedByPortraitUrl, string? DecidedByPortraitUrl,
DateTimeOffset CreatedAt); DateTimeOffset CreatedAt);
public record ApprovalRequestDto( internal record ApprovalRequestDto(
Guid Id, Guid Id,
Guid WorkspaceId, Guid WorkspaceId,
Guid ContentItemId, Guid ContentItemId,
@@ -40,7 +40,7 @@ public record ApprovalRequestDto(
DateTimeOffset? CompletedAt, DateTimeOffset? CompletedAt,
IReadOnlyCollection<ApprovalDecisionDto> Decisions); IReadOnlyCollection<ApprovalDecisionDto> Decisions);
public class GetApprovalsHandler( internal class GetApprovalsHandler(
AppDbContext dbContext, AppDbContext dbContext,
AccessScopeService accessScopeService) AccessScopeService accessScopeService)
: Endpoint<GetApprovalsRequest, IReadOnlyCollection<ApprovalRequestDto>> : Endpoint<GetApprovalsRequest, IReadOnlyCollection<ApprovalRequestDto>>

View File

@@ -3,19 +3,22 @@ using Microsoft.EntityFrameworkCore;
using Socialize.Api.Data; using Socialize.Api.Data;
using Socialize.Api.Infrastructure.Security; using Socialize.Api.Infrastructure.Security;
using Socialize.Api.Modules.ContentItems.Data; using Socialize.Api.Modules.ContentItems.Data;
using Socialize.Api.Modules.ContentItems.Contracts;
using Socialize.Api.Modules.Approvals.Data; using Socialize.Api.Modules.Approvals.Data;
using Socialize.Api.Modules.Approvals.Services; using Socialize.Api.Modules.Approvals.Services;
using Socialize.Api.Modules.Notifications.Contracts; using Socialize.Api.Modules.Notifications.Contracts;
using Socialize.Api.Modules.Workspaces.Data; using Socialize.Api.Modules.Workspaces.Data;
using System.Security.Claims;
using System.Text.Json;
namespace Socialize.Api.Modules.Approvals.Handlers; namespace Socialize.Api.Modules.Approvals.Handlers;
public record SubmitApprovalDecisionRequest( internal record SubmitApprovalDecisionRequest(
string Decision, string Decision,
string? ReviewerName, string? ReviewerName,
string? ReviewerEmail); string? ReviewerEmail);
public class SubmitApprovalDecisionRequestValidator internal class SubmitApprovalDecisionRequestValidator
: Validator<SubmitApprovalDecisionRequest> : Validator<SubmitApprovalDecisionRequest>
{ {
public SubmitApprovalDecisionRequestValidator() public SubmitApprovalDecisionRequestValidator()
@@ -29,10 +32,11 @@ public class SubmitApprovalDecisionRequestValidator
} }
} }
public class SubmitApprovalDecisionHandler( internal class SubmitApprovalDecisionHandler(
AppDbContext dbContext, AppDbContext dbContext,
AccessScopeService accessScopeService, AccessScopeService accessScopeService,
ApprovalWorkflowRuntimeService approvalWorkflowRuntimeService, ApprovalWorkflowRuntimeService approvalWorkflowRuntimeService,
IContentItemActivityWriter activityWriter,
INotificationEventWriter notificationEventWriter) INotificationEventWriter notificationEventWriter)
: Endpoint<SubmitApprovalDecisionRequest, ApprovalRequestDto> : Endpoint<SubmitApprovalDecisionRequest, ApprovalRequestDto>
{ {
@@ -76,12 +80,14 @@ public class SubmitApprovalDecisionHandler(
} }
string normalizedDecision = request.Decision.Trim(); string normalizedDecision = request.Decision.Trim();
string decidedByName = User?.Identity?.IsAuthenticated == true ClaimsPrincipal? currentUser = User;
? User.GetAlias() ?? User.GetName() bool isAuthenticated = currentUser?.Identity?.IsAuthenticated == true;
: string.IsNullOrWhiteSpace(request.ReviewerName) ? approval.ReviewerName : request.ReviewerName.Trim(); string decidedByName = isAuthenticated
string decidedByEmail = User?.Identity?.IsAuthenticated == true ? currentUser!.GetAlias() ?? currentUser!.GetName()
? User.GetEmail() : GetReviewerName(request.ReviewerName, approval.ReviewerName);
: string.IsNullOrWhiteSpace(request.ReviewerEmail) ? approval.ReviewerEmail : request.ReviewerEmail.Trim(); string decidedByEmail = isAuthenticated
? currentUser!.GetEmail()
: GetReviewerEmail(request.ReviewerEmail, approval.ReviewerEmail);
ApprovalDecision decision = new() ApprovalDecision decision = new()
{ {
@@ -120,6 +126,24 @@ public class SubmitApprovalDecisionHandler(
dbContext.ApprovalDecisions.Add(decision); dbContext.ApprovalDecisions.Add(decision);
await dbContext.SaveChangesAsync(ct); await dbContext.SaveChangesAsync(ct);
await activityWriter.WriteAsync(
new ContentItemActivityWriteModel(
approval.WorkspaceId,
approval.ContentItemId,
"approval.decision.recorded",
"ApprovalDecision",
decision.Id,
$"{decidedByName} recorded {normalizedDecision} for {contentItem.Title}.",
decision.DecidedByUserId,
decidedByEmail,
JsonSerializer.Serialize(new
{
stage = approval.Stage,
status = contentItem.Status,
decision = normalizedDecision,
})),
ct);
await notificationEventWriter.WriteAsync( await notificationEventWriter.WriteAsync(
new NotificationEventWriteModel( new NotificationEventWriteModel(
approval.WorkspaceId, approval.WorkspaceId,
@@ -186,4 +210,18 @@ public class SubmitApprovalDecisionHandler(
await SendOkAsync(dto, ct); await SendOkAsync(dto, ct);
} }
private static string GetReviewerName(string? requestedName, string fallbackName)
{
return string.IsNullOrWhiteSpace(requestedName)
? fallbackName
: requestedName.Trim();
}
private static string GetReviewerEmail(string? requestedEmail, string fallbackEmail)
{
return string.IsNullOrWhiteSpace(requestedEmail)
? fallbackEmail
: requestedEmail.Trim();
}
} }

View File

@@ -2,7 +2,7 @@ using Socialize.Api.Modules.Approvals.Services;
namespace Socialize.Api.Modules.Approvals; namespace Socialize.Api.Modules.Approvals;
public static class DependencyInjection internal static class ModuleRegistration
{ {
public static WebApplicationBuilder AddApprovalsModule( public static WebApplicationBuilder AddApprovalsModule(
this WebApplicationBuilder builder) this WebApplicationBuilder builder)

View File

@@ -2,20 +2,20 @@ using Socialize.Api.Modules.Identity.Contracts;
namespace Socialize.Api.Modules.Approvals.Services; namespace Socialize.Api.Modules.Approvals.Services;
public static class ApprovalStepTargetTypes internal static class ApprovalStepTargetTypes
{ {
public const string Role = "Role"; public const string Role = "Role";
public const string Membership = "Membership"; public const string Membership = "Membership";
public const string Member = "Member"; public const string Member = "Member";
} }
public static class ApprovalMembershipTargets internal static class ApprovalMembershipTargets
{ {
public const string Team = "Team"; public const string Team = "Team";
public const string Client = "Client"; public const string Client = "Client";
} }
public static class ApprovalStepConfigurationRules internal static class ApprovalStepConfigurationRules
{ {
public static readonly IReadOnlySet<string> AllowedTargetTypes = new HashSet<string>(StringComparer.Ordinal) public static readonly IReadOnlySet<string> AllowedTargetTypes = new HashSet<string>(StringComparer.Ordinal)
{ {

View File

@@ -2,7 +2,7 @@ using Socialize.Api.Modules.Identity.Contracts;
namespace Socialize.Api.Modules.Approvals.Services; namespace Socialize.Api.Modules.Approvals.Services;
public static class ApprovalModes internal static class ApprovalModes
{ {
public const string None = "None"; public const string None = "None";
public const string Optional = "Optional"; public const string Optional = "Optional";
@@ -10,7 +10,7 @@ public static class ApprovalModes
public const string MultiLevel = "Multi-level"; public const string MultiLevel = "Multi-level";
} }
public static class ApprovalWorkflowRules internal static class ApprovalWorkflowRules
{ {
public static bool BlocksManualApprovedOrScheduledStatus(string approvalMode) public static bool BlocksManualApprovedOrScheduledStatus(string approvalMode)
{ {

View File

@@ -11,15 +11,15 @@ using Socialize.Api.Modules.Workspaces.Data;
namespace Socialize.Api.Modules.Approvals.Services; namespace Socialize.Api.Modules.Approvals.Services;
public record ApprovalWorkflowStartResult(bool Succeeded, string? ErrorMessage); internal record ApprovalWorkflowStartResult(bool Succeeded, string? ErrorMessage);
public record ApprovalWorkflowDecisionResult( internal record ApprovalWorkflowDecisionResult(
bool Succeeded, bool Succeeded,
string? ErrorMessage, string? ErrorMessage,
int StatusCode, int StatusCode,
bool IsWorkflowStep); bool IsWorkflowStep);
public class ApprovalWorkflowRuntimeService( internal class ApprovalWorkflowRuntimeService(
AppDbContext dbContext, AppDbContext dbContext,
INotificationEventWriter notificationEventWriter) INotificationEventWriter notificationEventWriter)
{ {
@@ -145,13 +145,15 @@ public class ApprovalWorkflowRuntimeService(
dbContext.ApprovalDecisions.Add(decision); dbContext.ApprovalDecisions.Add(decision);
await dbContext.SaveChangesAsync(ct); await dbContext.SaveChangesAsync(ct);
int approvedCount = await dbContext.ApprovalDecisions var approvalDecisionParticipants = await dbContext.ApprovalDecisions
.Where(candidate => candidate.ApprovalRequestId == approval.Id && candidate.Decision == ApprovedState) .Where(candidate => candidate.ApprovalRequestId == approval.Id && candidate.Decision == ApprovedState)
.Select(candidate => candidate.DecidedByUserId.HasValue .Select(candidate => candidate.DecidedByUserId.HasValue
? candidate.DecidedByUserId.Value.ToString() ? candidate.DecidedByUserId.Value.ToString()
: candidate.DecidedByEmail.ToLower()) : candidate.DecidedByEmail)
.Distinct() .ToListAsync(ct);
.CountAsync(ct); int approvedCount = approvalDecisionParticipants
.Distinct(StringComparer.OrdinalIgnoreCase)
.Count();
int requiredApproverCount = approval.WorkflowStepRequiredApproverCount ?? 1; int requiredApproverCount = approval.WorkflowStepRequiredApproverCount ?? 1;
if (!ApprovalWorkflowRules.HasRequiredStepApprovals(approvedCount, requiredApproverCount)) if (!ApprovalWorkflowRules.HasRequiredStepApprovals(approvedCount, requiredApproverCount))
@@ -394,7 +396,7 @@ public class ApprovalWorkflowRuntimeService(
private static string CreateAccessToken() private static string CreateAccessToken()
{ {
return Convert.ToHexString(RandomNumberGenerator.GetBytes(16)).ToLowerInvariant(); return Convert.ToHexString(RandomNumberGenerator.GetBytes(16));
} }
private sealed record ApprovalNotificationRecipient(Guid UserId, string? Email); private sealed record ApprovalNotificationRecipient(Guid UserId, string? Email);

View File

@@ -1,6 +1,6 @@
namespace Socialize.Api.Modules.Assets.Data; namespace Socialize.Api.Modules.Assets.Data;
public class Asset internal class Asset
{ {
public Guid Id { get; init; } public Guid Id { get; init; }
public Guid WorkspaceId { get; set; } public Guid WorkspaceId { get; set; }
@@ -10,6 +10,7 @@ public class Asset
public required string DisplayName { get; set; } public required string DisplayName { get; set; }
public string? GoogleDriveFileId { get; set; } public string? GoogleDriveFileId { get; set; }
public string? GoogleDriveLink { get; set; } public string? GoogleDriveLink { get; set; }
public string? GoogleDriveWorkspaceFolderPath { get; set; }
public string? PreviewUrl { get; set; } public string? PreviewUrl { get; set; }
public int CurrentRevisionNumber { get; set; } public int CurrentRevisionNumber { get; set; }
public DateTimeOffset CreatedAt { get; init; } public DateTimeOffset CreatedAt { get; init; }

View File

@@ -1,8 +1,10 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Socialize.Api.Modules.ContentItems.Data;
using Socialize.Api.Modules.Workspaces.Data;
namespace Socialize.Api.Modules.Assets.Data; namespace Socialize.Api.Modules.Assets.Data;
public static class AssetModelConfiguration internal static class AssetModelConfiguration
{ {
public static ModelBuilder ConfigureAssetsModule(this ModelBuilder modelBuilder) public static ModelBuilder ConfigureAssetsModule(this ModelBuilder modelBuilder)
{ {
@@ -15,12 +17,21 @@ public static class AssetModelConfiguration
asset.Property(x => x.DisplayName).HasMaxLength(256).IsRequired(); asset.Property(x => x.DisplayName).HasMaxLength(256).IsRequired();
asset.Property(x => x.GoogleDriveFileId).HasMaxLength(256); asset.Property(x => x.GoogleDriveFileId).HasMaxLength(256);
asset.Property(x => x.GoogleDriveLink).HasMaxLength(2048); asset.Property(x => x.GoogleDriveLink).HasMaxLength(2048);
asset.Property(x => x.GoogleDriveWorkspaceFolderPath).HasMaxLength(512);
asset.Property(x => x.PreviewUrl).HasMaxLength(2048); asset.Property(x => x.PreviewUrl).HasMaxLength(2048);
asset.Property(x => x.CreatedAt) asset.Property(x => x.CreatedAt)
.ValueGeneratedOnAdd() .ValueGeneratedOnAdd()
.HasDefaultValueSql("CURRENT_TIMESTAMP"); .HasDefaultValueSql("CURRENT_TIMESTAMP");
asset.HasIndex(x => x.WorkspaceId); asset.HasIndex(x => x.WorkspaceId);
asset.HasIndex(x => x.ContentItemId); asset.HasIndex(x => x.ContentItemId);
asset.HasOne<Workspace>()
.WithMany()
.HasForeignKey(x => x.WorkspaceId)
.OnDelete(DeleteBehavior.Restrict);
asset.HasOne<ContentItem>()
.WithMany()
.HasForeignKey(x => x.ContentItemId)
.OnDelete(DeleteBehavior.Restrict);
}); });
modelBuilder.Entity<AssetRevision>(revision => modelBuilder.Entity<AssetRevision>(revision =>
@@ -35,6 +46,10 @@ public static class AssetModelConfiguration
.HasDefaultValueSql("CURRENT_TIMESTAMP"); .HasDefaultValueSql("CURRENT_TIMESTAMP");
revision.HasIndex(x => x.AssetId); revision.HasIndex(x => x.AssetId);
revision.HasIndex(x => new { x.AssetId, x.RevisionNumber }).IsUnique(); revision.HasIndex(x => new { x.AssetId, x.RevisionNumber }).IsUnique();
revision.HasOne<Asset>()
.WithMany()
.HasForeignKey(x => x.AssetId)
.OnDelete(DeleteBehavior.Cascade);
}); });
return modelBuilder; return modelBuilder;

View File

@@ -1,6 +1,6 @@
namespace Socialize.Api.Modules.Assets.Data; namespace Socialize.Api.Modules.Assets.Data;
public class AssetRevision internal class AssetRevision
{ {
public Guid Id { get; init; } public Guid Id { get; init; }
public Guid AssetId { get; set; } public Guid AssetId { get; set; }

View File

@@ -3,17 +3,19 @@ using Microsoft.EntityFrameworkCore;
using Socialize.Api.Data; using Socialize.Api.Data;
using Socialize.Api.Infrastructure.Security; using Socialize.Api.Infrastructure.Security;
using Socialize.Api.Modules.Assets.Data; using Socialize.Api.Modules.Assets.Data;
using Socialize.Api.Modules.ContentItems.Contracts;
using Socialize.Api.Modules.ContentItems.Data; using Socialize.Api.Modules.ContentItems.Data;
using Socialize.Api.Modules.Notifications.Contracts; using Socialize.Api.Modules.Notifications.Contracts;
using System.Text.Json;
namespace Socialize.Api.Modules.Assets.Handlers; namespace Socialize.Api.Modules.Assets.Handlers;
public record CreateAssetRevisionRequest( internal record CreateAssetRevisionRequest(
string SourceReference, string SourceReference,
string? PreviewUrl, string? PreviewUrl,
string? Notes); string? Notes);
public class CreateAssetRevisionRequestValidator internal class CreateAssetRevisionRequestValidator
: Validator<CreateAssetRevisionRequest> : Validator<CreateAssetRevisionRequest>
{ {
public CreateAssetRevisionRequestValidator() public CreateAssetRevisionRequestValidator()
@@ -24,9 +26,10 @@ public class CreateAssetRevisionRequestValidator
} }
} }
public class CreateAssetRevisionHandler( internal class CreateAssetRevisionHandler(
AppDbContext dbContext, AppDbContext dbContext,
AccessScopeService accessScopeService, AccessScopeService accessScopeService,
IContentItemActivityWriter activityWriter,
INotificationEventWriter notificationEventWriter) INotificationEventWriter notificationEventWriter)
: Endpoint<CreateAssetRevisionRequest, AssetRevisionDto> : Endpoint<CreateAssetRevisionRequest, AssetRevisionDto>
{ {
@@ -78,6 +81,25 @@ public class CreateAssetRevisionHandler(
if (contentItem is not null) if (contentItem is not null)
{ {
await activityWriter.WriteAsync(
new ContentItemActivityWriteModel(
asset.WorkspaceId,
asset.ContentItemId,
"asset.revision.created",
"AssetRevision",
revision.Id,
$"A new asset revision was added to {asset.DisplayName}.",
User.GetUserId(),
User.GetEmail(),
JsonSerializer.Serialize(new
{
assetId = asset.Id,
revisionNumber,
sourceReference = revision.SourceReference,
notes = revision.Notes,
})),
ct);
await notificationEventWriter.WriteAsync( await notificationEventWriter.WriteAsync(
new NotificationEventWriteModel( new NotificationEventWriteModel(
asset.WorkspaceId, asset.WorkspaceId,

View File

@@ -3,12 +3,16 @@ using Microsoft.EntityFrameworkCore;
using Socialize.Api.Data; using Socialize.Api.Data;
using Socialize.Api.Infrastructure.Security; using Socialize.Api.Infrastructure.Security;
using Socialize.Api.Modules.Assets.Data; using Socialize.Api.Modules.Assets.Data;
using Socialize.Api.Modules.ContentItems.Contracts;
using Socialize.Api.Modules.ContentItems.Data; using Socialize.Api.Modules.ContentItems.Data;
using Socialize.Api.Modules.Notifications.Contracts; using Socialize.Api.Modules.Notifications.Contracts;
using Socialize.Api.Modules.Organizations.Data;
using Socialize.Api.Modules.Workspaces.Data;
using System.Text.Json;
namespace Socialize.Api.Modules.Assets.Handlers; namespace Socialize.Api.Modules.Assets.Handlers;
public record CreateGoogleDriveAssetRequest( internal record CreateGoogleDriveAssetRequest(
Guid WorkspaceId, Guid WorkspaceId,
Guid ContentItemId, Guid ContentItemId,
string AssetType, string AssetType,
@@ -17,7 +21,7 @@ public record CreateGoogleDriveAssetRequest(
string GoogleDriveLink, string GoogleDriveLink,
string? PreviewUrl); string? PreviewUrl);
public class CreateGoogleDriveAssetRequestValidator internal class CreateGoogleDriveAssetRequestValidator
: Validator<CreateGoogleDriveAssetRequest> : Validator<CreateGoogleDriveAssetRequest>
{ {
public CreateGoogleDriveAssetRequestValidator() public CreateGoogleDriveAssetRequestValidator()
@@ -32,9 +36,10 @@ public class CreateGoogleDriveAssetRequestValidator
} }
} }
public class CreateGoogleDriveAssetHandler( internal class CreateGoogleDriveAssetHandler(
AppDbContext dbContext, AppDbContext dbContext,
AccessScopeService accessScopeService, AccessScopeService accessScopeService,
IContentItemActivityWriter activityWriter,
INotificationEventWriter notificationEventWriter) INotificationEventWriter notificationEventWriter)
: Endpoint<CreateGoogleDriveAssetRequest, AssetDto> : Endpoint<CreateGoogleDriveAssetRequest, AssetDto>
{ {
@@ -64,6 +69,26 @@ public class CreateGoogleDriveAssetHandler(
return; return;
} }
Workspace? workspace = await dbContext.Workspaces
.SingleOrDefaultAsync(candidate => candidate.Id == contentItem.WorkspaceId, ct);
if (workspace is null)
{
await SendNotFoundAsync(ct);
return;
}
Organization? organization = await dbContext.Organizations
.SingleOrDefaultAsync(candidate => candidate.Id == workspace.OrganizationId, ct);
if (organization is null)
{
await SendNotFoundAsync(ct);
return;
}
string? workspaceFolderPath = organization.IsGoogleDriveDamEnabled
? $"{organization.GoogleDriveRootFolderName}/{workspace.Slug}"
: null;
Asset asset = new() Asset asset = new()
{ {
Id = Guid.NewGuid(), Id = Guid.NewGuid(),
@@ -74,6 +99,7 @@ public class CreateGoogleDriveAssetHandler(
DisplayName = request.DisplayName.Trim(), DisplayName = request.DisplayName.Trim(),
GoogleDriveFileId = request.GoogleDriveFileId.Trim(), GoogleDriveFileId = request.GoogleDriveFileId.Trim(),
GoogleDriveLink = request.GoogleDriveLink.Trim(), GoogleDriveLink = request.GoogleDriveLink.Trim(),
GoogleDriveWorkspaceFolderPath = workspaceFolderPath,
PreviewUrl = string.IsNullOrWhiteSpace(request.PreviewUrl) ? null : request.PreviewUrl.Trim(), PreviewUrl = string.IsNullOrWhiteSpace(request.PreviewUrl) ? null : request.PreviewUrl.Trim(),
CurrentRevisionNumber = 1, CurrentRevisionNumber = 1,
CreatedAt = DateTimeOffset.UtcNow, CreatedAt = DateTimeOffset.UtcNow,
@@ -93,6 +119,26 @@ public class CreateGoogleDriveAssetHandler(
dbContext.AssetRevisions.Add(revision); dbContext.AssetRevisions.Add(revision);
await dbContext.SaveChangesAsync(ct); await dbContext.SaveChangesAsync(ct);
await activityWriter.WriteAsync(
new ContentItemActivityWriteModel(
asset.WorkspaceId,
asset.ContentItemId,
"asset.google-drive-linked",
"Asset",
asset.Id,
$"Google Drive asset {asset.DisplayName} was linked to {contentItem.Title}.",
User.GetUserId(),
User.GetEmail(),
JsonSerializer.Serialize(new
{
assetType = asset.AssetType,
sourceType = asset.SourceType,
googleDriveFileId = asset.GoogleDriveFileId,
googleDriveWorkspaceFolderPath = asset.GoogleDriveWorkspaceFolderPath,
currentRevisionNumber = asset.CurrentRevisionNumber,
})),
ct);
await notificationEventWriter.WriteAsync( await notificationEventWriter.WriteAsync(
new NotificationEventWriteModel( new NotificationEventWriteModel(
asset.WorkspaceId, asset.WorkspaceId,
@@ -115,6 +161,7 @@ public class CreateGoogleDriveAssetHandler(
asset.DisplayName, asset.DisplayName,
asset.GoogleDriveFileId, asset.GoogleDriveFileId,
asset.GoogleDriveLink, asset.GoogleDriveLink,
asset.GoogleDriveWorkspaceFolderPath,
asset.PreviewUrl, asset.PreviewUrl,
asset.CurrentRevisionNumber, asset.CurrentRevisionNumber,
asset.CreatedAt, asset.CreatedAt,

View File

@@ -5,9 +5,9 @@ using Socialize.Api.Infrastructure.Security;
namespace Socialize.Api.Modules.Assets.Handlers; namespace Socialize.Api.Modules.Assets.Handlers;
public record GetAssetsRequest(Guid ContentItemId); internal record GetAssetsRequest(Guid ContentItemId);
public record AssetRevisionDto( internal record AssetRevisionDto(
Guid Id, Guid Id,
Guid AssetId, Guid AssetId,
int RevisionNumber, int RevisionNumber,
@@ -17,7 +17,7 @@ public record AssetRevisionDto(
Guid? CreatedByUserId, Guid? CreatedByUserId,
DateTimeOffset CreatedAt); DateTimeOffset CreatedAt);
public record AssetDto( internal record AssetDto(
Guid Id, Guid Id,
Guid WorkspaceId, Guid WorkspaceId,
Guid ContentItemId, Guid ContentItemId,
@@ -26,12 +26,13 @@ public record AssetDto(
string DisplayName, string DisplayName,
string? GoogleDriveFileId, string? GoogleDriveFileId,
string? GoogleDriveLink, string? GoogleDriveLink,
string? GoogleDriveWorkspaceFolderPath,
string? PreviewUrl, string? PreviewUrl,
int CurrentRevisionNumber, int CurrentRevisionNumber,
DateTimeOffset CreatedAt, DateTimeOffset CreatedAt,
IReadOnlyCollection<AssetRevisionDto> Revisions); IReadOnlyCollection<AssetRevisionDto> Revisions);
public class GetAssetsHandler( internal class GetAssetsHandler(
AppDbContext dbContext, AppDbContext dbContext,
AccessScopeService accessScopeService) AccessScopeService accessScopeService)
: Endpoint<GetAssetsRequest, IReadOnlyCollection<AssetDto>> : Endpoint<GetAssetsRequest, IReadOnlyCollection<AssetDto>>
@@ -70,6 +71,7 @@ public class GetAssetsHandler(
asset.DisplayName, asset.DisplayName,
asset.GoogleDriveFileId, asset.GoogleDriveFileId,
asset.GoogleDriveLink, asset.GoogleDriveLink,
asset.GoogleDriveWorkspaceFolderPath,
asset.PreviewUrl, asset.PreviewUrl,
asset.CurrentRevisionNumber, asset.CurrentRevisionNumber,
asset.CreatedAt, asset.CreatedAt,

View File

@@ -0,0 +1,123 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using Socialize.Api.Data;
using Socialize.Api.Infrastructure.Security;
using Socialize.Api.Modules.Organizations.Data;
using Socialize.Api.Modules.Workspaces.Data;
namespace Socialize.Api.Modules.Assets.Handlers;
internal record WorkspaceDamBackingStoreDto(
string Type,
bool IsConfigured,
string? RootFolderId,
string? RootFolderName,
string? RootFolderUrl);
internal record WorkspaceDamFolderDto(
string Name,
string Path);
internal record WorkspaceDamDto(
Guid WorkspaceId,
Guid OrganizationId,
string WorkspaceName,
string WorkspaceSlug,
WorkspaceDamBackingStoreDto BackingStore,
WorkspaceDamFolderDto? Folder,
IReadOnlyCollection<AssetDto> Assets);
internal class GetWorkspaceDamHandler(
AppDbContext dbContext,
AccessScopeService accessScopeService)
: EndpointWithoutRequest<WorkspaceDamDto>
{
public override void Configure()
{
Get("/api/workspaces/{workspaceId:guid}/dam");
Options(o => o.WithTags("Assets"));
}
public override async Task HandleAsync(CancellationToken ct)
{
Guid workspaceId = Route<Guid>("workspaceId");
Workspace? workspace = await dbContext.Workspaces
.SingleOrDefaultAsync(candidate => candidate.Id == workspaceId, ct);
if (workspace is null)
{
await SendNotFoundAsync(ct);
return;
}
IReadOnlyCollection<Guid> accessibleWorkspaceIds = await accessScopeService.GetAccessibleWorkspaceIdsAsync(User, ct);
if (!accessibleWorkspaceIds.Contains(workspace.Id))
{
await SendForbiddenAsync(ct);
return;
}
Organization? organization = await dbContext.Organizations
.SingleOrDefaultAsync(candidate => candidate.Id == workspace.OrganizationId, ct);
if (organization is null)
{
await SendNotFoundAsync(ct);
return;
}
WorkspaceDamBackingStoreDto backingStore = new(
organization.IsGoogleDriveDamEnabled ? "GoogleDrive" : "Unconfigured",
organization.IsGoogleDriveDamEnabled,
organization.GoogleDriveRootFolderId,
organization.GoogleDriveRootFolderName,
organization.GoogleDriveRootFolderUrl);
WorkspaceDamFolderDto? folder = organization.IsGoogleDriveDamEnabled
? new WorkspaceDamFolderDto(
workspace.Slug,
$"{organization.GoogleDriveRootFolderName}/{workspace.Slug}")
: null;
List<AssetDto> assets = await dbContext.Assets
.Where(asset => asset.WorkspaceId == workspace.Id)
.OrderBy(asset => asset.DisplayName)
.Select(asset => new AssetDto(
asset.Id,
asset.WorkspaceId,
asset.ContentItemId,
asset.AssetType,
asset.SourceType,
asset.DisplayName,
asset.GoogleDriveFileId,
asset.GoogleDriveLink,
asset.GoogleDriveWorkspaceFolderPath,
asset.PreviewUrl,
asset.CurrentRevisionNumber,
asset.CreatedAt,
dbContext.AssetRevisions
.Where(revision => revision.AssetId == asset.Id)
.OrderByDescending(revision => revision.RevisionNumber)
.Select(revision => new AssetRevisionDto(
revision.Id,
revision.AssetId,
revision.RevisionNumber,
revision.SourceReference,
revision.PreviewUrl,
revision.Notes,
revision.CreatedByUserId,
revision.CreatedAt))
.ToList()))
.ToListAsync(ct);
await SendOkAsync(
new WorkspaceDamDto(
workspace.Id,
workspace.OrganizationId,
workspace.Name,
workspace.Slug,
backingStore,
folder,
assets),
ct);
}
}

View File

@@ -2,7 +2,7 @@ using Socialize.Api.Modules.Assets.Data;
namespace Socialize.Api.Modules.Assets; namespace Socialize.Api.Modules.Assets;
public static class DependencyInjection internal static class ModuleRegistration
{ {
public static WebApplicationBuilder AddAssetsModule( public static WebApplicationBuilder AddAssetsModule(
this WebApplicationBuilder builder) this WebApplicationBuilder builder)

View File

@@ -0,0 +1,18 @@
namespace Socialize.Api.Modules.CalendarIntegrations.Data;
internal class CalendarCatalogEntry
{
public Guid Id { get; init; }
public required string Title { get; set; }
public required string Description { get; set; }
public string? Country { get; set; }
public string? Region { get; set; }
public required string Language { get; set; }
public required string Category { get; set; }
public string? CultureOrReligion { get; set; }
public required string ProviderName { get; set; }
public required string SourceUrl { get; set; }
public required string TrustLevel { get; set; }
public required string DefaultColor { get; set; }
public DateTimeOffset CreatedAt { get; init; }
}

View File

@@ -0,0 +1,55 @@
namespace Socialize.Api.Modules.CalendarIntegrations.Data;
#pragma warning disable S1075 // Catalog seed entries intentionally store source URLs.
internal static class CalendarCatalogSeed
{
public static readonly CalendarCatalogEntry[] Entries =
[
new()
{
Id = Guid.Parse("10000000-0000-0000-0000-000000000001"),
Title = "United States Public Holidays",
Description = "Federal public holiday calendar for the United States.",
Country = "US",
Region = null,
Language = "en",
Category = "public-holiday",
CultureOrReligion = null,
ProviderName = "Nager.Date",
SourceUrl = "https://date.nager.at/api/v3/PublicHolidays/2026/US",
TrustLevel = "Verified",
DefaultColor = "#2F80ED",
},
new()
{
Id = Guid.Parse("10000000-0000-0000-0000-000000000002"),
Title = "Canada Public Holidays",
Description = "Public holiday calendar for Canada.",
Country = "CA",
Region = null,
Language = "en",
Category = "public-holiday",
CultureOrReligion = null,
ProviderName = "Nager.Date",
SourceUrl = "https://date.nager.at/api/v3/PublicHolidays/2026/CA",
TrustLevel = "Verified",
DefaultColor = "#2F80ED",
},
new()
{
Id = Guid.Parse("10000000-0000-0000-0000-000000000003"),
Title = "Common Marketing Moments",
Description = "Common retail, awareness, and social planning moments.",
Country = null,
Region = null,
Language = "en",
Category = "marketing-moment",
CultureOrReligion = null,
ProviderName = "Socialize",
SourceUrl = "https://example.com/socialize/marketing-moments.ics",
TrustLevel = "Maintained",
DefaultColor = "#9B51E0",
},
];
}

View File

@@ -0,0 +1,24 @@
namespace Socialize.Api.Modules.CalendarIntegrations.Data;
internal class CalendarEvent
{
public Guid Id { get; init; }
public Guid CalendarSourceId { get; set; }
public required string SourceEventUid { get; set; }
public required string Title { get; set; }
public string? Description { get; set; }
public bool IsAllDay { get; set; }
public bool IsFloatingTime { get; set; }
public DateOnly StartDate { get; set; }
public DateOnly EndDate { get; set; }
public DateTime? StartLocalDateTime { get; set; }
public DateTime? EndLocalDateTime { get; set; }
public DateTimeOffset? StartUtc { get; set; }
public DateTimeOffset? EndUtc { get; set; }
public string? TimeZoneId { get; set; }
public string? RecurrenceId { get; set; }
public string? Location { get; set; }
public string? SourceUrl { get; set; }
public DateTimeOffset? SourceLastModifiedAt { get; set; }
public DateTimeOffset ImportedAt { get; set; }
}

View File

@@ -0,0 +1,22 @@
namespace Socialize.Api.Modules.CalendarIntegrations.Data;
internal class CalendarSource
{
public Guid Id { get; init; }
public required string Scope { get; set; }
public Guid? OrganizationId { get; set; }
public Guid? WorkspaceId { get; set; }
public Guid? UserId { get; set; }
public string? SourceUrl { get; set; }
public string? CatalogSourceReference { get; set; }
public required string DisplayTitle { get; set; }
public required string Color { get; set; }
public required string Category { get; set; }
public bool IsEnabled { get; set; } = true;
public string? InheritanceMode { get; set; }
public DateTimeOffset? LastSuccessfulSyncAt { get; set; }
public DateTimeOffset? LastAttemptedSyncAt { get; set; }
public string? LastSyncError { get; set; }
public DateTimeOffset CreatedAt { get; init; }
public DateTimeOffset UpdatedAt { get; set; }
}

View File

@@ -0,0 +1,95 @@
using Microsoft.EntityFrameworkCore;
namespace Socialize.Api.Modules.CalendarIntegrations.Data;
internal static class CalendarSourceModelConfiguration
{
public static ModelBuilder ConfigureCalendarIntegrationsModule(this ModelBuilder modelBuilder)
{
modelBuilder.Entity<CalendarSource>(source =>
{
source.ToTable("CalendarSources");
source.HasKey(x => x.Id);
source.Property(x => x.Scope).HasMaxLength(32).IsRequired();
source.Property(x => x.SourceUrl).HasMaxLength(2048);
source.Property(x => x.CatalogSourceReference).HasMaxLength(256);
source.Property(x => x.DisplayTitle).HasMaxLength(256).IsRequired();
source.Property(x => x.Color).HasMaxLength(16).IsRequired();
source.Property(x => x.Category).HasMaxLength(64).IsRequired();
source.Property(x => x.InheritanceMode).HasMaxLength(32);
source.Property(x => x.LastSyncError).HasMaxLength(2048);
source.Property(x => x.CreatedAt)
.ValueGeneratedOnAdd()
.HasDefaultValueSql("CURRENT_TIMESTAMP");
source.Property(x => x.UpdatedAt)
.ValueGeneratedOnAdd()
.HasDefaultValueSql("CURRENT_TIMESTAMP");
source.HasIndex(x => x.Scope);
source.HasIndex(x => x.OrganizationId);
source.HasIndex(x => x.WorkspaceId);
source.HasIndex(x => x.UserId);
});
modelBuilder.Entity<CalendarCatalogEntry>(entry =>
{
entry.ToTable("CalendarCatalogEntries");
entry.HasKey(x => x.Id);
entry.Property(x => x.Title).HasMaxLength(256).IsRequired();
entry.Property(x => x.Description).HasMaxLength(1024).IsRequired();
entry.Property(x => x.Country).HasMaxLength(2);
entry.Property(x => x.Region).HasMaxLength(128);
entry.Property(x => x.Language).HasMaxLength(16).IsRequired();
entry.Property(x => x.Category).HasMaxLength(64).IsRequired();
entry.Property(x => x.CultureOrReligion).HasMaxLength(128);
entry.Property(x => x.ProviderName).HasMaxLength(128).IsRequired();
entry.Property(x => x.SourceUrl).HasMaxLength(2048).IsRequired();
entry.Property(x => x.TrustLevel).HasMaxLength(64).IsRequired();
entry.Property(x => x.DefaultColor).HasMaxLength(16).IsRequired();
entry.Property(x => x.CreatedAt)
.ValueGeneratedOnAdd()
.HasDefaultValueSql("CURRENT_TIMESTAMP");
entry.HasIndex(x => x.Country);
entry.HasIndex(x => x.Category);
entry.HasIndex(x => x.ProviderName);
entry.HasData(CalendarCatalogSeed.Entries);
});
modelBuilder.Entity<CalendarEvent>(calendarEvent =>
{
calendarEvent.ToTable("CalendarEvents");
calendarEvent.HasKey(x => x.Id);
calendarEvent.Property(x => x.SourceEventUid).HasMaxLength(512).IsRequired();
calendarEvent.Property(x => x.Title).HasMaxLength(512).IsRequired();
calendarEvent.Property(x => x.Description).HasMaxLength(4000);
calendarEvent.Property(x => x.TimeZoneId).HasMaxLength(128);
calendarEvent.Property(x => x.RecurrenceId).HasMaxLength(512);
calendarEvent.Property(x => x.Location).HasMaxLength(512);
calendarEvent.Property(x => x.SourceUrl).HasMaxLength(2048);
calendarEvent.HasIndex(x => x.CalendarSourceId);
calendarEvent.HasIndex(x => new { x.CalendarSourceId, x.SourceEventUid, x.StartDate }).IsUnique();
calendarEvent.HasOne<CalendarSource>()
.WithMany()
.HasForeignKey(x => x.CalendarSourceId)
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity<UserCalendarExportFeed>(feed =>
{
feed.ToTable("UserCalendarExportFeeds");
feed.HasKey(x => x.Id);
feed.Property(x => x.Token).HasMaxLength(96);
feed.Property(x => x.TokenHash).HasMaxLength(64);
feed.Property(x => x.CreatedAt)
.ValueGeneratedOnAdd()
.HasDefaultValueSql("CURRENT_TIMESTAMP");
feed.Property(x => x.UpdatedAt)
.ValueGeneratedOnAdd()
.HasDefaultValueSql("CURRENT_TIMESTAMP");
feed.HasIndex(x => x.UserId).IsUnique();
feed.HasIndex(x => x.TokenHash).IsUnique();
});
return modelBuilder;
}
}

View File

@@ -0,0 +1,12 @@
namespace Socialize.Api.Modules.CalendarIntegrations.Data;
internal class UserCalendarExportFeed
{
public Guid Id { get; init; }
public Guid UserId { get; set; }
public string? Token { get; set; }
public string? TokenHash { get; set; }
public DateTimeOffset CreatedAt { get; init; }
public DateTimeOffset UpdatedAt { get; set; }
public DateTimeOffset? RevokedAt { get; set; }
}

View File

@@ -0,0 +1,112 @@
using Socialize.Api.Modules.CalendarIntegrations.Data;
using Socialize.Api.Modules.CalendarIntegrations.Services;
namespace Socialize.Api.Modules.CalendarIntegrations.Handlers;
internal record CalendarSourceDto(
Guid Id,
string Scope,
Guid? OrganizationId,
Guid? WorkspaceId,
Guid? UserId,
string? SourceUrl,
string? CatalogSourceReference,
string DisplayTitle,
string Color,
string Category,
bool IsEnabled,
string? InheritanceMode,
bool IsReadOnly,
DateTimeOffset? LastSuccessfulSyncAt,
DateTimeOffset? LastAttemptedSyncAt,
string? LastSyncError,
DateTimeOffset CreatedAt,
DateTimeOffset UpdatedAt)
{
public static CalendarSourceDto FromSource(CalendarSource source, bool isReadOnly)
{
return new CalendarSourceDto(
source.Id,
source.Scope,
source.OrganizationId,
source.WorkspaceId,
source.UserId,
source.SourceUrl,
source.CatalogSourceReference,
source.DisplayTitle,
source.Color,
source.Category,
source.IsEnabled,
source.InheritanceMode,
isReadOnly,
source.LastSuccessfulSyncAt,
source.LastAttemptedSyncAt,
source.LastSyncError,
source.CreatedAt,
source.UpdatedAt);
}
}
internal record UpsertCalendarSourceRequest(
string Scope,
Guid? OrganizationId,
Guid? WorkspaceId,
string? SourceUrl,
string? CatalogSourceReference,
string DisplayTitle,
string Color,
string Category,
bool IsEnabled,
string? InheritanceMode);
internal class UpsertCalendarSourceRequestValidator
: FastEndpoints.Validator<UpsertCalendarSourceRequest>
{
public UpsertCalendarSourceRequestValidator()
{
RuleFor(x => x.Scope)
.NotEmpty()
.Must(CalendarSourceRules.IsSupportedScope)
.WithMessage("A valid calendar source scope should be specified.");
RuleFor(x => x.DisplayTitle).NotEmpty().MaximumLength(256);
RuleFor(x => x.Category).NotEmpty().MaximumLength(64);
RuleFor(x => x.Color)
.NotEmpty()
.Matches("^#[0-9A-Fa-f]{6}$")
.WithMessage("Color should be a six digit hex color, for example #2F80ED.");
RuleFor(x => x.SourceUrl)
.MaximumLength(2048)
.Must(value => string.IsNullOrWhiteSpace(value) || Uri.TryCreate(value, UriKind.Absolute, out Uri? uri) &&
(uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps))
.WithMessage("Source URL should be an absolute HTTP or HTTPS URL.");
RuleFor(x => x.CatalogSourceReference).MaximumLength(256);
RuleFor(x => x)
.Must(x => !string.IsNullOrWhiteSpace(x.SourceUrl) || !string.IsNullOrWhiteSpace(x.CatalogSourceReference))
.WithMessage("A source URL or catalog source reference should be specified.");
RuleFor(x => x)
.Must(x => x.Scope != CalendarSourceScopes.Organization || x.OrganizationId.HasValue)
.WithMessage("Organization calendar sources require an organization id.");
RuleFor(x => x)
.Must(x => x.Scope != CalendarSourceScopes.Workspace || x.WorkspaceId.HasValue)
.WithMessage("Workspace calendar sources require a workspace id.");
RuleFor(x => x)
.Must(x => x.Scope != CalendarSourceScopes.User || (!x.OrganizationId.HasValue && !x.WorkspaceId.HasValue))
.WithMessage("User calendar sources should not include organization or workspace ids.");
RuleFor(x => x)
.Must(x => x.Scope == CalendarSourceScopes.Organization ||
(!x.OrganizationId.HasValue && string.IsNullOrWhiteSpace(x.InheritanceMode)))
.WithMessage("Only organization calendar sources can set organization ids or inheritance modes.");
RuleFor(x => x.InheritanceMode)
.Must(value => string.IsNullOrWhiteSpace(value) || CalendarSourceRules.IsSupportedInheritanceMode(value))
.WithMessage("A valid inheritance mode should be specified.");
}
}

View File

@@ -0,0 +1,132 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using Socialize.Api.Data;
using Socialize.Api.Infrastructure.Security;
using Socialize.Api.Modules.CalendarIntegrations.Data;
using Socialize.Api.Modules.CalendarIntegrations.Services;
using Socialize.Api.Modules.Organizations.Services;
namespace Socialize.Api.Modules.CalendarIntegrations.Handlers;
internal class CreateCalendarSourceHandler(
AppDbContext dbContext,
AccessScopeService accessScopeService,
OrganizationAccessService organizationAccessService)
: Endpoint<UpsertCalendarSourceRequest, CalendarSourceDto>
{
public override void Configure()
{
Post("/api/calendar-integrations/sources");
Options(o => o.WithTags("Calendar Integrations"));
}
public override async Task HandleAsync(UpsertCalendarSourceRequest request, CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(request);
Guid currentUserId = User.GetUserId();
string scope = request.Scope.Trim();
Guid? organizationId = request.OrganizationId;
Guid? workspaceId = request.WorkspaceId;
if (!await CanCreateAsync(scope, organizationId, workspaceId, currentUserId, ct))
{
await SendForbiddenAsync(ct);
return;
}
string? sourceUrl = NormalizeOptional(request.SourceUrl);
string? catalogSourceReference = NormalizeOptional(request.CatalogSourceReference);
if (await SourceAlreadyExistsAsync(scope, organizationId, workspaceId, currentUserId, sourceUrl, catalogSourceReference, ct))
{
AddError(request => request.SourceUrl, "This calendar source has already been added.");
await SendErrorsAsync(cancellation: ct);
return;
}
CalendarSource source = new()
{
Id = Guid.NewGuid(),
Scope = scope,
OrganizationId = scope == CalendarSourceScopes.Organization ? organizationId : null,
WorkspaceId = scope == CalendarSourceScopes.Workspace ? workspaceId : null,
UserId = scope == CalendarSourceScopes.User ? currentUserId : null,
SourceUrl = sourceUrl,
CatalogSourceReference = catalogSourceReference,
DisplayTitle = request.DisplayTitle.Trim(),
Color = request.Color.Trim(),
Category = request.Category.Trim(),
IsEnabled = request.IsEnabled,
InheritanceMode = scope == CalendarSourceScopes.Organization
? NormalizeOptional(request.InheritanceMode) ?? CalendarSourceInheritanceModes.Optional
: null,
UpdatedAt = DateTimeOffset.UtcNow,
};
dbContext.CalendarSources.Add(source);
await dbContext.SaveChangesAsync(ct);
await SendAsync(CalendarSourceDto.FromSource(source, isReadOnly: false), StatusCodes.Status201Created, ct);
}
private async Task<bool> CanCreateAsync(
string scope,
Guid? organizationId,
Guid? workspaceId,
Guid currentUserId,
CancellationToken ct)
{
return scope switch
{
CalendarSourceScopes.Organization when organizationId.HasValue =>
await dbContext.Organizations.AnyAsync(organization => organization.Id == organizationId.Value, ct) &&
await organizationAccessService.HasOrganizationPermissionAsync(
User,
organizationId.Value,
OrganizationPermissions.ManageConnectors,
ct),
CalendarSourceScopes.Workspace when workspaceId.HasValue =>
await dbContext.Workspaces.AnyAsync(workspace => workspace.Id == workspaceId.Value, ct) &&
await accessScopeService.CanManageWorkspaceAsync(User, workspaceId.Value, ct),
CalendarSourceScopes.User => currentUserId != Guid.Empty,
_ => false,
};
}
private Task<bool> SourceAlreadyExistsAsync(
string scope,
Guid? organizationId,
Guid? workspaceId,
Guid currentUserId,
string? sourceUrl,
string? catalogSourceReference,
CancellationToken ct)
{
IQueryable<CalendarSource> query = dbContext.CalendarSources
.Where(source => source.Scope == scope);
query = scope switch
{
CalendarSourceScopes.Organization => query.Where(source => source.OrganizationId == organizationId),
CalendarSourceScopes.Workspace => query.Where(source => source.WorkspaceId == workspaceId),
CalendarSourceScopes.User => query.Where(source => source.UserId == currentUserId),
_ => query.Where(_ => false),
};
string? normalizedUrl = sourceUrl?.Trim();
string? normalizedCatalogReference = catalogSourceReference?.Trim();
return query.AnyAsync(source =>
(!string.IsNullOrWhiteSpace(normalizedCatalogReference) &&
source.CatalogSourceReference == normalizedCatalogReference) ||
(!string.IsNullOrWhiteSpace(normalizedUrl) &&
source.SourceUrl != null &&
EF.Functions.ILike(source.SourceUrl, normalizedUrl)),
ct);
}
private static string? NormalizeOptional(string? value)
{
return string.IsNullOrWhiteSpace(value) ? null : value.Trim();
}
}

View File

@@ -0,0 +1,64 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using Socialize.Api.Data;
using Socialize.Api.Infrastructure.Security;
using Socialize.Api.Modules.CalendarIntegrations.Data;
using Socialize.Api.Modules.CalendarIntegrations.Services;
using Socialize.Api.Modules.Organizations.Services;
namespace Socialize.Api.Modules.CalendarIntegrations.Handlers;
internal class DeleteCalendarSourceHandler(
AppDbContext dbContext,
AccessScopeService accessScopeService,
OrganizationAccessService organizationAccessService)
: EndpointWithoutRequest
{
public override void Configure()
{
Delete("/api/calendar-integrations/sources/{sourceId:guid}");
Options(o => o.WithTags("Calendar Integrations"));
}
public override async Task HandleAsync(CancellationToken ct)
{
Guid sourceId = Route<Guid>("sourceId");
CalendarSource? source = await dbContext.CalendarSources.SingleOrDefaultAsync(candidate => candidate.Id == sourceId, ct);
if (source is null)
{
await SendNotFoundAsync(ct);
return;
}
if (!await CanManageExistingSourceAsync(source, User.GetUserId(), ct))
{
await SendForbiddenAsync(ct);
return;
}
dbContext.CalendarSources.Remove(source);
await dbContext.SaveChangesAsync(ct);
await SendNoContentAsync(ct);
}
private async Task<bool> CanManageExistingSourceAsync(
CalendarSource source,
Guid currentUserId,
CancellationToken ct)
{
return source.Scope switch
{
CalendarSourceScopes.Organization when source.OrganizationId.HasValue =>
await organizationAccessService.HasOrganizationPermissionAsync(
User,
source.OrganizationId.Value,
OrganizationPermissions.ManageConnectors,
ct),
CalendarSourceScopes.Workspace when source.WorkspaceId.HasValue =>
await accessScopeService.CanManageWorkspaceAsync(User, source.WorkspaceId.Value, ct),
CalendarSourceScopes.User => source.UserId == currentUserId,
_ => false,
};
}
}

View File

@@ -0,0 +1,115 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using Socialize.Api.Data;
using Socialize.Api.Modules.CalendarIntegrations.Data;
namespace Socialize.Api.Modules.CalendarIntegrations.Handlers;
internal sealed class ListCalendarCatalogRequest
{
public string? Search { get; set; }
public string? Country { get; set; }
public string? Region { get; set; }
public string? Language { get; set; }
public string? Category { get; set; }
public string? CultureOrReligion { get; set; }
public string? Provider { get; set; }
}
internal record CalendarCatalogEntryDto(
Guid Id,
string Title,
string Description,
string? Country,
string? Region,
string Language,
string Category,
string? CultureOrReligion,
string ProviderName,
string SourceUrl,
string TrustLevel,
string DefaultColor);
internal class ListCalendarCatalogHandler(AppDbContext dbContext)
: Endpoint<ListCalendarCatalogRequest, IReadOnlyCollection<CalendarCatalogEntryDto>>
{
public override void Configure()
{
Get("/api/calendar-integrations/catalog");
Options(o => o.WithTags("Calendar Integrations"));
}
public override async Task HandleAsync(ListCalendarCatalogRequest request, CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(request);
IQueryable<CalendarCatalogEntry> query = dbContext.CalendarCatalogEntries.AsQueryable();
if (!string.IsNullOrWhiteSpace(request.Search))
{
string search = $"%{request.Search.Trim()}%";
query = query.Where(entry =>
EF.Functions.ILike(entry.Title, search) ||
EF.Functions.ILike(entry.Description, search) ||
EF.Functions.ILike(entry.ProviderName, search));
}
if (!string.IsNullOrWhiteSpace(request.Country))
{
string country = request.Country.Trim().ToUpperInvariant();
query = query.Where(entry => entry.Country == country);
}
if (!string.IsNullOrWhiteSpace(request.Region))
{
string region = request.Region.Trim();
query = query.Where(entry => entry.Region == region);
}
if (!string.IsNullOrWhiteSpace(request.Language))
{
string language = request.Language.Trim();
query = query.Where(entry => entry.Language == language);
}
if (!string.IsNullOrWhiteSpace(request.Category))
{
string category = request.Category.Trim();
query = query.Where(entry => entry.Category == category);
}
if (!string.IsNullOrWhiteSpace(request.CultureOrReligion))
{
string cultureOrReligion = request.CultureOrReligion.Trim();
query = query.Where(entry => entry.CultureOrReligion == cultureOrReligion);
}
if (!string.IsNullOrWhiteSpace(request.Provider))
{
string provider = request.Provider.Trim();
query = query.Where(entry => entry.ProviderName == provider);
}
CalendarCatalogEntryDto[] entries = await query
.OrderBy(entry => entry.Country)
.ThenBy(entry => entry.Category)
.ThenBy(entry => entry.Title)
.Take(100)
.Select(entry => new CalendarCatalogEntryDto(
entry.Id,
entry.Title,
entry.Description,
entry.Country,
entry.Region,
entry.Language,
entry.Category,
entry.CultureOrReligion,
entry.ProviderName,
entry.SourceUrl,
entry.TrustLevel,
entry.DefaultColor))
.ToArrayAsync(ct);
await SendOkAsync(entries, ct);
}
}

View File

@@ -0,0 +1,133 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using Socialize.Api.Data;
using Socialize.Api.Infrastructure.Security;
using Socialize.Api.Modules.CalendarIntegrations.Data;
using Socialize.Api.Modules.CalendarIntegrations.Services;
namespace Socialize.Api.Modules.CalendarIntegrations.Handlers;
internal sealed class ListCalendarEventsRequest
{
public Guid? WorkspaceId { get; set; }
public DateOnly? StartDate { get; set; }
public DateOnly? EndDate { get; set; }
}
internal record CalendarEventDto(
Guid Id,
Guid CalendarSourceId,
string SourceEventUid,
string Title,
string? Description,
bool IsAllDay,
bool IsFloatingTime,
DateOnly StartDate,
DateOnly EndDate,
DateTime? StartLocalDateTime,
DateTime? EndLocalDateTime,
DateTimeOffset? StartUtc,
DateTimeOffset? EndUtc,
string? TimeZoneId,
string? RecurrenceId,
string? Location,
string? SourceUrl,
DateTimeOffset? SourceLastModifiedAt,
DateTimeOffset ImportedAt);
internal class ListCalendarEventsHandler(
AppDbContext dbContext,
AccessScopeService accessScopeService)
: Endpoint<ListCalendarEventsRequest, IReadOnlyCollection<CalendarEventDto>>
{
public override void Configure()
{
Get("/api/calendar-integrations/events");
Options(o => o.WithTags("Calendar Integrations"));
}
public override async Task HandleAsync(ListCalendarEventsRequest request, CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(request);
Guid currentUserId = User.GetUserId();
DateOnly startDate = request.StartDate ?? DateOnly.FromDateTime(DateTime.UtcNow.Date.AddMonths(-1));
DateOnly endDate = request.EndDate ?? DateOnly.FromDateTime(DateTime.UtcNow.Date.AddMonths(3));
if (request.WorkspaceId.HasValue &&
!await accessScopeService.CanAccessWorkspaceAsync(User, request.WorkspaceId.Value, ct))
{
await SendForbiddenAsync(ct);
return;
}
IQueryable<CalendarSource> visibleSources = dbContext.CalendarSources
.Where(source => source.IsEnabled);
if (request.WorkspaceId.HasValue)
{
Guid? organizationId = await dbContext.Workspaces
.Where(workspace => workspace.Id == request.WorkspaceId.Value)
.Select(workspace => (Guid?)workspace.OrganizationId)
.SingleOrDefaultAsync(ct);
if (!organizationId.HasValue)
{
await SendNotFoundAsync(ct);
return;
}
visibleSources = visibleSources.Where(source =>
source.Scope == CalendarSourceScopes.Organization && source.OrganizationId == organizationId ||
source.Scope == CalendarSourceScopes.Workspace && source.WorkspaceId == request.WorkspaceId ||
source.Scope == CalendarSourceScopes.User && source.UserId == currentUserId);
}
else
{
IReadOnlyCollection<Guid> workspaceIds = await accessScopeService.GetAccessibleWorkspaceIdsAsync(User, ct);
Guid[] organizationIds = await dbContext.Workspaces
.Where(workspace => workspaceIds.Contains(workspace.Id))
.Select(workspace => workspace.OrganizationId)
.Distinct()
.ToArrayAsync(ct);
visibleSources = visibleSources.Where(source =>
source.Scope == CalendarSourceScopes.Organization && source.OrganizationId.HasValue && organizationIds.Contains(source.OrganizationId.Value) ||
source.Scope == CalendarSourceScopes.Workspace && source.WorkspaceId.HasValue && workspaceIds.Contains(source.WorkspaceId.Value) ||
source.Scope == CalendarSourceScopes.User && source.UserId == currentUserId);
}
Guid[] sourceIds = await visibleSources
.Select(source => source.Id)
.ToArrayAsync(ct);
CalendarEventDto[] events = await dbContext.CalendarEvents
.Where(calendarEvent => sourceIds.Contains(calendarEvent.CalendarSourceId))
.Where(calendarEvent => calendarEvent.StartDate <= endDate && calendarEvent.EndDate >= startDate)
.OrderBy(calendarEvent => calendarEvent.StartDate)
.ThenBy(calendarEvent => calendarEvent.Title)
.Select(calendarEvent => new CalendarEventDto(
calendarEvent.Id,
calendarEvent.CalendarSourceId,
calendarEvent.SourceEventUid,
calendarEvent.Title,
calendarEvent.Description,
calendarEvent.IsAllDay,
calendarEvent.IsFloatingTime,
calendarEvent.StartDate,
calendarEvent.EndDate,
calendarEvent.StartLocalDateTime,
calendarEvent.EndLocalDateTime,
calendarEvent.StartUtc,
calendarEvent.EndUtc,
calendarEvent.TimeZoneId,
calendarEvent.RecurrenceId,
calendarEvent.Location,
calendarEvent.SourceUrl,
calendarEvent.SourceLastModifiedAt,
calendarEvent.ImportedAt))
.ToArrayAsync(ct);
await SendOkAsync(events, ct);
}
}

View File

@@ -0,0 +1,77 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using Socialize.Api.Data;
using Socialize.Api.Infrastructure.Security;
using Socialize.Api.Modules.CalendarIntegrations.Data;
using Socialize.Api.Modules.CalendarIntegrations.Services;
namespace Socialize.Api.Modules.CalendarIntegrations.Handlers;
internal record ListCalendarSourcesRequest(Guid? WorkspaceId);
internal class ListCalendarSourcesHandler(
AppDbContext dbContext,
AccessScopeService accessScopeService)
: Endpoint<ListCalendarSourcesRequest, IReadOnlyCollection<CalendarSourceDto>>
{
public override void Configure()
{
Get("/api/calendar-integrations/sources");
Options(o => o.WithTags("Calendar Integrations"));
}
public override async Task HandleAsync(ListCalendarSourcesRequest request, CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(request);
Guid currentUserId = User.GetUserId();
List<CalendarSource> sources;
if (request.WorkspaceId.HasValue)
{
var workspace = await dbContext.Workspaces
.Where(candidate => candidate.Id == request.WorkspaceId.Value)
.Select(candidate => new { candidate.Id, candidate.OrganizationId })
.SingleOrDefaultAsync(ct);
if (workspace is null)
{
await SendNotFoundAsync(ct);
return;
}
if (!await accessScopeService.CanAccessWorkspaceAsync(User, workspace.Id, ct))
{
await SendForbiddenAsync(ct);
return;
}
sources = await dbContext.CalendarSources
.Where(source =>
source.Scope == CalendarSourceScopes.Organization && source.OrganizationId == workspace.OrganizationId ||
source.Scope == CalendarSourceScopes.Workspace && source.WorkspaceId == workspace.Id ||
source.Scope == CalendarSourceScopes.User && source.UserId == currentUserId)
.OrderBy(source => source.Scope)
.ThenBy(source => source.DisplayTitle)
.ToListAsync(ct);
await SendOkAsync(
sources
.Select(source => CalendarSourceDto.FromSource(
source,
CalendarSourceRules.IsInheritedOrganizationSource(source, workspace.OrganizationId)))
.ToArray(),
ct);
return;
}
sources = await dbContext.CalendarSources
.Where(source => source.Scope == CalendarSourceScopes.User && source.UserId == currentUserId)
.OrderBy(source => source.DisplayTitle)
.ToListAsync(ct);
await SendOkAsync(
sources.Select(source => CalendarSourceDto.FromSource(source, isReadOnly: false)).ToArray(),
ct);
}
}

View File

@@ -0,0 +1,65 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using Socialize.Api.Data;
using Socialize.Api.Infrastructure.Security;
using Socialize.Api.Modules.CalendarIntegrations.Data;
using Socialize.Api.Modules.CalendarIntegrations.Services;
using Socialize.Api.Modules.Organizations.Services;
namespace Socialize.Api.Modules.CalendarIntegrations.Handlers;
internal class RefreshCalendarSourceHandler(
AppDbContext dbContext,
AccessScopeService accessScopeService,
OrganizationAccessService organizationAccessService,
CalendarImportSyncService syncService)
: EndpointWithoutRequest<CalendarSourceDto>
{
public override void Configure()
{
Post("/api/calendar-integrations/sources/{sourceId:guid}/refresh");
Options(o => o.WithTags("Calendar Integrations"));
}
public override async Task HandleAsync(CancellationToken ct)
{
Guid sourceId = Route<Guid>("sourceId");
CalendarSource? source = await dbContext.CalendarSources.SingleOrDefaultAsync(candidate => candidate.Id == sourceId, ct);
if (source is null)
{
await SendNotFoundAsync(ct);
return;
}
if (!await CanManageExistingSourceAsync(source, User.GetUserId(), ct))
{
await SendForbiddenAsync(ct);
return;
}
await syncService.RefreshSourceAsync(source.Id, ct);
await dbContext.Entry(source).ReloadAsync(ct);
await SendOkAsync(CalendarSourceDto.FromSource(source, isReadOnly: false), ct);
}
private async Task<bool> CanManageExistingSourceAsync(
CalendarSource source,
Guid currentUserId,
CancellationToken ct)
{
return source.Scope switch
{
CalendarSourceScopes.Organization when source.OrganizationId.HasValue =>
await organizationAccessService.HasOrganizationPermissionAsync(
User,
source.OrganizationId.Value,
OrganizationPermissions.ManageConnectors,
ct),
CalendarSourceScopes.Workspace when source.WorkspaceId.HasValue =>
await accessScopeService.CanManageWorkspaceAsync(User, source.WorkspaceId.Value, ct),
CalendarSourceScopes.User => source.UserId == currentUserId,
_ => false,
};
}
}

View File

@@ -0,0 +1,91 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using Socialize.Api.Data;
using Socialize.Api.Infrastructure.Security;
using Socialize.Api.Modules.CalendarIntegrations.Data;
using Socialize.Api.Modules.CalendarIntegrations.Services;
using Socialize.Api.Modules.Organizations.Services;
namespace Socialize.Api.Modules.CalendarIntegrations.Handlers;
internal class UpdateCalendarSourceHandler(
AppDbContext dbContext,
AccessScopeService accessScopeService,
OrganizationAccessService organizationAccessService)
: Endpoint<UpsertCalendarSourceRequest, CalendarSourceDto>
{
public override void Configure()
{
Put("/api/calendar-integrations/sources/{sourceId:guid}");
Options(o => o.WithTags("Calendar Integrations"));
}
public override async Task HandleAsync(UpsertCalendarSourceRequest request, CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(request);
Guid sourceId = Route<Guid>("sourceId");
CalendarSource? source = await dbContext.CalendarSources.SingleOrDefaultAsync(candidate => candidate.Id == sourceId, ct);
if (source is null)
{
await SendNotFoundAsync(ct);
return;
}
Guid currentUserId = User.GetUserId();
if (!await CanManageExistingSourceAsync(source, currentUserId, ct))
{
await SendForbiddenAsync(ct);
return;
}
if (source.Scope != request.Scope.Trim() ||
source.OrganizationId != (request.Scope == CalendarSourceScopes.Organization ? request.OrganizationId : null) ||
source.WorkspaceId != (request.Scope == CalendarSourceScopes.Workspace ? request.WorkspaceId : null))
{
AddError("Calendar source scope cannot be changed.");
await SendErrorsAsync(StatusCodes.Status409Conflict, ct);
return;
}
source.SourceUrl = NormalizeOptional(request.SourceUrl);
source.CatalogSourceReference = NormalizeOptional(request.CatalogSourceReference);
source.DisplayTitle = request.DisplayTitle.Trim();
source.Color = request.Color.Trim();
source.Category = request.Category.Trim();
source.IsEnabled = request.IsEnabled;
source.InheritanceMode = source.Scope == CalendarSourceScopes.Organization
? NormalizeOptional(request.InheritanceMode) ?? CalendarSourceInheritanceModes.Optional
: null;
source.UpdatedAt = DateTimeOffset.UtcNow;
await dbContext.SaveChangesAsync(ct);
await SendOkAsync(CalendarSourceDto.FromSource(source, isReadOnly: false), ct);
}
private async Task<bool> CanManageExistingSourceAsync(
CalendarSource source,
Guid currentUserId,
CancellationToken ct)
{
return source.Scope switch
{
CalendarSourceScopes.Organization when source.OrganizationId.HasValue =>
await organizationAccessService.HasOrganizationPermissionAsync(
User,
source.OrganizationId.Value,
OrganizationPermissions.ManageConnectors,
ct),
CalendarSourceScopes.Workspace when source.WorkspaceId.HasValue =>
await accessScopeService.CanManageWorkspaceAsync(User, source.WorkspaceId.Value, ct),
CalendarSourceScopes.User => source.UserId == currentUserId,
_ => false,
};
}
private static string? NormalizeOptional(string? value)
{
return string.IsNullOrWhiteSpace(value) ? null : value.Trim();
}
}

View File

@@ -0,0 +1,224 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using Socialize.Api.Data;
using Socialize.Api.Infrastructure.Security;
using Socialize.Api.Modules.CalendarIntegrations.Data;
using Socialize.Api.Modules.CalendarIntegrations.Services;
using Socialize.Api.Modules.Identity.Data;
namespace Socialize.Api.Modules.CalendarIntegrations.Handlers;
internal record UserCalendarExportFeedDto(
bool IsEnabled,
string? FeedUrl,
DateTimeOffset? CreatedAt,
DateTimeOffset? UpdatedAt,
DateTimeOffset? RevokedAt);
internal class GetUserCalendarExportFeedHandler(AppDbContext dbContext)
: EndpointWithoutRequest<UserCalendarExportFeedDto>
{
public override void Configure()
{
Get("/api/calendar-integrations/export-feed");
Options(o => o.WithTags("Calendar Integrations"));
}
public override async Task HandleAsync(CancellationToken ct)
{
UserCalendarExportFeed? feed = await dbContext.UserCalendarExportFeeds
.SingleOrDefaultAsync(candidate => candidate.UserId == User.GetUserId(), ct);
await SendAsync(UserCalendarExportFeedMapper.ToDto(feed, UserCalendarExportFeedMapper.BuildFeedUrl(feed)), cancellation: ct);
}
}
internal class EnableUserCalendarExportFeedHandler(AppDbContext dbContext)
: EndpointWithoutRequest<UserCalendarExportFeedDto>
{
public override void Configure()
{
Post("/api/calendar-integrations/export-feed/enable");
Options(o => o.WithTags("Calendar Integrations"));
}
public override async Task HandleAsync(CancellationToken ct)
{
Guid userId = User.GetUserId();
string token = CalendarExportFeedTokenService.GenerateToken();
string tokenHash = CalendarExportFeedTokenService.HashToken(token);
DateTimeOffset now = DateTimeOffset.UtcNow;
UserCalendarExportFeed? feed = await dbContext.UserCalendarExportFeeds
.SingleOrDefaultAsync(candidate => candidate.UserId == userId, ct);
if (feed is null)
{
feed = new UserCalendarExportFeed
{
Id = Guid.NewGuid(),
UserId = userId,
Token = token,
TokenHash = tokenHash,
UpdatedAt = now,
};
dbContext.UserCalendarExportFeeds.Add(feed);
}
else if (feed.TokenHash is null || feed.RevokedAt.HasValue)
{
feed.Token = token;
feed.TokenHash = tokenHash;
feed.RevokedAt = null;
feed.UpdatedAt = now;
}
else
{
token = string.Empty;
}
await dbContext.SaveChangesAsync(ct);
await SendAsync(UserCalendarExportFeedMapper.ToDto(feed, UserCalendarExportFeedMapper.BuildFeedUrl(feed, token)), cancellation: ct);
}
}
internal class RegenerateUserCalendarExportFeedHandler(AppDbContext dbContext)
: EndpointWithoutRequest<UserCalendarExportFeedDto>
{
public override void Configure()
{
Post("/api/calendar-integrations/export-feed/regenerate");
Options(o => o.WithTags("Calendar Integrations"));
}
public override async Task HandleAsync(CancellationToken ct)
{
Guid userId = User.GetUserId();
string token = CalendarExportFeedTokenService.GenerateToken();
DateTimeOffset now = DateTimeOffset.UtcNow;
UserCalendarExportFeed? feed = await dbContext.UserCalendarExportFeeds
.SingleOrDefaultAsync(candidate => candidate.UserId == userId, ct);
if (feed is null)
{
feed = new UserCalendarExportFeed
{
Id = Guid.NewGuid(),
UserId = userId,
UpdatedAt = now,
};
dbContext.UserCalendarExportFeeds.Add(feed);
}
feed.TokenHash = CalendarExportFeedTokenService.HashToken(token);
feed.Token = token;
feed.RevokedAt = null;
feed.UpdatedAt = now;
await dbContext.SaveChangesAsync(ct);
await SendAsync(UserCalendarExportFeedMapper.ToDto(feed, UserCalendarExportFeedMapper.BuildFeedUrl(feed, token)), cancellation: ct);
}
}
internal class RevokeUserCalendarExportFeedHandler(AppDbContext dbContext)
: EndpointWithoutRequest<UserCalendarExportFeedDto>
{
public override void Configure()
{
Delete("/api/calendar-integrations/export-feed");
Options(o => o.WithTags("Calendar Integrations"));
}
public override async Task HandleAsync(CancellationToken ct)
{
UserCalendarExportFeed? feed = await dbContext.UserCalendarExportFeeds
.SingleOrDefaultAsync(candidate => candidate.UserId == User.GetUserId(), ct);
if (feed is not null)
{
feed.TokenHash = null;
feed.Token = null;
feed.RevokedAt = DateTimeOffset.UtcNow;
feed.UpdatedAt = feed.RevokedAt.Value;
await dbContext.SaveChangesAsync(ct);
}
await SendAsync(UserCalendarExportFeedMapper.ToDto(feed, null), cancellation: ct);
}
}
internal class GetUserCalendarExportFeedIcsHandler(
AppDbContext dbContext,
CalendarExportFeedService feedService)
: EndpointWithoutRequest
{
public override void Configure()
{
AllowAnonymous();
Get("/api/calendar-integrations/export-feed/{token}.ics");
Options(o => o.WithTags("Calendar Integrations"));
}
public override async Task HandleAsync(CancellationToken ct)
{
string? token = Route<string?>("token");
if (string.IsNullOrWhiteSpace(token))
{
await SendNotFoundAsync(ct);
return;
}
string tokenHash = CalendarExportFeedTokenService.HashToken(token);
UserCalendarExportFeed? feed = await dbContext.UserCalendarExportFeeds
.SingleOrDefaultAsync(candidate =>
candidate.TokenHash == tokenHash &&
!candidate.RevokedAt.HasValue,
ct);
if (feed is null)
{
await SendNotFoundAsync(ct);
return;
}
User? user = await dbContext.Users.SingleOrDefaultAsync(candidate => candidate.Id == feed.UserId, ct);
if (user is null)
{
await SendNotFoundAsync(ct);
return;
}
string appBaseUrl = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host}";
string ics = await feedService.BuildUserFeedAsync(feed.UserId, user.Email, appBaseUrl, ct);
HttpContext.Response.ContentType = "text/calendar; charset=utf-8";
await HttpContext.Response.WriteAsync(ics, ct);
}
}
file static class UserCalendarExportFeedMapper
{
public static UserCalendarExportFeedDto ToDto(UserCalendarExportFeed? feed, string? feedUrl)
{
return new UserCalendarExportFeedDto(
feed?.TokenHash is not null && !feed.RevokedAt.HasValue,
feedUrl,
feed?.CreatedAt,
feed?.UpdatedAt,
feed?.RevokedAt);
}
public static string? BuildFeedUrl(UserCalendarExportFeed? feed, string? token = null)
{
if (feed?.TokenHash is null || feed.RevokedAt.HasValue)
{
return null;
}
string effectiveToken = string.IsNullOrWhiteSpace(token) ? feed.Token ?? string.Empty : token;
return string.IsNullOrWhiteSpace(effectiveToken)
? null
: $"/api/calendar-integrations/export-feed/{effectiveToken}.ics";
}
}

View File

@@ -0,0 +1,13 @@
namespace Socialize.Api.Modules.CalendarIntegrations;
internal static class ModuleRegistration
{
public static WebApplicationBuilder AddCalendarIntegrationsModule(this WebApplicationBuilder builder)
{
builder.Services.AddScoped<Services.CalendarExportFeedService>();
builder.Services.AddScoped<Services.CalendarImportSyncService>();
builder.Services.AddHostedService<Services.CalendarImportBackgroundService>();
return builder;
}
}

View File

@@ -0,0 +1,86 @@
using System.Text;
using System.Globalization;
namespace Socialize.Api.Modules.CalendarIntegrations.Services;
internal sealed record CalendarExportFeedEvent(
string Uid,
string Title,
DateTimeOffset StartsAt,
DateTimeOffset EndsAt,
bool IsAllDay,
string? Description,
string? Url);
internal static class CalendarExportFeedBuilder
{
public static string Build(string calendarName, IReadOnlyCollection<CalendarExportFeedEvent> events)
{
StringBuilder builder = new();
builder.AppendLine("BEGIN:VCALENDAR");
builder.AppendLine("VERSION:2.0");
builder.AppendLine("PRODID:-//Socialize//User Work Calendar//EN");
builder.AppendLine("CALSCALE:GREGORIAN");
builder.AppendLine("METHOD:PUBLISH");
AppendLineInvariant(builder, $"X-WR-CALNAME:{EscapeText(calendarName)}");
foreach (CalendarExportFeedEvent feedEvent in events.OrderBy(calendarEvent => calendarEvent.StartsAt))
{
builder.AppendLine("BEGIN:VEVENT");
AppendLineInvariant(builder, $"UID:{EscapeText(feedEvent.Uid)}");
AppendLineInvariant(builder, $"DTSTAMP:{FormatUtc(DateTimeOffset.UtcNow)}");
AppendLineInvariant(builder, $"SUMMARY:{EscapeText(feedEvent.Title)}");
if (feedEvent.IsAllDay)
{
AppendLineInvariant(builder, $"DTSTART;VALUE=DATE:{FormatDate(feedEvent.StartsAt)}");
AppendLineInvariant(builder, $"DTEND;VALUE=DATE:{FormatDate(feedEvent.EndsAt)}");
}
else
{
AppendLineInvariant(builder, $"DTSTART:{FormatUtc(feedEvent.StartsAt)}");
AppendLineInvariant(builder, $"DTEND:{FormatUtc(feedEvent.EndsAt)}");
}
if (!string.IsNullOrWhiteSpace(feedEvent.Description))
{
AppendLineInvariant(builder, $"DESCRIPTION:{EscapeText(feedEvent.Description)}");
}
if (!string.IsNullOrWhiteSpace(feedEvent.Url))
{
AppendLineInvariant(builder, $"URL:{EscapeText(feedEvent.Url)}");
}
builder.AppendLine("END:VEVENT");
}
builder.AppendLine("END:VCALENDAR");
return builder.ToString();
}
private static string FormatDate(DateTimeOffset value)
{
return value.ToString("yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);
}
private static string FormatUtc(DateTimeOffset value)
{
return value.UtcDateTime.ToString("yyyyMMdd'T'HHmmss'Z'", System.Globalization.CultureInfo.InvariantCulture);
}
private static string EscapeText(string value)
{
return value
.Replace("\\", "\\\\", StringComparison.Ordinal)
.Replace("\r\n", "\\n", StringComparison.Ordinal)
.Replace("\n", "\\n", StringComparison.Ordinal)
.Replace(";", "\\;", StringComparison.Ordinal)
.Replace(",", "\\,", StringComparison.Ordinal);
}
private static void AppendLineInvariant(StringBuilder builder, FormattableString value)
{
builder.AppendLine(value.ToString(CultureInfo.InvariantCulture));
}
}

View File

@@ -0,0 +1,173 @@
using Microsoft.EntityFrameworkCore;
using Socialize.Api.Data;
namespace Socialize.Api.Modules.CalendarIntegrations.Services;
internal class CalendarExportFeedService(AppDbContext dbContext)
{
public async Task<string> BuildUserFeedAsync(Guid userId, string? userEmail, string appBaseUrl, CancellationToken ct)
{
string normalizedEmail = userEmail?.Trim() ?? string.Empty;
Guid[] workspaceIds = await dbContext.Workspaces
.Where(workspace =>
workspace.OwnerUserId == userId ||
dbContext.OrganizationMemberships.Any(membership =>
membership.OrganizationId == workspace.OrganizationId &&
membership.UserId == userId))
.Select(workspace => workspace.Id)
.ToArrayAsync(ct);
List<CalendarExportFeedEvent> events = [];
events.AddRange(await dbContext.ContentItems
.Where(item => workspaceIds.Contains(item.WorkspaceId) && item.DueDate.HasValue)
.Join(
dbContext.Workspaces,
item => item.WorkspaceId,
workspace => workspace.Id,
(item, workspace) => new { item, workspace })
.Join(
dbContext.Clients,
itemWorkspace => itemWorkspace.item.ClientId,
client => client.Id,
(itemWorkspace, client) => new { itemWorkspace.item, itemWorkspace.workspace, client })
.Join(
dbContext.Campaigns,
itemWorkspaceClient => itemWorkspaceClient.item.CampaignId,
campaign => campaign.Id,
(itemWorkspaceClient, campaign) => new { itemWorkspaceClient.item, itemWorkspaceClient.workspace, itemWorkspaceClient.client, campaign })
.Select(candidate => ToContentFeedEvent(
candidate.item.Id,
candidate.item.Title,
candidate.item.Status,
candidate.item.DueDate!.Value,
candidate.workspace.Name,
candidate.client.Name,
candidate.campaign.Name,
appBaseUrl))
.ToListAsync(ct));
events.AddRange(await dbContext.ApprovalRequests
.Where(approval =>
approval.DueAt.HasValue &&
(approval.RequestedByUserId == userId ||
(!string.IsNullOrEmpty(normalizedEmail) && EF.Functions.ILike(approval.ReviewerEmail, normalizedEmail))))
.Join(
dbContext.ContentItems,
approval => approval.ContentItemId,
item => item.Id,
(approval, item) => new { approval, item })
.Where(candidate => workspaceIds.Contains(candidate.approval.WorkspaceId))
.Join(
dbContext.Workspaces,
approvalItem => approvalItem.approval.WorkspaceId,
workspace => workspace.Id,
(approvalItem, workspace) => new { approvalItem.approval, approvalItem.item, workspace })
.Select(candidate => ToApprovalFeedEvent(
candidate.approval.Id,
candidate.item.Id,
candidate.item.Title,
candidate.approval.Stage,
candidate.approval.State,
candidate.approval.DueAt!.Value,
candidate.workspace.Name,
appBaseUrl))
.ToListAsync(ct));
events.AddRange(await dbContext.Campaigns
.Where(campaign => workspaceIds.Contains(campaign.WorkspaceId))
.Join(
dbContext.Workspaces,
campaign => campaign.WorkspaceId,
workspace => workspace.Id,
(campaign, workspace) => new { campaign, workspace })
.Select(candidate => ToCampaignFeedEvent(
candidate.campaign.Id,
candidate.campaign.Name,
candidate.campaign.Status,
candidate.campaign.StartDate,
candidate.campaign.EndDate,
candidate.workspace.Name,
appBaseUrl))
.ToListAsync(ct));
return CalendarExportFeedBuilder.Build("Socialize my work", events);
}
private static CalendarExportFeedEvent ToContentFeedEvent(
Guid contentItemId,
string title,
string status,
DateTimeOffset dueDate,
string workspaceName,
string clientName,
string campaignName,
string appBaseUrl)
{
(DateTimeOffset start, DateTimeOffset end, bool isAllDay) = NormalizeEventTime(dueDate);
return new CalendarExportFeedEvent(
$"content-{contentItemId}@socialize",
title,
start,
end,
isAllDay,
$"Status: {status}\nWorkspace: {workspaceName}\nClient: {clientName}\nCampaign: {campaignName}",
$"{appBaseUrl.TrimEnd('/')}/app/content/{contentItemId}");
}
private static CalendarExportFeedEvent ToApprovalFeedEvent(
Guid approvalId,
Guid contentItemId,
string contentTitle,
string stage,
string state,
DateTimeOffset dueAt,
string workspaceName,
string appBaseUrl)
{
(DateTimeOffset start, DateTimeOffset end, bool isAllDay) = NormalizeEventTime(dueAt);
return new CalendarExportFeedEvent(
$"approval-{approvalId}@socialize",
$"Approval due: {contentTitle}",
start,
end,
isAllDay,
$"Stage: {stage}\nState: {state}\nWorkspace: {workspaceName}",
$"{appBaseUrl.TrimEnd('/')}/app/content/{contentItemId}");
}
private static CalendarExportFeedEvent ToCampaignFeedEvent(
Guid campaignId,
string name,
string status,
DateTimeOffset startDate,
DateTimeOffset endDate,
string workspaceName,
string appBaseUrl)
{
DateTimeOffset start = new(startDate.Date, startDate.Offset);
DateTimeOffset end = new(endDate.Date.AddDays(1), endDate.Offset);
return new CalendarExportFeedEvent(
$"campaign-{campaignId}@socialize",
$"Campaign: {name}",
start,
end <= start ? start.AddDays(1) : end,
true,
$"Status: {status}\nWorkspace: {workspaceName}",
$"{appBaseUrl.TrimEnd('/')}/app/campaigns/{campaignId}");
}
private static (DateTimeOffset Start, DateTimeOffset End, bool IsAllDay) NormalizeEventTime(DateTimeOffset value)
{
if (value.TimeOfDay == TimeSpan.Zero)
{
DateTimeOffset start = new(value.Date, value.Offset);
return (start, start.AddDays(1), true);
}
return (value, value.AddMinutes(30), false);
}
}

View File

@@ -0,0 +1,27 @@
using System.Security.Cryptography;
namespace Socialize.Api.Modules.CalendarIntegrations.Services;
internal static class CalendarExportFeedTokenService
{
public static string GenerateToken()
{
Span<byte> bytes = stackalloc byte[32];
RandomNumberGenerator.Fill(bytes);
return Base64UrlEncode(bytes);
}
public static string HashToken(string token)
{
byte[] bytes = SHA256.HashData(System.Text.Encoding.UTF8.GetBytes(token));
return Convert.ToHexString(bytes);
}
private static string Base64UrlEncode(ReadOnlySpan<byte> bytes)
{
return Convert.ToBase64String(bytes)
.TrimEnd('=')
.Replace('+', '-')
.Replace('/', '_');
}
}

View File

@@ -0,0 +1,37 @@
namespace Socialize.Api.Modules.CalendarIntegrations.Services;
internal sealed class CalendarImportBackgroundService(
IServiceScopeFactory scopeFactory,
ILogger<CalendarImportBackgroundService> logger)
: BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
using PeriodicTimer timer = new(TimeSpan.FromHours(6));
while (!stoppingToken.IsCancellationRequested)
{
await RefreshDueSourcesAsync(stoppingToken);
await timer.WaitForNextTickAsync(stoppingToken);
}
}
private async Task RefreshDueSourcesAsync(CancellationToken stoppingToken)
{
try
{
using IServiceScope scope = scopeFactory.CreateScope();
CalendarImportSyncService syncService = scope.ServiceProvider.GetRequiredService<CalendarImportSyncService>();
await syncService.RefreshDueSourcesAsync(stoppingToken);
}
catch (OperationCanceledException ex) when (stoppingToken.IsCancellationRequested)
{
logger.LogDebug(ex, "Calendar import background sync stopped.");
}
#pragma warning disable CA1031 // Background service should log and continue after unexpected sync failures.
catch (Exception ex)
{
logger.LogError(ex, "Calendar import background sync failed.");
}
#pragma warning restore CA1031
}
}

View File

@@ -0,0 +1,315 @@
using Microsoft.EntityFrameworkCore;
using Socialize.Api.Data;
using Socialize.Api.Modules.CalendarIntegrations.Data;
using System.Globalization;
using System.Text.Json;
namespace Socialize.Api.Modules.CalendarIntegrations.Services;
#pragma warning disable S1075 // Supplemental observance identifiers intentionally use stable URI-like values.
internal sealed class CalendarImportSyncService(
AppDbContext dbContext,
IHttpClientFactory httpClientFactory)
{
private static readonly JsonSerializerOptions JsonSerializerOptions = new(JsonSerializerDefaults.Web);
public async Task RefreshSourceAsync(Guid sourceId, CancellationToken ct)
{
CalendarSource? source = await dbContext.CalendarSources
.SingleOrDefaultAsync(candidate => candidate.Id == sourceId, ct);
if (source is null)
{
throw new InvalidOperationException("Calendar source was not found.");
}
source.LastAttemptedSyncAt = DateTimeOffset.UtcNow;
if (string.IsNullOrWhiteSpace(source.SourceUrl))
{
source.LastSyncError = "Calendar source does not have a source URL.";
await dbContext.SaveChangesAsync(ct);
return;
}
try
{
using HttpClient httpClient = httpClientFactory.CreateClient();
DateOnly rangeStart = DateOnly.FromDateTime(DateTime.UtcNow.AddYears(-1));
DateOnly rangeEnd = DateOnly.FromDateTime(DateTime.UtcNow.AddYears(2));
IReadOnlyCollection<ParsedCalendarEvent> parsedEvents = await GetParsedEventsAsync(
httpClient,
source.SourceUrl,
rangeStart,
rangeEnd,
ct);
await ReplaceEventsAsync(source.Id, parsedEvents, ct);
source.LastSuccessfulSyncAt = DateTimeOffset.UtcNow;
source.LastSyncError = null;
source.LastAttemptedSyncAt = source.LastSuccessfulSyncAt;
await dbContext.SaveChangesAsync(ct);
}
catch (HttpRequestException ex)
{
await RecordSyncFailureAsync(source, ex.Message, ct);
}
catch (FormatException ex)
{
await RecordSyncFailureAsync(source, ex.Message, ct);
}
catch (InvalidOperationException ex)
{
await RecordSyncFailureAsync(source, ex.Message, ct);
}
}
public async Task RefreshDueSourcesAsync(CancellationToken ct)
{
DateTimeOffset staleBefore = DateTimeOffset.UtcNow.AddHours(-12);
Guid[] sourceIds = await dbContext.CalendarSources
.Where(source => source.IsEnabled && source.SourceUrl != null)
.Where(source => source.LastAttemptedSyncAt == null || source.LastAttemptedSyncAt < staleBefore)
.OrderBy(source => source.LastAttemptedSyncAt)
.Select(source => source.Id)
.Take(25)
.ToArrayAsync(ct);
foreach (Guid sourceId in sourceIds)
{
await RefreshSourceAsync(sourceId, ct);
}
}
private async Task ReplaceEventsAsync(
Guid sourceId,
IReadOnlyCollection<ParsedCalendarEvent> parsedEvents,
CancellationToken ct)
{
await dbContext.CalendarEvents
.Where(calendarEvent => calendarEvent.CalendarSourceId == sourceId)
.ExecuteDeleteAsync(ct);
DateTimeOffset importedAt = DateTimeOffset.UtcNow;
foreach (ParsedCalendarEvent parsedEvent in parsedEvents)
{
dbContext.CalendarEvents.Add(new CalendarEvent
{
Id = Guid.NewGuid(),
CalendarSourceId = sourceId,
SourceEventUid = parsedEvent.SourceEventUid,
Title = parsedEvent.Title,
Description = parsedEvent.Description,
IsAllDay = parsedEvent.IsAllDay,
IsFloatingTime = parsedEvent.IsFloatingTime,
StartDate = parsedEvent.StartDate,
EndDate = parsedEvent.EndDate,
StartLocalDateTime = parsedEvent.StartLocalDateTime,
EndLocalDateTime = parsedEvent.EndLocalDateTime,
StartUtc = parsedEvent.StartUtc,
EndUtc = parsedEvent.EndUtc,
TimeZoneId = parsedEvent.TimeZoneId,
RecurrenceId = parsedEvent.RecurrenceId,
Location = parsedEvent.Location,
SourceUrl = parsedEvent.SourceUrl,
SourceLastModifiedAt = parsedEvent.SourceLastModifiedAt,
ImportedAt = importedAt,
});
}
}
private static async Task<IReadOnlyCollection<ParsedCalendarEvent>> GetParsedEventsAsync(
HttpClient httpClient,
string sourceUrl,
DateOnly rangeStart,
DateOnly rangeEnd,
CancellationToken ct)
{
if (TryGetNagerCountryCode(sourceUrl, out string? countryCode))
{
return await GetNagerEventsAsync(httpClient, sourceUrl, countryCode!, rangeStart, rangeEnd, ct);
}
string content = await httpClient.GetStringAsync(new Uri(sourceUrl), ct);
return IcsCalendarParser.Parse(content, rangeStart, rangeEnd);
}
private static async Task<IReadOnlyCollection<ParsedCalendarEvent>> GetNagerEventsAsync(
HttpClient httpClient,
string sourceUrl,
string countryCode,
DateOnly rangeStart,
DateOnly rangeEnd,
CancellationToken ct)
{
List<ParsedCalendarEvent> events = [];
for (int year = rangeStart.Year; year <= rangeEnd.Year; year++)
{
string yearUrl = BuildNagerYearUrl(sourceUrl, countryCode, year);
string json = await httpClient.GetStringAsync(new Uri(yearUrl), ct);
NagerHoliday[] holidays = JsonSerializer.Deserialize<NagerHoliday[]>(json, JsonSerializerOptions) ?? [];
foreach (NagerHoliday holiday in holidays)
{
if (!DateOnly.TryParse(holiday.Date, CultureInfo.InvariantCulture, out DateOnly date) ||
date < rangeStart ||
date > rangeEnd)
{
continue;
}
events.Add(ToParsedEvent(
$"nager-{countryCode}-{date:yyyyMMdd}-{NormalizeUidPart(holiday.Name)}",
string.IsNullOrWhiteSpace(holiday.Name) ? holiday.LocalName : holiday.Name,
holiday.LocalName,
date,
string.Join(", ", holiday.Types ?? []),
yearUrl));
}
events.AddRange(GetSupplementalCountryEvents(countryCode, year, rangeStart, rangeEnd));
}
return events
.GroupBy(calendarEvent => calendarEvent.SourceEventUid)
.Select(group => group.First())
.OrderBy(calendarEvent => calendarEvent.StartDate)
.ThenBy(calendarEvent => calendarEvent.Title)
.ToArray();
}
private static ParsedCalendarEvent ToParsedEvent(
string uid,
string? title,
string? localName,
DateOnly date,
string? types,
string sourceUrl)
{
string? description = string.IsNullOrWhiteSpace(types)
? localName
: $"{localName}\nTypes: {types}";
return new ParsedCalendarEvent(
uid,
string.IsNullOrWhiteSpace(title) ? "Untitled event" : title,
description,
IsAllDay: true,
IsFloatingTime: false,
date,
date.AddDays(1),
StartLocalDateTime: null,
EndLocalDateTime: null,
StartUtc: null,
EndUtc: null,
TimeZoneId: null,
RecurrenceId: null,
Location: null,
sourceUrl,
SourceLastModifiedAt: null);
}
private static IReadOnlyCollection<ParsedCalendarEvent> GetSupplementalCountryEvents(
string countryCode,
int year,
DateOnly rangeStart,
DateOnly rangeEnd)
{
if (!countryCode.Equals("CA", StringComparison.OrdinalIgnoreCase))
{
return [];
}
DateOnly mothersDay = NthWeekdayOfMonth(year, month: 5, DayOfWeek.Sunday, occurrence: 2);
if (mothersDay < rangeStart || mothersDay > rangeEnd)
{
return [];
}
return
[
ToParsedEvent(
$"socialize-ca-mothers-day-{year}",
"Mother's Day",
"Mother's Day",
mothersDay,
"Observance",
"socialize://calendar-observances/CA"),
];
}
private static DateOnly NthWeekdayOfMonth(int year, int month, DayOfWeek dayOfWeek, int occurrence)
{
DateOnly date = new(year, month, 1);
while (date.DayOfWeek != dayOfWeek)
{
date = date.AddDays(1);
}
return date.AddDays((occurrence - 1) * 7);
}
private static bool TryGetNagerCountryCode(string sourceUrl, out string? countryCode)
{
countryCode = null;
if (!Uri.TryCreate(sourceUrl, UriKind.Absolute, out Uri? uri) ||
!uri.Host.Contains("date.nager.at", StringComparison.OrdinalIgnoreCase))
{
return false;
}
string[] segments = uri.AbsolutePath
.Split('/', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
string? candidate = segments.LastOrDefault(segment => segment.Length == 2);
if (candidate is null)
{
return false;
}
countryCode = candidate.ToUpperInvariant();
return true;
}
private static string BuildNagerYearUrl(string sourceUrl, string countryCode, int year)
{
if (Uri.TryCreate(sourceUrl, UriKind.Absolute, out Uri? uri))
{
return $"{uri.Scheme}://{uri.Host}/api/v3/PublicHolidays/{year}/{countryCode}";
}
return $"https://date.nager.at/api/v3/PublicHolidays/{year}/{countryCode}";
}
private static string NormalizeUidPart(string? value)
{
return new string((value ?? "holiday")
.ToUpperInvariant()
.Select(character => char.IsLetterOrDigit(character) ? character : '-')
.ToArray())
.Trim('-');
}
private async Task RecordSyncFailureAsync(
CalendarSource source,
string message,
CancellationToken ct)
{
source.LastSyncError = NormalizeSyncError(message);
await dbContext.SaveChangesAsync(ct);
}
public static string NormalizeSyncError(string message)
{
ArgumentNullException.ThrowIfNull(message);
return message.Length > 2048 ? message[..2048] : message;
}
private sealed record NagerHoliday(
string Date,
string LocalName,
string Name,
string[]? Types);
}

View File

@@ -0,0 +1,14 @@
namespace Socialize.Api.Modules.CalendarIntegrations.Services;
internal static class CalendarSourceScopes
{
public const string Organization = "Organization";
public const string Workspace = "Workspace";
public const string User = "User";
}
internal static class CalendarSourceInheritanceModes
{
public const string Required = "Required";
public const string Optional = "Optional";
}

View File

@@ -0,0 +1,51 @@
using Socialize.Api.Modules.CalendarIntegrations.Data;
namespace Socialize.Api.Modules.CalendarIntegrations.Services;
internal static class CalendarSourceRules
{
public static readonly string[] SupportedScopes =
[
CalendarSourceScopes.Organization,
CalendarSourceScopes.Workspace,
CalendarSourceScopes.User,
];
public static readonly string[] SupportedInheritanceModes =
[
CalendarSourceInheritanceModes.Required,
CalendarSourceInheritanceModes.Optional,
];
public static bool IsSupportedScope(string? scope)
{
return SupportedScopes.Contains(scope?.Trim(), StringComparer.Ordinal);
}
public static bool IsSupportedInheritanceMode(string? inheritanceMode)
{
return SupportedInheritanceModes.Contains(inheritanceMode?.Trim(), StringComparer.Ordinal);
}
public static bool IsInheritedOrganizationSource(CalendarSource source, Guid workspaceOrganizationId)
{
return source.Scope == CalendarSourceScopes.Organization &&
source.OrganizationId == workspaceOrganizationId;
}
public static bool CanManageScope(
string scope,
bool canManageOrganizationCalendars,
bool canManageWorkspaceCalendars,
Guid currentUserId,
Guid? sourceUserId)
{
return scope switch
{
CalendarSourceScopes.Organization => canManageOrganizationCalendars,
CalendarSourceScopes.Workspace => canManageWorkspaceCalendars,
CalendarSourceScopes.User => sourceUserId == currentUserId,
_ => false,
};
}
}

View File

@@ -0,0 +1,420 @@
using System.Globalization;
using System.Text;
namespace Socialize.Api.Modules.CalendarIntegrations.Services;
internal record ParsedCalendarEvent(
string SourceEventUid,
string Title,
string? Description,
bool IsAllDay,
bool IsFloatingTime,
DateOnly StartDate,
DateOnly EndDate,
DateTime? StartLocalDateTime,
DateTime? EndLocalDateTime,
DateTimeOffset? StartUtc,
DateTimeOffset? EndUtc,
string? TimeZoneId,
string? RecurrenceId,
string? Location,
string? SourceUrl,
DateTimeOffset? SourceLastModifiedAt);
internal record IcsDateTimeValue(
bool IsAllDay,
bool IsFloatingTime,
DateOnly Date,
DateTime? LocalDateTime,
DateTimeOffset? UtcDateTime,
string? TimeZoneId);
internal sealed record IcsRawEvent(
string Uid,
string Title,
string? Description,
IcsDateTimeValue Start,
IcsDateTimeValue? End,
string? RRule,
string? Location,
string? SourceUrl,
DateTimeOffset? LastModifiedAt);
internal static class IcsCalendarParser
{
public static IReadOnlyCollection<ParsedCalendarEvent> Parse(
string content,
DateOnly rangeStart,
DateOnly rangeEnd)
{
ArgumentNullException.ThrowIfNull(content);
List<ParsedCalendarEvent> events = [];
foreach (IcsRawEvent rawEvent in ReadRawEvents(content))
{
events.AddRange(Expand(rawEvent, rangeStart, rangeEnd));
}
return events
.OrderBy(calendarEvent => calendarEvent.StartDate)
.ThenBy(calendarEvent => calendarEvent.Title)
.ToArray();
}
private static IEnumerable<IcsRawEvent> ReadRawEvents(string content)
{
List<string> lines = UnfoldLines(content).ToList();
int index = 0;
while (index < lines.Count)
{
if (!lines[index].Equals("BEGIN:VEVENT", StringComparison.OrdinalIgnoreCase))
{
index++;
continue;
}
Dictionary<string, List<(Dictionary<string, string> Parameters, string Value)>> properties =
new(StringComparer.OrdinalIgnoreCase);
index++;
while (index < lines.Count && !lines[index].Equals("END:VEVENT", StringComparison.OrdinalIgnoreCase))
{
ParseProperty(lines[index], properties);
index++;
}
if (!TryGetFirst(properties, "DTSTART", out var startProperty))
{
continue;
}
IcsDateTimeValue start = ParseDateTimeValue(startProperty.Value, startProperty.Parameters);
IcsDateTimeValue? end = TryGetFirst(properties, "DTEND", out var endProperty)
? ParseDateTimeValue(endProperty.Value, endProperty.Parameters)
: null;
string uid = TryGetFirst(properties, "UID", out var uidProperty)
? uidProperty.Value
: $"{start.Date:yyyyMMdd}:{GetText(properties, "SUMMARY") ?? "calendar-event"}";
yield return new IcsRawEvent(
uid,
GetText(properties, "SUMMARY") ?? "Untitled event",
GetText(properties, "DESCRIPTION"),
start,
end,
GetText(properties, "RRULE"),
GetText(properties, "LOCATION"),
GetText(properties, "URL"),
TryGetFirst(properties, "LAST-MODIFIED", out var lastModified)
? ParseDateTimeValue(lastModified.Value, lastModified.Parameters).UtcDateTime
: null);
index++;
}
}
private static IEnumerable<string> UnfoldLines(string content)
{
StringBuilder? current = null;
using StringReader reader = new(content.Replace("\r\n", "\n", StringComparison.Ordinal).Replace('\r', '\n'));
while (reader.ReadLine() is { } line)
{
if ((line.StartsWith(' ') || line.StartsWith('\t')) && current is not null)
{
current.Append(line[1..]);
continue;
}
if (current is not null)
{
yield return current.ToString();
}
current = new StringBuilder(line);
}
if (current is not null)
{
yield return current.ToString();
}
}
private static void ParseProperty(
string line,
Dictionary<string, List<(Dictionary<string, string> Parameters, string Value)>> properties)
{
int separatorIndex = line.IndexOf(':', StringComparison.Ordinal);
if (separatorIndex < 0)
{
return;
}
string nameAndParameters = line[..separatorIndex];
string value = UnescapeText(line[(separatorIndex + 1)..]);
string[] nameParts = nameAndParameters.Split(';');
string name = nameParts[0];
Dictionary<string, string> parameters = new(StringComparer.OrdinalIgnoreCase);
foreach (string parameterPart in nameParts.Skip(1))
{
int equalsIndex = parameterPart.IndexOf('=', StringComparison.Ordinal);
if (equalsIndex > 0)
{
parameters[parameterPart[..equalsIndex]] = parameterPart[(equalsIndex + 1)..].Trim('"');
}
}
if (!properties.TryGetValue(name, out var values))
{
values = [];
properties[name] = values;
}
values.Add((parameters, value));
}
private static string UnescapeText(string value)
{
return value
.Replace("\\n", "\n", StringComparison.OrdinalIgnoreCase)
.Replace("\\,", ",", StringComparison.Ordinal)
.Replace("\\;", ";", StringComparison.Ordinal)
.Replace("\\\\", "\\", StringComparison.Ordinal);
}
private static bool TryGetFirst(
Dictionary<string, List<(Dictionary<string, string> Parameters, string Value)>> properties,
string key,
out (Dictionary<string, string> Parameters, string Value) value)
{
if (properties.TryGetValue(key, out var values) && values.Count > 0)
{
value = values[0];
return true;
}
value = default;
return false;
}
private static string? GetText(
Dictionary<string, List<(Dictionary<string, string> Parameters, string Value)>> properties,
string key)
{
return TryGetFirst(properties, key, out var value) ? value.Value : null;
}
private static IcsDateTimeValue ParseDateTimeValue(
string value,
Dictionary<string, string> parameters)
{
bool isAllDay = parameters.TryGetValue("VALUE", out string? valueType) &&
valueType.Equals("DATE", StringComparison.OrdinalIgnoreCase);
if (isAllDay || value.Length == 8)
{
DateOnly date = DateOnly.ParseExact(value, "yyyyMMdd", CultureInfo.InvariantCulture);
return new IcsDateTimeValue(true, false, date, null, null, null);
}
bool utc = value.EndsWith('Z');
string parseValue = utc ? value[..^1] : value;
DateTime local = DateTime.ParseExact(parseValue, "yyyyMMdd'T'HHmmss", CultureInfo.InvariantCulture);
if (utc)
{
DateTimeOffset utcValue = new(DateTime.SpecifyKind(local, DateTimeKind.Utc));
return new IcsDateTimeValue(false, false, DateOnly.FromDateTime(local), local, utcValue, "UTC");
}
string? timeZoneId = parameters.GetValueOrDefault("TZID");
bool floating = string.IsNullOrWhiteSpace(timeZoneId);
return new IcsDateTimeValue(
false,
floating,
DateOnly.FromDateTime(local),
local,
TryConvertToUtc(local, timeZoneId),
timeZoneId);
}
private static DateTimeOffset? TryConvertToUtc(DateTime localDateTime, string? timeZoneId)
{
if (string.IsNullOrWhiteSpace(timeZoneId))
{
return null;
}
try
{
TimeZoneInfo timeZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId);
DateTime unspecified = DateTime.SpecifyKind(localDateTime, DateTimeKind.Unspecified);
return TimeZoneInfo.ConvertTimeToUtc(unspecified, timeZone);
}
catch (TimeZoneNotFoundException)
{
return null;
}
catch (InvalidTimeZoneException)
{
return null;
}
}
private static IEnumerable<ParsedCalendarEvent> Expand(
IcsRawEvent rawEvent,
DateOnly rangeStart,
DateOnly rangeEnd)
{
TimeSpan duration = GetDuration(rawEvent.Start, rawEvent.End);
IReadOnlyCollection<DateOnly> starts = ExpandStartDates(rawEvent, rangeStart, rangeEnd);
foreach (DateOnly startDate in starts)
{
int dayOffset = startDate.DayNumber - rawEvent.Start.Date.DayNumber;
IcsDateTimeValue occurrenceStart = Shift(rawEvent.Start, dayOffset);
IcsDateTimeValue occurrenceEnd = rawEvent.End is null
? ShiftByDuration(occurrenceStart, duration)
: Shift(rawEvent.End, dayOffset);
yield return new ParsedCalendarEvent(
rawEvent.Uid,
rawEvent.Title,
rawEvent.Description,
occurrenceStart.IsAllDay,
occurrenceStart.IsFloatingTime,
occurrenceStart.Date,
occurrenceEnd.Date,
occurrenceStart.LocalDateTime,
occurrenceEnd.LocalDateTime,
occurrenceStart.UtcDateTime,
occurrenceEnd.UtcDateTime,
occurrenceStart.TimeZoneId,
rawEvent.RRule is null ? null : rawEvent.Uid,
rawEvent.Location,
rawEvent.SourceUrl,
rawEvent.LastModifiedAt);
}
}
private static TimeSpan GetDuration(IcsDateTimeValue start, IcsDateTimeValue? end)
{
if (end is null)
{
return start.IsAllDay ? TimeSpan.FromDays(1) : TimeSpan.Zero;
}
if (start.IsAllDay)
{
return TimeSpan.FromDays(Math.Max(1, end.Date.DayNumber - start.Date.DayNumber));
}
if (start.LocalDateTime.HasValue && end.LocalDateTime.HasValue)
{
return end.LocalDateTime.Value - start.LocalDateTime.Value;
}
return TimeSpan.Zero;
}
private static List<DateOnly> ExpandStartDates(
IcsRawEvent rawEvent,
DateOnly rangeStart,
DateOnly rangeEnd)
{
if (string.IsNullOrWhiteSpace(rawEvent.RRule))
{
return IsInRange(rawEvent.Start.Date, rangeStart, rangeEnd) ? [rawEvent.Start.Date] : [];
}
Dictionary<string, string> rule = rawEvent.RRule
.Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
.Select(part => part.Split('=', 2))
.Where(parts => parts.Length == 2)
.ToDictionary(parts => parts[0], parts => parts[1], StringComparer.OrdinalIgnoreCase);
string frequency = rule.GetValueOrDefault("FREQ", "DAILY");
int interval = int.TryParse(rule.GetValueOrDefault("INTERVAL"), out int parsedInterval)
? Math.Max(1, parsedInterval)
: 1;
int? count = int.TryParse(rule.GetValueOrDefault("COUNT"), out int parsedCount) ? parsedCount : null;
DateOnly? until = TryParseUntil(rule.GetValueOrDefault("UNTIL"));
List<DateOnly> dates = [];
DateOnly current = rawEvent.Start.Date;
for (int occurrence = 1; occurrence <= (count ?? 500); occurrence++)
{
if (until.HasValue && current > until.Value)
{
break;
}
if (current > rangeEnd)
{
break;
}
if (IsInRange(current, rangeStart, rangeEnd))
{
dates.Add(current);
}
current = frequency.ToUpperInvariant() switch
{
"YEARLY" => current.AddYears(interval),
"MONTHLY" => current.AddMonths(interval),
"WEEKLY" => current.AddDays(7 * interval),
_ => current.AddDays(interval),
};
}
return dates;
}
private static DateOnly? TryParseUntil(string? value)
{
if (string.IsNullOrWhiteSpace(value))
{
return null;
}
string dateValue = value.EndsWith('Z') ? value[..^1] : value;
if (dateValue.Length >= 8 &&
DateOnly.TryParseExact(dateValue[..8], "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateOnly date))
{
return date;
}
return null;
}
private static bool IsInRange(DateOnly value, DateOnly rangeStart, DateOnly rangeEnd)
{
return value >= rangeStart && value <= rangeEnd;
}
private static IcsDateTimeValue Shift(IcsDateTimeValue value, int dayOffset)
{
return value with
{
Date = value.Date.AddDays(dayOffset),
LocalDateTime = value.LocalDateTime?.AddDays(dayOffset),
UtcDateTime = value.UtcDateTime?.AddDays(dayOffset),
};
}
private static IcsDateTimeValue ShiftByDuration(IcsDateTimeValue value, TimeSpan duration)
{
if (value.IsAllDay)
{
return value with { Date = value.Date.AddDays(Math.Max(1, (int)duration.TotalDays)) };
}
return value with
{
Date = value.LocalDateTime.HasValue
? DateOnly.FromDateTime(value.LocalDateTime.Value.Add(duration))
: value.Date,
LocalDateTime = value.LocalDateTime?.Add(duration),
UtcDateTime = value.UtcDateTime?.Add(duration),
};
}
}

View File

@@ -1,6 +1,6 @@
namespace Socialize.Api.Modules.Campaigns.Data; namespace Socialize.Api.Modules.Campaigns.Data;
public class Campaign internal class Campaign
{ {
public Guid Id { get; init; } public Guid Id { get; init; }
public Guid WorkspaceId { get; set; } public Guid WorkspaceId { get; set; }

View File

@@ -1,8 +1,10 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Socialize.Api.Modules.Clients.Data;
using Socialize.Api.Modules.Workspaces.Data;
namespace Socialize.Api.Modules.Campaigns.Data; namespace Socialize.Api.Modules.Campaigns.Data;
public static class CampaignModelConfiguration internal static class CampaignModelConfiguration
{ {
public static ModelBuilder ConfigureCampaignsModule(this ModelBuilder modelBuilder) public static ModelBuilder ConfigureCampaignsModule(this ModelBuilder modelBuilder)
{ {
@@ -20,6 +22,14 @@ public static class CampaignModelConfiguration
campaign.HasIndex(x => new { x.ClientId, x.Name }).IsUnique(); campaign.HasIndex(x => new { x.ClientId, x.Name }).IsUnique();
campaign.HasIndex(x => x.WorkspaceId); campaign.HasIndex(x => x.WorkspaceId);
campaign.HasIndex(x => x.ClientId); campaign.HasIndex(x => x.ClientId);
campaign.HasOne<Workspace>()
.WithMany()
.HasForeignKey(x => x.WorkspaceId)
.OnDelete(DeleteBehavior.Restrict);
campaign.HasOne<Client>()
.WithMany()
.HasForeignKey(x => x.ClientId)
.OnDelete(DeleteBehavior.Restrict);
}); });
return modelBuilder; return modelBuilder;

View File

@@ -6,7 +6,7 @@ using Socialize.Api.Modules.Campaigns.Data;
namespace Socialize.Api.Modules.Campaigns.Handlers; namespace Socialize.Api.Modules.Campaigns.Handlers;
public record CreateCampaignRequest( internal record CreateCampaignRequest(
Guid WorkspaceId, Guid WorkspaceId,
Guid ClientId, Guid ClientId,
string Name, string Name,
@@ -15,7 +15,7 @@ public record CreateCampaignRequest(
string? Description, string? Description,
string? Notes); string? Notes);
public class CreateCampaignRequestValidator internal class CreateCampaignRequestValidator
: Validator<CreateCampaignRequest> : Validator<CreateCampaignRequest>
{ {
public CreateCampaignRequestValidator() public CreateCampaignRequestValidator()
@@ -32,7 +32,7 @@ public class CreateCampaignRequestValidator
} }
} }
public class CreateCampaignHandler( internal class CreateCampaignHandler(
AppDbContext dbContext, AppDbContext dbContext,
AccessScopeService accessScopeService) AccessScopeService accessScopeService)
: Endpoint<CreateCampaignRequest, CampaignDto> : Endpoint<CreateCampaignRequest, CampaignDto>

View File

@@ -6,9 +6,9 @@ using Socialize.Api.Modules.Campaigns.Data;
namespace Socialize.Api.Modules.Campaigns.Handlers; namespace Socialize.Api.Modules.Campaigns.Handlers;
public record GetCampaignsRequest(Guid? WorkspaceId, Guid? ClientId); internal record GetCampaignsRequest(Guid? WorkspaceId, Guid? ClientId);
public record CampaignDto( internal record CampaignDto(
Guid Id, Guid Id,
Guid WorkspaceId, Guid WorkspaceId,
Guid ClientId, Guid ClientId,
@@ -19,7 +19,7 @@ public record CampaignDto(
DateTimeOffset StartDate, DateTimeOffset StartDate,
DateTimeOffset EndDate); DateTimeOffset EndDate);
public class GetCampaignsHandler( internal class GetCampaignsHandler(
AppDbContext dbContext, AppDbContext dbContext,
AccessScopeService accessScopeService) AccessScopeService accessScopeService)
: Endpoint<GetCampaignsRequest, IReadOnlyCollection<CampaignDto>> : Endpoint<GetCampaignsRequest, IReadOnlyCollection<CampaignDto>>
@@ -34,7 +34,7 @@ public class GetCampaignsHandler(
{ {
IQueryable<Campaign> query = dbContext.Campaigns.AsQueryable(); IQueryable<Campaign> query = dbContext.Campaigns.AsQueryable();
if (!accessScopeService.IsManager(User)) if (!AccessScopeService.IsManager(User))
{ {
IReadOnlyCollection<Guid> workspaceScopeIds = await accessScopeService.GetAccessibleWorkspaceIdsAsync(User, ct); IReadOnlyCollection<Guid> workspaceScopeIds = await accessScopeService.GetAccessibleWorkspaceIdsAsync(User, ct);
IReadOnlyCollection<Guid> clientScopeIds = User.GetClientScopeIds(); IReadOnlyCollection<Guid> clientScopeIds = User.GetClientScopeIds();

View File

@@ -2,7 +2,7 @@ using Socialize.Api.Modules.Campaigns.Data;
namespace Socialize.Api.Modules.Campaigns; namespace Socialize.Api.Modules.Campaigns;
public static class DependencyInjection internal static class ModuleRegistration
{ {
public static WebApplicationBuilder AddCampaignsModule( public static WebApplicationBuilder AddCampaignsModule(
this WebApplicationBuilder builder) this WebApplicationBuilder builder)

View File

@@ -0,0 +1,12 @@
namespace Socialize.Api.Modules.Channels.Data;
internal class Channel
{
public Guid Id { get; init; }
public Guid WorkspaceId { get; set; }
public required string Name { get; set; }
public required string Network { get; set; }
public string? Handle { get; set; }
public string? ExternalUrl { get; set; }
public DateTimeOffset CreatedAt { get; init; }
}

View File

@@ -0,0 +1,31 @@
using Microsoft.EntityFrameworkCore;
using Socialize.Api.Modules.Workspaces.Data;
namespace Socialize.Api.Modules.Channels.Data;
internal static class ChannelModelConfiguration
{
public static ModelBuilder ConfigureChannelsModule(this ModelBuilder modelBuilder)
{
modelBuilder.Entity<Channel>(channel =>
{
channel.ToTable("Channels");
channel.HasKey(x => x.Id);
channel.Property(x => x.Name).HasMaxLength(256).IsRequired();
channel.Property(x => x.Network).HasMaxLength(64).IsRequired();
channel.Property(x => x.Handle).HasMaxLength(256);
channel.Property(x => x.ExternalUrl).HasMaxLength(2048);
channel.Property(x => x.CreatedAt)
.ValueGeneratedOnAdd()
.HasDefaultValueSql("CURRENT_TIMESTAMP");
channel.HasIndex(x => x.WorkspaceId);
channel.HasIndex(x => new { x.WorkspaceId, x.Network, x.Name }).IsUnique();
channel.HasOne<Workspace>()
.WithMany()
.HasForeignKey(x => x.WorkspaceId)
.OnDelete(DeleteBehavior.Restrict);
});
return modelBuilder;
}
}

Some files were not shown because too many files have changed in this diff Show More