Skip to content

Commit

Permalink
feat: improved optional product analytics (#452)
Browse files Browse the repository at this point in the history
* feat: add fe posthog config

* feat: add fe posthog to metadata endpoint

* feat: dynamically load ph on fe

* feat: add analyticsOptOut to tenant db

* feat: respect opt out

* feat: update tenant settings

* feat: mask all text
  • Loading branch information
grutt committed May 5, 2024
1 parent d673cf1 commit 2b910a8
Show file tree
Hide file tree
Showing 27 changed files with 885 additions and 152 deletions.
4 changes: 4 additions & 0 deletions api-contracts/openapi/components/schemas/_index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ APIMeta:
$ref: "./metadata.yaml#/APIMeta"
APIMetaAuth:
$ref: "./metadata.yaml#/APIMetaAuth"
APIMetaPosthog:
$ref: "./metadata.yaml#/APIMetaPosthog"
ListAPIMetaIntegration:
$ref: "./metadata.yaml#/ListAPIMetaIntegration"
APIMetaIntegration:
Expand Down Expand Up @@ -50,6 +52,8 @@ TenantList:
$ref: "./tenant.yaml#/TenantList"
CreateTenantRequest:
$ref: "./tenant.yaml#/CreateTenantRequest"
UpdateTenantRequest:
$ref: "./tenant.yaml#/UpdateTenantRequest"
Event:
$ref: "./event.yaml#/Event"
EventData:
Expand Down
16 changes: 16 additions & 0 deletions api-contracts/openapi/components/schemas/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ APIMeta:
type: string
description: the Pylon app ID for usepylon.com chat support
example: 12345678-1234-1234-1234-123456789012
posthog:
$ref: "#/APIMetaPosthog"

APIMetaAuth:
type: object
properties:
Expand All @@ -18,6 +21,19 @@ APIMetaAuth:
example:
- basic
- google

APIMetaPosthog:
type: object
properties:
apiKey:
type: string
description: the PostHog API key
example: phk_1234567890abcdef
apiHost:
type: string
description: the PostHog API host
example: https://posthog.example.com

ListAPIMetaIntegration:
type: array
items:
Expand Down
10 changes: 10 additions & 0 deletions api-contracts/openapi/components/schemas/tenant.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Tenant:
slug:
type: string
description: The slug of the tenant.
analyticsOptOut:
type: boolean
description: Whether the tenant has opted out of analytics.
required:
- metadata
- name
Expand All @@ -31,6 +34,13 @@ CreateTenantRequest:
- slug
type: object

UpdateTenantRequest:
properties:
analyticsOptOut:
type: boolean
description: Whether the tenant has opted out of analytics.
type: object

TenantMember:
properties:
metadata:
Expand Down
2 changes: 2 additions & 0 deletions api-contracts/openapi/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ paths:
$ref: "./paths/user/user.yaml#/rejectInvite"
/api/v1/tenants:
$ref: "./paths/tenant/tenant.yaml#/tenants"
/api/v1/tenants/{tenant}:
$ref: "./paths/tenant/tenant.yaml#/updateTenants"
/api/v1/tenants/{tenant}/invites:
$ref: "./paths/tenant/tenant.yaml#/invites"
/api/v1/tenants/{tenant}/invites/{tenant-invite}:
Expand Down
44 changes: 44 additions & 0 deletions api-contracts/openapi/paths/tenant/tenant.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,50 @@ tenants:
summary: Create tenant
tags:
- Tenant
updateTenants:
patch:
x-resources: ["tenant"]
description: Update an existing tenant
operationId: tenant:update
parameters:
- description: The tenant id
in: path
name: tenant
required: true
schema:
type: string
format: uuid
minLength: 36
maxLength: 36
requestBody:
content:
application/json:
schema:
$ref: "../../components/schemas/_index.yaml#/UpdateTenantRequest"
description: The tenant properties to update
required: true
responses:
"200":
content:
application/json:
schema:
$ref: "../../components/schemas/_index.yaml#/Tenant"
description: Successfully created the tenant
"400":
content:
application/json:
schema:
$ref: "../../components/schemas/_index.yaml#/APIErrors"
description: A malformed or bad request
"403":
content:
application/json:
schema:
$ref: "../../components/schemas/_index.yaml#/APIError"
description: Forbidden
summary: Update tenant
tags:
- Tenant
invites:
post:
x-resources: ["tenant"]
Expand Down
10 changes: 10 additions & 0 deletions api/v1/server/handlers/metadata/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,21 @@ func (u *MetadataService) MetadataGet(ctx echo.Context, request gen.MetadataGetR

pylonAppID := u.config.Pylon.AppID

var posthogConfig *gen.APIMetaPosthog

if u.config.FePosthog != nil {
posthogConfig = &gen.APIMetaPosthog{
ApiKey: &u.config.FePosthog.ApiKey,
ApiHost: &u.config.FePosthog.ApiHost,
}
}

meta := gen.APIMeta{
Auth: &gen.APIMetaAuth{
Schemes: &authTypes,
},
PylonAppId: &pylonAppID,
Posthog: posthogConfig,
}

return gen.MetadataGet200JSONResponse(meta), nil
Expand Down
39 changes: 39 additions & 0 deletions api/v1/server/handlers/tenants/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package tenants

import (
"github.com/labstack/echo/v4"

"github.com/hatchet-dev/hatchet/api/v1/server/oas/gen"
"github.com/hatchet-dev/hatchet/api/v1/server/oas/transformers"
"github.com/hatchet-dev/hatchet/internal/repository"
"github.com/hatchet-dev/hatchet/internal/repository/prisma/db"
)

func (t *TenantService) TenantUpdate(ctx echo.Context, request gen.TenantUpdateRequestObject) (gen.TenantUpdateResponseObject, error) {
tenant := ctx.Get("tenant").(*db.TenantModel)

// validate the request
if apiErrors, err := t.config.Validator.ValidateAPI(request.Body); err != nil {
return nil, err
} else if apiErrors != nil {
return gen.TenantUpdate400JSONResponse(*apiErrors), nil
}

// construct the database query
createOpts := &repository.UpdateTenantOpts{}

if request.Body.AnalyticsOptOut != nil {
createOpts.AnalyticsOptOut = request.Body.AnalyticsOptOut
}

// update the tenant
tenant, err := t.config.APIRepository.Tenant().UpdateTenant(tenant.ID, createOpts)

if err != nil {
return nil, err
}

return gen.TenantUpdate200JSONResponse(
*transformers.ToTenant(tenant),
), nil
}
Loading

0 comments on commit 2b910a8

Please sign in to comment.