commit 1fd7dad8e47dee46626811fccf1811636a90153a Author: troneras Date: Mon Aug 12 17:33:10 2024 +0200 # This is a combination of 2 commits. # This is the 1st commit message: first commit # This is the commit message #2: fixed cache diff --git a/.env.local b/.env.local new file mode 100644 index 0000000..4898d33 --- /dev/null +++ b/.env.local @@ -0,0 +1,5 @@ +LISTMONK_URL=http://localhost:9000 +NEXT_APP_URL=http://localhost:3000 +BASIC_AUTH_USERNAME=username +BASIC_AUTH_PASSWORD=password +API_KEY=your-api-key diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..9a0496d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,22 @@ +FROM golang:1.17-alpine AS builder + +WORKDIR /app + +COPY go.mod go.sum ./ +RUN go mod download + +COPY . . + +RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main . + +FROM alpine:latest + +RUN apk --no-cache add ca-certificates + +WORKDIR /root/ + +COPY --from=builder /app/main . + +EXPOSE 8808 + +CMD ["./main"] \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..00e2db0 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# ghost-listmonk-connector diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..ef9a551 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,36 @@ +version: '3' +services: + app: + build: . + ports: + - "8808:8808" + environment: + - PORT=8808 + - LISTMONK_URL=http://listmonk:9000 + - BASIC_AUTH_USERNAME=admin + - BASIC_AUTH_PASSWORD=password + depends_on: + - listmonk + + listmonk: + image: listmonk/listmonk:latest + ports: + - "9000:9000" + environment: + - APP_DB_HOST=db + - APP_DB_USER=listmonk + - APP_DB_PASSWORD=listmonk + depends_on: + - db + + db: + image: postgres:13 + environment: + - POSTGRES_USER=listmonk + - POSTGRES_PASSWORD=listmonk + - POSTGRES_DB=listmonk + volumes: + - listmonk-data:/var/lib/postgresql/data + +volumes: + listmonk-data: \ No newline at end of file diff --git a/ghost_listmonk.db b/ghost_listmonk.db new file mode 100644 index 0000000..9ddb8cf Binary files /dev/null and b/ghost_listmonk.db differ diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b4bba73 --- /dev/null +++ b/go.mod @@ -0,0 +1,43 @@ +module github.com/troneras/ghost-listmonk-connector + +go 1.21.6 + +require ( + github.com/gin-contrib/cors v1.7.2 + github.com/gin-gonic/gin v1.10.0 + github.com/google/uuid v1.6.0 + github.com/mattn/go-sqlite3 v1.14.22 + github.com/stretchr/testify v1.9.0 +) + +require ( + github.com/bytedance/sonic v1.12.1 // indirect + github.com/bytedance/sonic/loader v0.2.0 // indirect + github.com/cloudwego/base64x v0.1.4 // indirect + github.com/cloudwego/iasm v0.2.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/gabriel-vasile/mimetype v1.4.5 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.22.0 // indirect + github.com/goccy/go-json v0.10.3 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/cpuid/v2 v2.2.8 // indirect + github.com/kr/text v0.2.0 // indirect + github.com/leodido/go-urn v1.4.0 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pelletier/go-toml/v2 v2.2.2 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.2.12 // indirect + golang.org/x/arch v0.9.0 // indirect + golang.org/x/crypto v0.26.0 // indirect + golang.org/x/net v0.28.0 // indirect + golang.org/x/sys v0.24.0 // indirect + golang.org/x/text v0.17.0 // indirect + google.golang.org/protobuf v1.34.2 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..8a06fc6 --- /dev/null +++ b/go.sum @@ -0,0 +1,102 @@ +github.com/bytedance/sonic v1.12.1 h1:jWl5Qz1fy7X1ioY74WqO0KjAMtAGQs4sYnjiEBiyX24= +github.com/bytedance/sonic v1.12.1/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKzMzT9r/rk= +github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= +github.com/bytedance/sonic/loader v0.2.0 h1:zNprn+lsIP06C/IqCHs3gPQIvnvpKbbxyXQP1iU4kWM= +github.com/bytedance/sonic/loader v0.2.0/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= +github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y= +github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= +github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg= +github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gabriel-vasile/mimetype v1.4.5 h1:J7wGKdGu33ocBOhGy0z653k/lFKLFDPJMG8Gql0kxn4= +github.com/gabriel-vasile/mimetype v1.4.5/go.mod h1:ibHel+/kbxn9x2407k1izTA1S81ku1z/DlgOW2QE0M4= +github.com/gin-contrib/cors v1.7.2 h1:oLDHxdg8W/XDoN/8zamqk/Drgt4oVZDvaV0YmvVICQw= +github.com/gin-contrib/cors v1.7.2/go.mod h1:SUJVARKgQ40dmrzgXEVxj2m7Ig1v1qIboQkPDTQ9t2E= +github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= +github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU= +github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y= +github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.22.0 h1:k6HsTZ0sTnROkhS//R0O+55JgM8C4Bx7ia+JlgcnOao= +github.com/go-playground/validator/v10 v10.22.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= +github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA= +github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= +github.com/klauspost/cpuid/v2 v2.2.8 h1:+StwCXwm9PdpiEkPyzBXIy+M9KUb4ODm0Zarf1kS5BM= +github.com/klauspost/cpuid/v2 v2.2.8/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= +github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= +github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= +github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= +github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= +github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= +github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8= +github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= +github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= +github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= +github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= +golang.org/x/arch v0.9.0 h1:ub9TgUInamJ8mrZIGlBG6/4TqWeMszd4N8lNorbrr6k= +golang.org/x/arch v0.9.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= +golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw= +golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54= +golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE= +golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg= +golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= +golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= +google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= diff --git a/handlers/son_handlers.go b/handlers/son_handlers.go new file mode 100644 index 0000000..18bbc46 --- /dev/null +++ b/handlers/son_handlers.go @@ -0,0 +1,139 @@ +package handlers + +import ( + "net/http" + + "github.com/gin-gonic/gin" + "github.com/troneras/ghost-listmonk-connector/models" + "github.com/troneras/ghost-listmonk-connector/services" + "github.com/troneras/ghost-listmonk-connector/utils" +) + +type SonHandler struct { + storage *services.SonStorage +} + +type ListmonkHandler struct { + client *services.ListmonkClient +} + +func NewSonHandler(storage *services.SonStorage) *SonHandler { + return &SonHandler{storage: storage} +} + +func NewListmonkHandler(client *services.ListmonkClient) *ListmonkHandler { + return &ListmonkHandler{client: client} +} + +func (h *ListmonkHandler) GetLists(c *gin.Context) { + lists, err := h.client.GetLists() + if err != nil { + utils.ErrorLogger.Errorf("Failed to get lists: %v", err) + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + + c.JSON(http.StatusOK, gin.H{"data": lists}) +} + +func (h *ListmonkHandler) GetTemplates(c *gin.Context) { + templates, err := h.client.GetTemplates() + if err != nil { + utils.ErrorLogger.Errorf("Failed to get templates: %v", err) + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + + c.JSON(http.StatusOK, gin.H{"data": templates}) +} + +func (h *SonHandler) Create(c *gin.Context) { + var son models.Son + if err := c.ShouldBindJSON(&son); err != nil { + utils.ErrorLogger.Errorf("Invalid Son data: %v", err) + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + if err := h.storage.Create(&son); err != nil { + utils.ErrorLogger.Errorf("Failed to create Son: %v", err) + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + + utils.InfoLogger.Infof("Created Son: %s", utils.PrettyPrint(son)) + c.JSON(http.StatusCreated, son) +} + +func (h *SonHandler) Get(c *gin.Context) { + id := c.Param("id") + son, err := h.storage.Get(id) + if err != nil { + if err == services.ErrSonNotFound { + utils.ErrorLogger.Errorf("Son not found: %s", id) + c.JSON(http.StatusNotFound, gin.H{"error": "Son not found"}) + } else { + utils.ErrorLogger.Errorf("Failed to get Son: %v", err) + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + } + return + } + + utils.InfoLogger.Infof("Retrieved Son: %s", utils.PrettyPrint(son)) + c.JSON(http.StatusOK, son) +} + +func (h *SonHandler) Update(c *gin.Context) { + id := c.Param("id") + var son models.Son + if err := c.ShouldBindJSON(&son); err != nil { + utils.ErrorLogger.Errorf("Invalid Son data: %v", err) + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + son.ID = id + if err := h.storage.Update(son); err != nil { + if err == services.ErrSonNotFound { + utils.ErrorLogger.Errorf("Son not found for update: %s", id) + c.JSON(http.StatusNotFound, gin.H{"error": "Son not found"}) + } else { + utils.ErrorLogger.Errorf("Failed to update Son: %v", err) + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + } + return + } + + utils.InfoLogger.Infof("Updated Son: %s", utils.PrettyPrint(son)) + c.JSON(http.StatusOK, son) +} + +func (h *SonHandler) Delete(c *gin.Context) { + id := c.Param("id") + if err := h.storage.Delete(id); err != nil { + if err == services.ErrSonNotFound { + utils.ErrorLogger.Errorf("Son not found for deletion: %s", id) + c.JSON(http.StatusNotFound, gin.H{"error": "Son not found"}) + } else { + utils.ErrorLogger.Errorf("Failed to delete Son: %v", err) + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + } + return + } + + utils.InfoLogger.Infof("Deleted Son: %s", id) + c.JSON(http.StatusOK, gin.H{"message": "Son deleted successfully"}) +} + +func (h *SonHandler) List(c *gin.Context) { + sons, err := h.storage.List() + + if err != nil { + utils.ErrorLogger.Errorf("Failed to list Sons: %v", err) + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + + utils.InfoLogger.Infof("Retrieved %d Sons", len(sons)) + c.JSON(http.StatusOK, sons) +} diff --git a/handlers/webhook_handler.go b/handlers/webhook_handler.go new file mode 100644 index 0000000..1f29744 --- /dev/null +++ b/handlers/webhook_handler.go @@ -0,0 +1,97 @@ +package handlers + +import ( + "net/http" + + "github.com/gin-gonic/gin" + "github.com/troneras/ghost-listmonk-connector/models" + "github.com/troneras/ghost-listmonk-connector/services" + "github.com/troneras/ghost-listmonk-connector/utils" +) + +type WebhookHandler struct { + sonStorage *services.SonStorage + executor *services.SonExecutor +} + +func NewWebhookHandler(sonStorage *services.SonStorage, executor *services.SonExecutor) *WebhookHandler { + return &WebhookHandler{ + sonStorage: sonStorage, + executor: executor, + } +} + +func (h *WebhookHandler) HandleWebhook(c *gin.Context) { + var webhookData map[string]interface{} + if err := c.ShouldBindJSON(&webhookData); err != nil { + utils.ErrorLogger.Errorf("Invalid webhook data: %v", err) + c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid webhook data"}) + return + } + + utils.InfoLogger.Infof("Received webhook: %s", utils.PrettyPrint(webhookData)) + + triggerType, err := determineTriggerType(webhookData) + if err != nil { + utils.ErrorLogger.Errorf("Unable to determine trigger type: %v", err) + c.JSON(http.StatusBadRequest, gin.H{"error": "Unable to determine trigger type"}) + return + } + + utils.InfoLogger.Infof("Determined trigger type: %s", triggerType) + + // Find and execute relevant Sons + sons, err := h.sonStorage.List() + + if err != nil { + utils.ErrorLogger.Errorf("Failed to list Sons: %v", err) + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to list Sons"}) + return + } + + executedCount := 0 + for _, son := range sons { + if son.Trigger == triggerType { + go func(s models.Son) { + utils.InfoLogger.Infof("Executing Son %s for trigger %s", s.ID, triggerType) + h.executor.ExecuteSon(s, webhookData) + }(son) + executedCount++ + } + } + + utils.InfoLogger.Infof("Executed %d Sons for trigger %s", executedCount, triggerType) + c.JSON(http.StatusOK, gin.H{"message": "Webhook processed successfully", "sons_executed": executedCount}) +} + +func determineTriggerType(webhookData map[string]interface{}) (models.TriggerType, error) { + if member, ok := webhookData["member"].(map[string]interface{}); ok { + if _, ok := member["current"]; ok { + if previous, ok := member["previous"].(map[string]interface{}); ok && len(previous) > 0 { + // log member["previous"] + utils.InfoLogger.Infof("Previous member data: %v", previous) + return models.TriggerMemberUpdated, nil + } + return models.TriggerMemberCreated, nil + } + // Assuming member deletion doesn't have 'current' field + return models.TriggerMemberDeleted, nil + } + + if post, ok := webhookData["post"].(map[string]interface{}); ok { + if status, ok := post["status"].(string); ok && status == "published" { + return models.TriggerPostPublished, nil + } + if status, ok := post["status"].(string); ok && status == "scheduled" { + return models.TriggerPostScheduled, nil + } + } + + if page, ok := webhookData["page"].(map[string]interface{}); ok { + if status, ok := page["status"].(string); ok && status == "published" { + return models.TriggerPagePublished, nil + } + } + + return "", utils.NewError("UnknownTriggerType", "Unable to determine trigger type from webhook data") +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..990888c --- /dev/null +++ b/main.go @@ -0,0 +1,70 @@ +package main + +import ( + "github.com/gin-contrib/cors" + "github.com/gin-gonic/gin" + "github.com/troneras/ghost-listmonk-connector/handlers" + "github.com/troneras/ghost-listmonk-connector/middleware" + "github.com/troneras/ghost-listmonk-connector/services" + "github.com/troneras/ghost-listmonk-connector/utils" +) + +func main() { + config, err := utils.LoadConfig() + if err != nil { + utils.ErrorLogger.Fatalf("Failed to load configuration: %v", err) + } + r := gin.Default() + + // CORS configuration + corsConfig := cors.DefaultConfig() + corsConfig.AllowOrigins = []string{"http://localhost:3000"} // Add your frontend URL here + corsConfig.AllowMethods = []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"} + corsConfig.AllowHeaders = []string{"Origin", "Content-Type", "Accept", "Authorization"} + corsConfig.AllowCredentials = true + + r.Use(cors.New(corsConfig)) + + // Initialize services + sonStorage, err := services.NewSonStorage("ghost_listmonk.db") + if err != nil { + utils.ErrorLogger.Fatalf("Failed to initialize son storage: %v", err) + } + + listmonkClient := services.NewListmonkClient(config) + sonExecutor := services.NewSonExecutor(listmonkClient) + sonHandler := handlers.NewSonHandler(sonStorage) + webhookHandler := handlers.NewWebhookHandler(sonStorage, sonExecutor) + listmonkHandler := handlers.NewListmonkHandler(listmonkClient) + + // Webhook endpoint (no auth required) + r.POST("/webhook", webhookHandler.HandleWebhook) + + // Web UI routes (auth required) + authorized := r.Group("/") + authorized.Use(middleware.APIKeyAuth(config.APIKey)) + { + authorized.GET("/", handleHome) + + // Son routes + sons := authorized.Group("/sons") + { + sons.POST("", sonHandler.Create) + sons.GET("", sonHandler.List) + sons.GET("/:id", sonHandler.Get) + sons.PUT("/:id", sonHandler.Update) + sons.DELETE("/:id", sonHandler.Delete) + } + authorized.GET("/lists", listmonkHandler.GetLists) + authorized.GET("/templates", listmonkHandler.GetTemplates) + } + + utils.InfoLogger.Infof("Server starting on port %s", config.Port) + if err := r.Run(":" + config.Port); err != nil { + utils.ErrorLogger.Fatalf("Failed to start server: %v", err) + } +} + +func handleHome(c *gin.Context) { + c.JSON(200, gin.H{"message": "Welcome to the Ghost-Listmonk Connector"}) +} diff --git a/middleware/auth.go b/middleware/auth.go new file mode 100644 index 0000000..29851ce --- /dev/null +++ b/middleware/auth.go @@ -0,0 +1,24 @@ +package middleware + +import ( + "net/http" + + "github.com/gin-gonic/gin" +) + +func APIKeyAuth(validAPIKey string) gin.HandlerFunc { + return func(c *gin.Context) { + apiKey := c.GetHeader("Authorization") + if apiKey == "" { + apiKey = c.Query("api_key") + } + + if apiKey != "Bearer "+validAPIKey { + c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid API key"}) + c.Abort() + return + } + + c.Next() + } +} diff --git a/models/son.go b/models/son.go new file mode 100644 index 0000000..ebe39bc --- /dev/null +++ b/models/son.go @@ -0,0 +1,68 @@ +package models + +import ( + "encoding/json" + "errors" + "time" +) + +type TriggerType string +type ActionType string + +const ( + TriggerMemberCreated TriggerType = "member_created" + TriggerMemberDeleted TriggerType = "member_deleted" + TriggerMemberUpdated TriggerType = "member_updated" + TriggerPagePublished TriggerType = "page_published" + TriggerPostPublished TriggerType = "post_published" + TriggerPostScheduled TriggerType = "post_scheduled" +) + +const ( + ActionSendTransactionalEmail ActionType = "send_transactional_email" + ActionManageSubscriber ActionType = "manage_subscriber" + ActionCreateCampaign ActionType = "create_campaign" +) + +type Son struct { + ID string `json:"id"` + Name string `json:"name"` + Trigger TriggerType `json:"trigger"` + Delay Duration `json:"delay"` + Actions []Action `json:"actions"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` +} + +type Action struct { + Type ActionType `json:"type"` + Parameters map[string]any `json:"parameters"` +} + +// Duration is a custom type to handle time.Duration in JSON +type Duration time.Duration + +func (d Duration) MarshalJSON() ([]byte, error) { + return json.Marshal(time.Duration(d).Minutes()) +} + +func (d *Duration) UnmarshalJSON(b []byte) error { + var v interface{} + if err := json.Unmarshal(b, &v); err != nil { + return err + } + switch value := v.(type) { + case float64: + *d = Duration(time.Duration(value) * time.Minute) + return nil + case string: + tmp, err := time.ParseDuration(value) + if err != nil { + return err + } + *d = Duration(tmp) + return nil + default: + return errors.New("invalid duration") + } +} diff --git a/previous_conversation.txt b/previous_conversation.txt new file mode 100644 index 0000000..04adcf1 --- /dev/null +++ b/previous_conversation.txt @@ -0,0 +1,240 @@ +Are you familiar with Ghost blog platform and listmonk? EditI'm familiar with the Ghost blogging platform, but I don't have detailed knowledge about listmonk. I can share what I know about Ghost and offer to discuss it further if you'd like. Would you like me to elaborate on Ghost, or do you have any specific questions about either platform? CopyRetryAListmonk is an opensource maillist/newsletter manager, it allows you to use Amazon SES for example to send mails. Ghost, on the other side only has integration with Mailchimp, that uses mailjet, what adds a recurring cost. I EditThank you for providing that information about listmonk. It's helpful to understand its functionality as an open-source mailing list and newsletter manager with the ability to use services like Amazon SES for sending emails. +You're correct about Ghost's native integration with Mailchimp. However, it's worth noting that Ghost does have some flexibility when it comes to email services: + +Native Mailchimp integration: As you mentioned, this is the built-in option that comes with Ghost, which can indeed add recurring costs. +Custom SMTP: Ghost allows you to use a custom SMTP server for sending emails. This means you could potentially set up your own email sending solution or use a different third-party service. +API and webhooks: Ghost has a robust API and webhook system that allows for custom integrations. While it might require some development work, it's possible to create custom integrations with other email services or tools. + +Given your interest in both Ghost and listmonk, it sounds like you might be looking for a more cost-effective or flexible email solution for a Ghost-based blog. Is that correct? If so, I can suggest a few potential approaches or discuss how you might integrate these systems. CopyRetryAThis is in part true, Ghost allows you to use a custom SMTP, but only for transactional emails, for newsletter ghost only allows you to use mailgun EditI appreciate you correcting and clarifying that information. You're absolutely right, and I apologize for my mistake. Let me restate the correct information: + +Ghost's newsletter functionality: As you pointed out, Ghost only allows Mailgun for sending newsletters. This is indeed a limitation if you're looking for more flexibility or cost-effective options for newsletter distribution. +Transactional emails: Ghost does allow custom SMTP for transactional emails (like password resets, notifications, etc.), but not for newsletters. + +This limitation with Ghost's newsletter functionality can indeed be frustrating for users who want more control over their email infrastructure or are looking to optimize costs. +Given this situation, I can see why you're interested in potentially using listmonk alongside Ghost. Some possible approaches could include: + +Using Ghost for your blog content and listmonk for managing your newsletter separately. +Exploring if there's a way to integrate listmonk with Ghost using Ghost's API or webhooks, though this would likely require custom development. +Investigating if there are any community-developed plugins or integrations that might bridge Ghost and listmonk or other email services. + +Would you like to discuss any of these options further, or do you have a specific goal in mind for combining Ghost and listmonk? CopyRetryAYes, I would like to manage my blog content with Ghost and my newsletter with ListMonk. When a new user registers to the newsletter, I can send a webhook from ghost where I want. I could use this information to then register this user in listmonk. EditThat's a great approach! You're on the right track with using Ghost's webhook functionality to connect it with listmonk. Here's a high-level overview of how you could set this up: + +Ghost Configuration: + +Set up a webhook in Ghost that triggers when a new newsletter subscriber is added. +Configure the webhook to send relevant user data (like email address) to an endpoint you control. + + +Intermediate API/Service: + +Create a simple API or serverless function (using AWS Lambda, Google Cloud Functions, or similar) that receives the webhook from Ghost. +This service would process the incoming data from Ghost and then make an API call to listmonk to add the new subscriber. + + +Listmonk Configuration: + +Set up listmonk and configure it to use your preferred email sending service (like Amazon SES). +Ensure listmonk's API is accessible to your intermediate service. + + +Integration Flow: + +User subscribes on your Ghost blog +Ghost sends webhook to your intermediate service +Your service processes the data and calls listmonk's API +Listmonk adds the new subscriber to your mailing list + + + +This setup allows you to maintain Ghost for content management while leveraging listmonk's more flexible and potentially cost-effective email management capabilities. +Would you like me to elaborate on any part of this process, such as setting up the webhook in Ghost or creating the intermediate service? CopyRetryAI'm thinking if handling it myself... my use case is that I've a google form collecting tens of thousands o applications and I need to reply to those people in masse (a few thousands per day), can you do some back of the envelope calculation of what the cost would be using lambda? EditCertainly, I can help you with a rough cost estimation for using AWS Lambda to handle this task. Let's break it down step by step: +AWS Lambda Cost EstimationClick to open document +Based on this calculation, using AWS Lambda for processing your email responses would be very cost-effective. The workload you described (a few thousand emails per day, totaling tens of thousands per month) falls well within AWS Lambda's free tier. +Key points: + +Cost: The processing itself would likely be free, assuming you're not using the Lambda free tier for other purposes. +Scalability: Lambda can easily handle this workload and scale up if needed. +Additional costs: Remember that this calculation doesn't include the cost of actually sending the emails. You'd need to factor in the costs of a service like Amazon SES (Simple Email Service) for sending the emails. +SES Costs: As a rough estimate, Amazon SES charges $0.10 per 1,000 emails for the first 62,000 emails each month, and $0.10 per 10,000 emails thereafter. So for 300,000 emails per month, the SES cost would be approximately $9.80. + +Would you like me to elaborate on any part of this calculation or discuss other aspects of implementing this solution? CopyRetryAThe other option would be to deploy a connector service, I could probably write this in go and host it alongside my listmonk and ghost EditThat's an excellent alternative approach. Developing a custom connector service in Go and hosting it alongside your Ghost and listmonk instances has several advantages: + +Cost-effective: You'd avoid additional cloud service costs for functions like AWS Lambda. +Control: Full control over the integration logic and deployment. +Performance: Go is known for its efficiency and low resource usage, making it ideal for this kind of service. +Simplicity: Keeping everything on the same infrastructure can simplify management and troubleshooting. + +Here's a high-level outline of how you might structure this: + +Go Connector Service: + +Create a small HTTP server in Go that listens for webhooks from Ghost. +Process the incoming data and use listmonk's API to add new subscribers. +Optionally, add logging and error handling for monitoring. + + +Deployment: + +Deploy this Go service on the same server or container environment where you're running Ghost and listmonk. +Ensure proper network configuration so Ghost can reach your connector, and your connector can reach listmonk. + + +Configuration: + +Set up Ghost to send webhooks to your Go connector service when new subscribers sign up. +Configure the connector with the necessary API details for listmonk. + + + +This approach would be particularly beneficial if you're already comfortable with Go and have the infrastructure in place to host additional services. It provides a lightweight, efficient solution that's fully under your control. +Would you like me to sketch out a basic structure for this Go connector service? Or do you have any specific questions about implementing this approach? CopyRetrylistmonk_apiyamlAExample webhook sent when I registered a user:Copy{ + "member": { + "current": { + "id": "66b68e4d46c2c0000128b809", + "uuid": "c3570109-5572-4ae5-91be-f4bfe2f02653", + "email": "connect@radianceflow.com", + "name": null, + "note": null, + "geolocation": "{\"area_code\":\"0\",\"country_code\":\"ES\",\"country_code3\":\"ESP\",\"continent_code\":\"EU\",\"asn\":13335,\"region\":\"Madrid\",\"latitude\":\"40.4163\",\"longitude\":\"-3.6934\",\"ip\":\"162.158.122.19\",\"organization_name\":\"CLOUDFLARENET\",\"organization\":\"AS13335 CLOUDFLARENET\",\"timezone\":\"Europe/Madrid\",\"city\":\"Madrid\",\"accuracy\":20,\"country\":\"Spain\"}", + "subscribed": true, + "created_at": "2024-08-09T21:46:53.000Z", + "updated_at": "2024-08-09T21:46:53.000Z", + "labels": [], + "subscriptions": [], + "avatar_image": "https://www.gravatar.com/avatar/e7e64e923ef0d0a0967445b2b73c7144?s=250&r=g&d=blank", + "comped": false, + "email_count": 0, + "email_opened_count": 0, + "email_open_rate": null, + "status": "free", + "last_seen_at": null, + "tiers": [], + "newsletters": [ + { + "id": "66b64b106243d80001ac9843", + "name": "troneras", + "description": null, + "status": "active" + } + ] + }, + "previous": {} + } +}We will need a service that can be deployed with docker compose, it should handle the webhooks from ghost, so it should have a custom server listening for them for example on service:8808/webhookIt should also have the domain where listmonk is in order to call it's API. Management will be done from a web interface. So we need to choose carefully the go framework. For authentication on the webui we can use basic auth or similar and configure this via env variables. The functionality of the UI will be as follows:Similar to Zapier, the user will be able to create multiple Zaps, lets call it something else to avoid branding conflicts, we could call it Sons, from Sonnet ;) In the Zap, sorry, I mean in the Son, he will do the following: +1. Give it a name +2. Select the trigger, (Event) +It will be one of the following: +* Member Created (Triggers when a new member is added) +* Member Deleted (Triggerst when a member is deleted) +* Member Updated (Triggers when a member is updated) +* Page Published (Triggers when a page is published) +* Post Published (Triggers when a post is published) +* Post Scheduled (Triggers when a post is scheduled) +1. Select if it should be inmediate or with a delay. Adding a delay allows users to create email sequences, that can be sent out at different intervals: a welcome email right away, a survey request five days later, and an exclusive offer one month after that. +2. Select the action. (can be multiple) + 1. sending a transactional email, using a template from listmonk (we will need to retrieve the templates from the api so the user can see the name from a dropdown) + 2. Creating/deleting a subscriber. Adding subscriber to a list + 3. Creating a campaign (will dig into this later, but probably we will need to select a template, a list to send it to, a date to send it, and we can probably pass some data to replace in the listmonk template placeholders) +3. Can you create the above application step by step, it doesn't need to be in one go.Certainly! I'll guide you through creating this application step by step. Let's start with the basic structure and gradually build up the functionality. +Step 1: Project Setup +Let's begin by setting up the project structure and choosing a Go web framework. For this application, we'll use the Gin framework, which is lightweight, fast, and has good support for API development. +Project SetupClick to open code +Now, let's create a basic main.go file to set up our Gin server: +main.goClick to open code +This sets up a basic Gin server with a webhook endpoint. We'll expand on this as we add more functionality. +Step 2: Docker Setup +Let's create a Dockerfile and a docker-compose.yml file for our application: +DockerfileClick to open code +Now, let's create a docker-compose.yml file: +docker-compose.ymlClick to open code +This docker-compose file sets up our application, Listmonk, and a PostgreSQL database for Listmonk. +Step 3: Basic Authentication +Let's implement basic authentication for our web UI. We'll create a middleware for this: +middleware/auth.goClick to open code +Now, let's update our main.go to use this middleware: +main.goClick to open code +This sets up a basic structure for our application with authentication for the web UI. In the next steps, we'll implement the core functionality for handling webhooks, managing "Sons" (our Zap-like automations), and interacting with the Listmonk API. +Let me know if you want to proceed with the next steps or if you need any clarification on what we've done so far. CopyRetryAready EditGreat! Let's move on to implementing the core functionality of our application. +Step 4: Implement Son (Automation) Model +First, we'll create a model for our Son (automation) structure. This will define how we store and manage our automations. +models/son.goClick to open code +Step 5: Implement Son Storage +Now, let's create a simple storage mechanism for our Sons. For simplicity, we'll use in-memory storage, but in a production environment, you'd want to use a database. +services/son_storage.goClick to open code +Step 6: Implement Son Handlers +Now, let's create handlers for CRUD operations on Sons. +handlers/son_handlers.goClick to open code +Step 7: Update main.go to include Son routes +Let's update our main.go file to include the new Son routes: +main.goClick to open code +These steps set up the basic structure for managing our Sons (automations). In the next steps, we'll implement the webhook handling logic and the Listmonk API integration to actually perform the actions defined in the Sons. +Would you like to proceed with implementing the webhook handling and Listmonk integration, or do you have any questions about what we've done so far? CopyRetryAcontinue EditCertainly! Let's move on to implementing the webhook handling and Listmonk integration. +Step 8: Implement Webhook Handling +First, we'll create a handler for processing the webhooks from Ghost. We'll need to parse the incoming webhook data and trigger the appropriate Sons based on the event type. +handlers/webhook_handler.goClick to open code +Step 9: Implement Listmonk API Client +Now, let's create a simple Listmonk API client to interact with Listmonk: +services/listmonk_client.goClick to open code +Step 10: Implement Son Executor +Now, let's create a service to execute the actions defined in a Son: +services/son_executor.goClick to open code +Step 11: Update main.go to include the new components +Let's update our main.go file to include the new webhook handler and Listmonk client: +main.goClick to open code +With these changes, we've implemented the core functionality of our Ghost-Listmonk connector. The application can now: + +Receive webhooks from Ghost +Process the webhooks and determine the trigger type +Find relevant Sons based on the trigger type +Execute the actions defined in the Sons, including sending transactional emails, managing subscribers, and creating campaigns in Listmonk + +The next steps would be to: + +Implement error handling and logging throughout the application +Add more comprehensive tests +Implement a user interface for managing Sons +Add more sophisticated scheduling and retry mechanisms for action execution +Implement caching and performance optimizations + +Would you like to focus on any of these areas next, or do you have any questions about the implementation so far? CopyRetryALet's continue with the next step EditCertainly! Let's focus on implementing error handling and logging throughout the application. This will make our application more robust and easier to debug. +Step 12: Implement Logging +First, let's create a custom logger that we can use throughout our application: +utils/logger.goClick to open code +Now, let's update our services and handlers to use this logger and implement better error handling. +Step 13: Update Son Storage with Logging and Error Handling +services/son_storage.goClick to open code +Step 14: Update Listmonk Client with Logging and Error Handling +services/listmonk_client.goClick to open code +Step 15: Update Son Executor with Logging and Error Handling +services/son_executor.goClick to open code +Step 16: Update Webhook Handler with Logging and Error Handling +handlers/webhook_handler.goClick to open code CopyRetryClaude’s response was limited as it hit the maximum length allowed at this time.Acontinue with step 16 EditCertainly! Let's continue with step 16, updating the Webhook Handler with logging and error handling. +handlers/webhook_handler.goClick to open code +Now, let's add a utility function to pretty print JSON for logging purposes: +utils/pretty_print.goClick to open code +Step 17: Update Son Handler with Logging and Error Handling +handlers/son_handlers.goClick to open code +Step 18: Update main.go to use the new logger +Finally, let's update our main.go file to use the new logger: +main.goClick to open code +With these changes, we've significantly improved our application's error handling and logging. This will make it easier to debug issues and monitor the application's behavior in production. +The next steps could include: + +Implementing comprehensive tests for all components +Creating a user interface for managing Sons +Adding more sophisticated scheduling and retry mechanisms for action execution +Implementing caching and performance optimizations + +Would you like to focus on any of these areas next, or do you have any other specific improvements in mind? CopyRetryAThere's an error on webhook_handler in this linereturn "", utils.ErrorLogger.NewError("UnknownTriggerType", "Unable to determine trigger type from webhook data") EditI apologize for the error. You're right, and thank you for catching that. The ErrorLogger doesn't have a NewError method. Let's correct this by creating a custom error type and using it in the webhook_handler.go file. +First, let's create a new file for custom errors: +utils/errors.goClick to open code +Now, let's update the webhook_handler.go file to use this new custom error: +handlers/webhook_handler.goClick to open code +This correction ensures that we're creating and returning a proper custom error when we can't determine the trigger type. The utils.NewError function creates a new CustomError with the given code and message. +When using this error in other parts of the code, you can type assert it to access the Code and Message fields if needed: +goCopyif err, ok := err.(*utils.CustomError); ok { + utils.ErrorLogger.Errorf("Custom error occurred: %s - %s", err.Code, err.Message) +} else { + utils.ErrorLogger.Errorf("An error occurred: %v", err) +} +This approach provides more structured error handling and allows you to include additional context (the error code) when logging or responding to errors. \ No newline at end of file diff --git a/repopack-output-backend-go.txt b/repopack-output-backend-go.txt new file mode 100644 index 0000000..94aa619 --- /dev/null +++ b/repopack-output-backend-go.txt @@ -0,0 +1,1634 @@ +================================================================ +Repopack Output File +================================================================ + +This file was generated by Repopack on: 2024-08-12T00:02:56.842Z + +Purpose: +-------- +This file contains a packed representation of the entire repository's contents. +It is designed to be easily consumable by AI systems for analysis, code review, +or other automated processes. + +File Format: +------------ +The content is organized as follows: +1. This header section +2. Repository structure +3. Multiple file entries, each consisting of: + a. A separator line (================) + b. The file path (File: path/to/file) + c. Another separator line + d. The full contents of the file + e. A blank line + +Usage Guidelines: +----------------- +1. This file should be treated as read-only. Any changes should be made to the + original repository files, not this packed version. +2. When processing this file, use the separators and "File:" markers to + distinguish between different files in the repository. +3. Be aware that this file may contain sensitive information. Handle it with + the same level of security as you would the original repository. + +Notes: +------ +- Some files may have been excluded based on .gitignore rules and Repopack's + configuration. +- Binary files are not included in this packed representation. Please refer to + the Repository Structure section for a complete list of file paths, including + binary files. + + + +For more information about Repopack, visit: https://github.com/yamadashy/repopack + +================================================================ +Repository Structure +================================================================ +handlers/ + son_handlers.go + webhook_handler.go +middleware/ + auth.go +models/ + son.go +services/ + database.go + listmonk_client_test.go + listmonk_client.go + son_executor.go + son_storage_test.go + son_storage.go +utils/ + config.go + errors.go + logger.go + pretty_print.go + uuid.go +.env.local +docker-compose.yml +Dockerfile +go.mod +main.go + +================================================================ +Repository Files +================================================================ + +================ +File: handlers/son_handlers.go +================ +package handlers + +import ( + "net/http" + + "github.com/gin-gonic/gin" + "github.com/troneras/ghost-listmonk-connector/models" + "github.com/troneras/ghost-listmonk-connector/services" + "github.com/troneras/ghost-listmonk-connector/utils" +) + +type SonHandler struct { + storage *services.SonStorage +} + +type ListmonkHandler struct { + client *services.ListmonkClient +} + +func NewSonHandler(storage *services.SonStorage) *SonHandler { + return &SonHandler{storage: storage} +} + +func NewListmonkHandler(client *services.ListmonkClient) *ListmonkHandler { + return &ListmonkHandler{client: client} +} + +func (h *ListmonkHandler) GetLists(c *gin.Context) { + lists, err := h.client.GetLists() + if err != nil { + utils.ErrorLogger.Errorf("Failed to get lists: %v", err) + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + + c.JSON(http.StatusOK, gin.H{"data": lists}) +} + +func (h *ListmonkHandler) GetTemplates(c *gin.Context) { + templates, err := h.client.GetTemplates() + if err != nil { + utils.ErrorLogger.Errorf("Failed to get templates: %v", err) + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + + c.JSON(http.StatusOK, gin.H{"data": templates}) +} + +func (h *SonHandler) Create(c *gin.Context) { + var son models.Son + if err := c.ShouldBindJSON(&son); err != nil { + utils.ErrorLogger.Errorf("Invalid Son data: %v", err) + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + if err := h.storage.Create(&son); err != nil { + utils.ErrorLogger.Errorf("Failed to create Son: %v", err) + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + + utils.InfoLogger.Infof("Created Son: %s", utils.PrettyPrint(son)) + c.JSON(http.StatusCreated, son) +} + +func (h *SonHandler) Get(c *gin.Context) { + id := c.Param("id") + son, err := h.storage.Get(id) + if err != nil { + if err == services.ErrSonNotFound { + utils.ErrorLogger.Errorf("Son not found: %s", id) + c.JSON(http.StatusNotFound, gin.H{"error": "Son not found"}) + } else { + utils.ErrorLogger.Errorf("Failed to get Son: %v", err) + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + } + return + } + + utils.InfoLogger.Infof("Retrieved Son: %s", utils.PrettyPrint(son)) + c.JSON(http.StatusOK, son) +} + +func (h *SonHandler) Update(c *gin.Context) { + id := c.Param("id") + var son models.Son + if err := c.ShouldBindJSON(&son); err != nil { + utils.ErrorLogger.Errorf("Invalid Son data: %v", err) + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + son.ID = id + if err := h.storage.Update(son); err != nil { + if err == services.ErrSonNotFound { + utils.ErrorLogger.Errorf("Son not found for update: %s", id) + c.JSON(http.StatusNotFound, gin.H{"error": "Son not found"}) + } else { + utils.ErrorLogger.Errorf("Failed to update Son: %v", err) + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + } + return + } + + utils.InfoLogger.Infof("Updated Son: %s", utils.PrettyPrint(son)) + c.JSON(http.StatusOK, son) +} + +func (h *SonHandler) Delete(c *gin.Context) { + id := c.Param("id") + if err := h.storage.Delete(id); err != nil { + if err == services.ErrSonNotFound { + utils.ErrorLogger.Errorf("Son not found for deletion: %s", id) + c.JSON(http.StatusNotFound, gin.H{"error": "Son not found"}) + } else { + utils.ErrorLogger.Errorf("Failed to delete Son: %v", err) + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + } + return + } + + utils.InfoLogger.Infof("Deleted Son: %s", id) + c.JSON(http.StatusOK, gin.H{"message": "Son deleted successfully"}) +} + +func (h *SonHandler) List(c *gin.Context) { + sons, err := h.storage.List() + + if err != nil { + utils.ErrorLogger.Errorf("Failed to list Sons: %v", err) + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + + utils.InfoLogger.Infof("Retrieved %d Sons", len(sons)) + c.JSON(http.StatusOK, sons) +} + +================ +File: handlers/webhook_handler.go +================ +package handlers + +import ( + "net/http" + + "github.com/gin-gonic/gin" + "github.com/troneras/ghost-listmonk-connector/models" + "github.com/troneras/ghost-listmonk-connector/services" + "github.com/troneras/ghost-listmonk-connector/utils" +) + +type WebhookHandler struct { + sonStorage *services.SonStorage + executor *services.SonExecutor +} + +func NewWebhookHandler(sonStorage *services.SonStorage, executor *services.SonExecutor) *WebhookHandler { + return &WebhookHandler{ + sonStorage: sonStorage, + executor: executor, + } +} + +func (h *WebhookHandler) HandleWebhook(c *gin.Context) { + var webhookData map[string]interface{} + if err := c.ShouldBindJSON(&webhookData); err != nil { + utils.ErrorLogger.Errorf("Invalid webhook data: %v", err) + c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid webhook data"}) + return + } + + utils.InfoLogger.Infof("Received webhook: %s", utils.PrettyPrint(webhookData)) + + triggerType, err := determineTriggerType(webhookData) + if err != nil { + utils.ErrorLogger.Errorf("Unable to determine trigger type: %v", err) + c.JSON(http.StatusBadRequest, gin.H{"error": "Unable to determine trigger type"}) + return + } + + utils.InfoLogger.Infof("Determined trigger type: %s", triggerType) + + // Find and execute relevant Sons + sons, err := h.sonStorage.List() + + if err != nil { + utils.ErrorLogger.Errorf("Failed to list Sons: %v", err) + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to list Sons"}) + return + } + + executedCount := 0 + for _, son := range sons { + if son.Trigger == triggerType { + go func(s models.Son) { + utils.InfoLogger.Infof("Executing Son %s for trigger %s", s.ID, triggerType) + h.executor.ExecuteSon(s, webhookData) + }(son) + executedCount++ + } + } + + utils.InfoLogger.Infof("Executed %d Sons for trigger %s", executedCount, triggerType) + c.JSON(http.StatusOK, gin.H{"message": "Webhook processed successfully", "sons_executed": executedCount}) +} + +func determineTriggerType(webhookData map[string]interface{}) (models.TriggerType, error) { + if member, ok := webhookData["member"].(map[string]interface{}); ok { + if _, ok := member["current"]; ok { + if _, ok := member["previous"]; ok { + return models.TriggerMemberUpdated, nil + } + return models.TriggerMemberCreated, nil + } + // Assuming member deletion doesn't have 'current' field + return models.TriggerMemberDeleted, nil + } + + if post, ok := webhookData["post"].(map[string]interface{}); ok { + if status, ok := post["status"].(string); ok && status == "published" { + return models.TriggerPostPublished, nil + } + if status, ok := post["status"].(string); ok && status == "scheduled" { + return models.TriggerPostScheduled, nil + } + } + + if page, ok := webhookData["page"].(map[string]interface{}); ok { + if status, ok := page["status"].(string); ok && status == "published" { + return models.TriggerPagePublished, nil + } + } + + return "", utils.NewError("UnknownTriggerType", "Unable to determine trigger type from webhook data") +} + +================ +File: middleware/auth.go +================ +package middleware + +import ( + "net/http" + + "github.com/gin-gonic/gin" +) + +func APIKeyAuth(validAPIKey string) gin.HandlerFunc { + return func(c *gin.Context) { + apiKey := c.GetHeader("Authorization") + if apiKey == "" { + apiKey = c.Query("api_key") + } + + if apiKey != "Bearer "+validAPIKey { + c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid API key"}) + c.Abort() + return + } + + c.Next() + } +} + +================ +File: models/son.go +================ +package models + +import ( + "encoding/json" + "errors" + "time" +) + +type TriggerType string +type ActionType string + +const ( + TriggerMemberCreated TriggerType = "member_created" + TriggerMemberDeleted TriggerType = "member_deleted" + TriggerMemberUpdated TriggerType = "member_updated" + TriggerPagePublished TriggerType = "page_published" + TriggerPostPublished TriggerType = "post_published" + TriggerPostScheduled TriggerType = "post_scheduled" +) + +const ( + ActionSendTransactionalEmail ActionType = "send_transactional_email" + ActionManageSubscriber ActionType = "manage_subscriber" + ActionCreateCampaign ActionType = "create_campaign" +) + +type Son struct { + ID string `json:"id"` + Name string `json:"name"` + Trigger TriggerType `json:"trigger"` + Delay Duration `json:"delay"` + Actions []Action `json:"actions"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` +} + +type Action struct { + Type ActionType `json:"type"` + Parameters map[string]any `json:"parameters"` +} + +// Duration is a custom type to handle time.Duration in JSON +type Duration time.Duration + +func (d Duration) MarshalJSON() ([]byte, error) { + return json.Marshal(time.Duration(d).Minutes()) +} + +func (d *Duration) UnmarshalJSON(b []byte) error { + var v interface{} + if err := json.Unmarshal(b, &v); err != nil { + return err + } + switch value := v.(type) { + case float64: + *d = Duration(time.Duration(value) * time.Minute) + return nil + case string: + tmp, err := time.ParseDuration(value) + if err != nil { + return err + } + *d = Duration(tmp) + return nil + default: + return errors.New("invalid duration") + } +} + +================ +File: services/database.go +================ +package services + +import ( + "database/sql" + "encoding/json" + "time" + + _ "github.com/mattn/go-sqlite3" + "github.com/troneras/ghost-listmonk-connector/models" + "github.com/troneras/ghost-listmonk-connector/utils" +) + +type Database struct { + db *sql.DB +} + +func NewDatabase(dbPath string) (*Database, error) { + db, err := sql.Open("sqlite3", dbPath) + if err != nil { + return nil, err + } + + if err := db.Ping(); err != nil { + return nil, err + } + + database := &Database{db: db} + if err := database.createTables(); err != nil { + return nil, err + } + + return database, nil +} + +func (d *Database) createTables() error { + _, err := d.db.Exec(` + CREATE TABLE IF NOT EXISTS sons ( + id TEXT PRIMARY KEY, + name TEXT, + trigger TEXT, + delay INTEGER, + created_at DATETIME, + updated_at DATETIME + ); + CREATE TABLE IF NOT EXISTS actions ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + son_id TEXT, + type TEXT, + parameters TEXT, + FOREIGN KEY (son_id) REFERENCES sons(id) + ); + `) + return err +} + +func (d *Database) CreateSon(son *models.Son) error { + son.ID = utils.GenerateUUID() + son.CreatedAt = time.Now() + son.UpdatedAt = time.Now() + + tx, err := d.db.Begin() + if err != nil { + return err + } + defer tx.Rollback() + + _, err = tx.Exec( + "INSERT INTO sons (id, name, trigger, delay, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)", + son.ID, son.Name, son.Trigger, int64(son.Delay), son.CreatedAt, son.UpdatedAt, + ) + if err != nil { + return err + } + + for _, action := range son.Actions { + parametersJSON, err := json.Marshal(action.Parameters) + if err != nil { + return err + } + _, err = tx.Exec( + "INSERT INTO actions (son_id, type, parameters) VALUES (?, ?, ?)", + son.ID, action.Type, string(parametersJSON), + ) + if err != nil { + return err + } + } + + return tx.Commit() +} + +func (d *Database) GetSon(id string) (*models.Son, error) { + son := &models.Son{} + err := d.db.QueryRow( + "SELECT id, name, trigger, delay, created_at, updated_at FROM sons WHERE id = ?", + id, + ).Scan(&son.ID, &son.Name, &son.Trigger, &son.Delay, &son.CreatedAt, &son.UpdatedAt) + if err != nil { + if err == sql.ErrNoRows { + return nil, ErrSonNotFound + } + return nil, err + } + + rows, err := d.db.Query("SELECT type, parameters FROM actions WHERE son_id = ?", id) + if err != nil { + return nil, err + } + defer rows.Close() + + for rows.Next() { + var action models.Action + var parametersJSON string + err := rows.Scan(&action.Type, ¶metersJSON) + if err != nil { + return nil, err + } + err = json.Unmarshal([]byte(parametersJSON), &action.Parameters) + if err != nil { + return nil, err + } + son.Actions = append(son.Actions, action) + } + + return son, nil +} + +func (d *Database) UpdateSon(son *models.Son) error { + son.UpdatedAt = time.Now() + + tx, err := d.db.Begin() + if err != nil { + return err + } + defer tx.Rollback() + + _, err = tx.Exec( + "UPDATE sons SET name = ?, trigger = ?, delay = ?, updated_at = ? WHERE id = ?", + son.Name, son.Trigger, int64(son.Delay), son.UpdatedAt, son.ID, + ) + if err != nil { + return err + } + + _, err = tx.Exec("DELETE FROM actions WHERE son_id = ?", son.ID) + if err != nil { + return err + } + + for _, action := range son.Actions { + parametersJSON, err := json.Marshal(action.Parameters) + if err != nil { + return err + } + _, err = tx.Exec( + "INSERT INTO actions (son_id, type, parameters) VALUES (?, ?, ?)", + son.ID, action.Type, string(parametersJSON), + ) + if err != nil { + return err + } + } + + return tx.Commit() +} + +func (d *Database) DeleteSon(id string) error { + tx, err := d.db.Begin() + if err != nil { + return err + } + defer tx.Rollback() + + _, err = tx.Exec("DELETE FROM actions WHERE son_id = ?", id) + if err != nil { + return err + } + + result, err := tx.Exec("DELETE FROM sons WHERE id = ?", id) + if err != nil { + return err + } + + affected, err := result.RowsAffected() + if err != nil { + return err + } + if affected == 0 { + return ErrSonNotFound + } + + return tx.Commit() +} + +func (d *Database) ListSons() ([]models.Son, error) { + rows, err := d.db.Query("SELECT id, name, trigger, delay, created_at, updated_at FROM sons") + if err != nil { + return nil, err + } + defer rows.Close() + + var sons []models.Son + for rows.Next() { + var son models.Son + err := rows.Scan(&son.ID, &son.Name, &son.Trigger, &son.Delay, &son.CreatedAt, &son.UpdatedAt) + if err != nil { + return nil, err + } + sons = append(sons, son) + } + + for i, son := range sons { + actionRows, err := d.db.Query("SELECT type, parameters FROM actions WHERE son_id = ?", son.ID) + if err != nil { + return nil, err + } + defer actionRows.Close() + + for actionRows.Next() { + var action models.Action + var parametersJSON string + err := actionRows.Scan(&action.Type, ¶metersJSON) + if err != nil { + return nil, err + } + err = json.Unmarshal([]byte(parametersJSON), &action.Parameters) + if err != nil { + return nil, err + } + sons[i].Actions = append(sons[i].Actions, action) + } + } + + return sons, nil +} + +================ +File: services/listmonk_client_test.go +================ +package services + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestListmonkClient(t *testing.T) { + // Create a mock server + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case "/api/tx": + w.WriteHeader(http.StatusOK) + w.Write([]byte(`{"data": true}`)) + case "/api/subscribers": + w.WriteHeader(http.StatusOK) + w.Write([]byte(`{"data": {"id": 1}}`)) + case "/api/campaigns": + w.WriteHeader(http.StatusOK) + w.Write([]byte(`{"data": {"id": 1}}`)) + default: + http.Error(w, "Not found", http.StatusNotFound) + } + })) + defer server.Close() + + // Create a client that uses the mock server URL + client := &ListmonkClient{ + baseURL: server.URL, + client: server.Client(), + } + + // Test SendTransactionalEmail + err := client.SendTransactionalEmail(1, "test@example.com", map[string]interface{}{"name": "Test"}) + assert.NoError(t, err) + + // Test ManageSubscriber + err = client.ManageSubscriber("test@example.com", "Test User", "enabled", []int{1}) + assert.NoError(t, err) + + // Test CreateCampaign + err = client.CreateCampaign("Test Campaign", "Test Subject", []int{1}, 1, "2023-01-01T00:00:00Z") + assert.NoError(t, err) +} + +================ +File: services/listmonk_client.go +================ +package services + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "net/http" + + "github.com/troneras/ghost-listmonk-connector/utils" +) + +type ListmonkList struct { + ID int `json:"id"` + Name string `json:"name"` +} + +type ListmonkTemplate struct { + ID int `json:"id"` + Name string `json:"name"` +} + +type ListmonkClient struct { + baseURL string + client *http.Client +} + +func NewListmonkClient(config *utils.Config) *ListmonkClient { + return &ListmonkClient{ + baseURL: config.ListmonkURL, + client: &http.Client{}, + } +} + +func (c *ListmonkClient) GetLists() ([]ListmonkList, error) { + resp, err := c.client.Get(c.baseURL + "/api/lists?page=1&per_page=100") + if err != nil { + return nil, fmt.Errorf("error fetching lists: %w", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode) + } + + var result struct { + Data struct { + Results []ListmonkList `json:"results"` + } `json:"data"` + } + if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { + return nil, fmt.Errorf("error decoding response: %w", err) + } + + return result.Data.Results, nil +} + +func (c *ListmonkClient) GetTemplates() ([]ListmonkTemplate, error) { + resp, err := c.client.Get(c.baseURL + "/api/templates?page=1&per_page=100") + if err != nil { + return nil, fmt.Errorf("error fetching templates: %w", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode) + } + + var result struct { + Data []ListmonkTemplate `json:"data"` + } + if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { + return nil, fmt.Errorf("error decoding response: %w", err) + } + + return result.Data, nil +} + +func (c *ListmonkClient) SendTransactionalEmail(templateID int, subscriberEmail string, data map[string]interface{}) error { + payload := map[string]interface{}{ + "subscriber_email": subscriberEmail, + "template_id": templateID, + "data": data, + } + + jsonPayload, err := json.Marshal(payload) + if err != nil { + utils.ErrorLogger.Errorf("Failed to marshal payload: %v", err) + return fmt.Errorf("failed to marshal payload: %w", err) + } + + resp, err := c.client.Post(c.baseURL+"/api/tx", "application/json", bytes.NewBuffer(jsonPayload)) + if err != nil { + utils.ErrorLogger.Errorf("Failed to send transactional email: %v", err) + return fmt.Errorf("failed to send transactional email: %w", err) + } + defer resp.Body.Close() + + body, _ := io.ReadAll(resp.Body) + if resp.StatusCode != http.StatusOK { + utils.ErrorLogger.Errorf("Unexpected status code: %d, body: %s", resp.StatusCode, string(body)) + return fmt.Errorf("unexpected status code: %d, body: %s", resp.StatusCode, string(body)) + } + + utils.InfoLogger.Infof("Sent transactional email to %s using template %d", subscriberEmail, templateID) + return nil +} + +func (c *ListmonkClient) ManageSubscriber(email string, name string, status string, lists []int) error { + payload := map[string]interface{}{ + "email": email, + "name": name, + "status": status, + "lists": lists, + "preconfirm_subscriptions": true, + } + + jsonPayload, err := json.Marshal(payload) + if err != nil { + utils.ErrorLogger.Errorf("Failed to marshal payload: %v", err) + return fmt.Errorf("failed to marshal payload: %w", err) + } + + resp, err := c.client.Post(c.baseURL+"/api/subscribers", "application/json", bytes.NewBuffer(jsonPayload)) + if err != nil { + utils.ErrorLogger.Errorf("Failed to manage subscriber: %v", err) + return fmt.Errorf("failed to manage subscriber: %w", err) + } + defer resp.Body.Close() + + body, _ := io.ReadAll(resp.Body) + if resp.StatusCode != http.StatusOK { + utils.ErrorLogger.Errorf("Unexpected status code: %d, body: %s", resp.StatusCode, string(body)) + return fmt.Errorf("unexpected status code: %d, body: %s", resp.StatusCode, string(body)) + } + + utils.InfoLogger.Infof("Managed subscriber %s with status %s", email, status) + return nil +} + +func (c *ListmonkClient) CreateCampaign(name string, subject string, lists []int, templateID int, sendAt string) error { + payload := map[string]interface{}{ + "name": name, + "subject": subject, + "lists": lists, + "template_id": templateID, + "send_at": sendAt, + } + + jsonPayload, err := json.Marshal(payload) + if err != nil { + utils.ErrorLogger.Errorf("Failed to marshal payload: %v", err) + return fmt.Errorf("failed to marshal payload: %w", err) + } + + resp, err := c.client.Post(c.baseURL+"/api/campaigns", "application/json", bytes.NewBuffer(jsonPayload)) + if err != nil { + utils.ErrorLogger.Errorf("Failed to create campaign: %v", err) + return fmt.Errorf("failed to create campaign: %w", err) + } + defer resp.Body.Close() + + body, _ := io.ReadAll(resp.Body) + if resp.StatusCode != http.StatusOK { + utils.ErrorLogger.Errorf("Unexpected status code: %d, body: %s", resp.StatusCode, string(body)) + return fmt.Errorf("unexpected status code: %d, body: %s", resp.StatusCode, string(body)) + } + + utils.InfoLogger.Infof("Created campaign %s with subject %s", name, subject) + return nil +} + +================ +File: services/son_executor.go +================ +package services + +import ( + "fmt" + "time" + + "github.com/troneras/ghost-listmonk-connector/models" + "github.com/troneras/ghost-listmonk-connector/utils" +) + +type SonExecutor struct { + listmonkClient *ListmonkClient +} + +func NewSonExecutor(listmonkClient *ListmonkClient) *SonExecutor { + return &SonExecutor{ + listmonkClient: listmonkClient, + } +} + +func (e *SonExecutor) ExecuteSon(son models.Son, data map[string]interface{}) { + utils.InfoLogger.Infof("Executing Son: %s", son.ID) + + if son.Delay > 0 { + utils.InfoLogger.Infof("Delaying execution of Son %s for %v", son.ID, time.Duration(son.Delay)) + time.Sleep(time.Duration(son.Delay)) + } + + for _, action := range son.Actions { + err := e.executeAction(action, data) + if err != nil { + utils.ErrorLogger.Errorf("Error executing action for Son %s: %v", son.ID, err) + } + } + + utils.InfoLogger.Infof("Finished executing Son: %s", son.ID) +} + +func (e *SonExecutor) executeAction(action models.Action, data map[string]interface{}) error { + utils.InfoLogger.Infof("Executing action: %s", action.Type) + + switch action.Type { + case models.ActionSendTransactionalEmail: + return e.sendTransactionalEmail(action.Parameters, data) + case models.ActionManageSubscriber: + return e.manageSubscriber(action.Parameters, data) + case models.ActionCreateCampaign: + return e.createCampaign(action.Parameters, data) + default: + err := fmt.Errorf("unknown action type: %s", action.Type) + utils.ErrorLogger.Errorf("%v", err) + return err + } +} + +func (e *SonExecutor) sendTransactionalEmail(params map[string]interface{}, data map[string]interface{}) error { + templateID := int(params["template_id"].(float64)) + subscriberEmail := data["member"].(map[string]interface{})["current"].(map[string]interface{})["email"].(string) + + utils.InfoLogger.Infof("Sending transactional email to %s using template %d", subscriberEmail, templateID) + return e.listmonkClient.SendTransactionalEmail(templateID, subscriberEmail, data) +} + +func (e *SonExecutor) manageSubscriber(params map[string]interface{}, data map[string]interface{}) error { + email := data["member"].(map[string]interface{})["current"].(map[string]interface{})["email"].(string) + name := data["member"].(map[string]interface{})["current"].(map[string]interface{})["name"].(string) + status := params["status"].(string) + lists := []int{} + for _, listID := range params["lists"].([]interface{}) { + lists = append(lists, int(listID.(float64))) + } + + utils.InfoLogger.Infof("Managing subscriber %s with status %s", email, status) + return e.listmonkClient.ManageSubscriber(email, name, status, lists) +} + +func (e *SonExecutor) createCampaign(params map[string]interface{}, data map[string]interface{}) error { + name := params["name"].(string) + subject := params["subject"].(string) + lists := []int{} + for _, listID := range params["lists"].([]interface{}) { + lists = append(lists, int(listID.(float64))) + } + templateID := int(params["template_id"].(float64)) + sendAt := params["send_at"].(string) + + utils.InfoLogger.Infof("Creating campaign %s with subject %s", name, subject) + return e.listmonkClient.CreateCampaign(name, subject, lists, templateID, sendAt) +} + +================ +File: services/son_storage_test.go +================ +package services + +import ( + "os" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/troneras/ghost-listmonk-connector/models" +) + +func TestSonStorage(t *testing.T) { + // Create a temporary database for testing + tempDBPath := "test_ghost_listmonk.db" + storage, err := NewSonStorage(tempDBPath) + assert.NoError(t, err) + defer os.Remove(tempDBPath) // Clean up the temporary database file after tests + + // Test Create + son := models.Son{ + Name: "Test Son", + Trigger: models.TriggerMemberCreated, + Delay: models.Duration(5 * time.Minute), + Actions: []models.Action{ + { + Type: models.ActionSendTransactionalEmail, + Parameters: map[string]interface{}{ + "template_id": float64(1), // Use float64 to match JSON unmarshaling behavior + }, + }, + }, + } + + err = storage.Create(&son) + assert.NoError(t, err) + assert.NotEmpty(t, son.ID) // Ensure an ID was generated + + // Test Get + retrievedSon, err := storage.Get(son.ID) + assert.NoError(t, err) + assert.Equal(t, son.Name, retrievedSon.Name) + assert.Equal(t, son.Trigger, retrievedSon.Trigger) + assert.Equal(t, son.Delay, retrievedSon.Delay) + assert.Len(t, retrievedSon.Actions, 1) + assert.Equal(t, son.Actions[0].Type, retrievedSon.Actions[0].Type) + assert.Equal(t, son.Actions[0].Parameters["template_id"], retrievedSon.Actions[0].Parameters["template_id"]) + + // Test Update + son.Name = "Updated Test Son" + err = storage.Update(son) + assert.NoError(t, err) + + updatedSon, err := storage.Get(son.ID) + assert.NoError(t, err) + assert.Equal(t, "Updated Test Son", updatedSon.Name) + + // Test List + sons, err := storage.List() + assert.NoError(t, err) + assert.Len(t, sons, 1) + assert.Equal(t, son.ID, sons[0].ID) + + // Test Delete + err = storage.Delete(son.ID) + assert.NoError(t, err) + + _, err = storage.Get(son.ID) + assert.Error(t, err) + assert.Equal(t, ErrSonNotFound, err) + + // Test error cases + err = storage.Create(&son) + assert.NoError(t, err) + + // Attempt to update a non-existent son (should fail) + nonExistentSon := models.Son{ID: "non-existent", Name: "Non-existent Son"} + err = storage.Update(nonExistentSon) + assert.Equal(t, ErrSonNotFound, err) + + err = storage.Delete("non-existent") + assert.Equal(t, ErrSonNotFound, err) +} + +================ +File: services/son_storage.go +================ +package services + +import ( + "database/sql" + "encoding/json" + "errors" + "time" + + _ "github.com/mattn/go-sqlite3" + "github.com/troneras/ghost-listmonk-connector/models" + "github.com/troneras/ghost-listmonk-connector/utils" +) + +var ( + ErrSonAlreadyExists = errors.New("son with this ID already exists") + ErrSonNotFound = errors.New("son not found") +) + +type SonStorage struct { + db *sql.DB +} + +func NewSonStorage(dbPath string) (*SonStorage, error) { + db, err := sql.Open("sqlite3", dbPath) + if err != nil { + return nil, err + } + + if err := db.Ping(); err != nil { + return nil, err + } + + storage := &SonStorage{db: db} + if err := storage.createTables(); err != nil { + return nil, err + } + + return storage, nil +} + +func (s *SonStorage) createTables() error { + _, err := s.db.Exec(` + CREATE TABLE IF NOT EXISTS sons ( + id TEXT PRIMARY KEY, + name TEXT, + trigger TEXT, + delay INTEGER, + actions TEXT, + created_at DATETIME, + updated_at DATETIME + ) + `) + return err +} + +func (s *SonStorage) Create(son *models.Son) error { + son.ID = utils.GenerateUUID() + son.CreatedAt = time.Now() + son.UpdatedAt = time.Now() + + actionsJSON, err := json.Marshal(son.Actions) + if err != nil { + return err + } + + _, err = s.db.Exec( + "INSERT INTO sons (id, name, trigger, delay, actions, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)", + son.ID, son.Name, son.Trigger, int64(son.Delay), actionsJSON, son.CreatedAt, son.UpdatedAt, + ) + if err != nil { + utils.ErrorLogger.Errorf("Failed to create Son: %v", err) + return err + } + + utils.InfoLogger.Infof("Created new Son with ID: %s", son.ID) + return nil +} + +func (s *SonStorage) Get(id string) (models.Son, error) { + var son models.Son + var actionsJSON []byte + var delayInt int64 + + err := s.db.QueryRow( + "SELECT id, name, trigger, delay, actions, created_at, updated_at FROM sons WHERE id = ?", + id, + ).Scan(&son.ID, &son.Name, &son.Trigger, &delayInt, &actionsJSON, &son.CreatedAt, &son.UpdatedAt) + + if err != nil { + if err == sql.ErrNoRows { + utils.ErrorLogger.Errorf("Failed to get Son: %v", ErrSonNotFound) + return models.Son{}, ErrSonNotFound + } + utils.ErrorLogger.Errorf("Failed to get Son: %v", err) + return models.Son{}, err + } + + son.Delay = models.Duration(time.Duration(delayInt)) + + err = json.Unmarshal(actionsJSON, &son.Actions) + if err != nil { + utils.ErrorLogger.Errorf("Failed to unmarshal actions: %v", err) + return models.Son{}, err + } + + utils.InfoLogger.Infof("Retrieved Son with ID: %s", id) + return son, nil +} + +func (s *SonStorage) Update(son models.Son) error { + son.UpdatedAt = time.Now() + + actionsJSON, err := json.Marshal(son.Actions) + if err != nil { + return err + } + + result, err := s.db.Exec( + "UPDATE sons SET name = ?, trigger = ?, delay = ?, actions = ?, updated_at = ? WHERE id = ?", + son.Name, son.Trigger, int64(son.Delay), actionsJSON, son.UpdatedAt, son.ID, + ) + if err != nil { + utils.ErrorLogger.Errorf("Failed to update Son: %v", err) + return err + } + + rowsAffected, err := result.RowsAffected() + if err != nil { + return err + } + + if rowsAffected == 0 { + utils.ErrorLogger.Errorf("Failed to update Son: %v", ErrSonNotFound) + return ErrSonNotFound + } + + utils.InfoLogger.Infof("Updated Son with ID: %s", son.ID) + return nil +} + +func (s *SonStorage) Delete(id string) error { + result, err := s.db.Exec("DELETE FROM sons WHERE id = ?", id) + if err != nil { + utils.ErrorLogger.Errorf("Failed to delete Son: %v", err) + return err + } + + rowsAffected, err := result.RowsAffected() + if err != nil { + return err + } + + if rowsAffected == 0 { + utils.ErrorLogger.Errorf("Failed to delete Son: %v", ErrSonNotFound) + return ErrSonNotFound + } + + utils.InfoLogger.Infof("Deleted Son with ID: %s", id) + return nil +} + +func (s *SonStorage) List() ([]models.Son, error) { + rows, err := s.db.Query("SELECT id, name, trigger, delay, actions, created_at, updated_at FROM sons") + if err != nil { + utils.ErrorLogger.Errorf("Failed to list Sons: %v", err) + return nil, err + } + defer rows.Close() + + var sons []models.Son + for rows.Next() { + var son models.Son + var actionsJSON []byte + var delayInt int64 + + err := rows.Scan(&son.ID, &son.Name, &son.Trigger, &delayInt, &actionsJSON, &son.CreatedAt, &son.UpdatedAt) + if err != nil { + utils.ErrorLogger.Errorf("Failed to scan Son: %v", err) + continue + } + + son.Delay = models.Duration(time.Duration(delayInt)) + + err = json.Unmarshal(actionsJSON, &son.Actions) + if err != nil { + utils.ErrorLogger.Errorf("Failed to unmarshal actions: %v", err) + continue + } + + sons = append(sons, son) + } + + if sons == nil { + sons = []models.Son{} // Ensure we always return an array, even if empty + } + + utils.InfoLogger.Infof("Retrieved list of %d Sons", len(sons)) + return sons, nil +} + +================ +File: utils/config.go +================ +package utils + +import ( + "bufio" + "fmt" + "os" + "strings" +) + +// Config holds all configuration values +type Config struct { + ListmonkURL string + APIKey string + Port string + // Add other configuration fields as needed +} + +// LoadConfig reads configuration from .env.local file and environment variables +func LoadConfig() (*Config, error) { + config := &Config{} + + // Read from .env.local file + if err := loadEnvFile(".env.local", config); err != nil { + ErrorLogger.Printf("Error loading .env.local file: %v", err) + // Continue execution, as we'll fall back to environment variables + } + + // Override with environment variables if they exist + if envListmonkURL := os.Getenv("LISTMONK_URL"); envListmonkURL != "" { + config.ListmonkURL = envListmonkURL + } + if envAPIKey := os.Getenv("API_KEY"); envAPIKey != "" { + config.APIKey = envAPIKey + } + + if envPort := os.Getenv("API_KEY"); envPort != "" { + config.Port = envPort + } + + // Validate required fields + if config.ListmonkURL == "" { + return nil, fmt.Errorf("LISTMONK_URL is not set") + } + if config.APIKey == "" { + return nil, fmt.Errorf("API_KEY is not set") + } + if config.Port == "" { + config.Port = "8808" + } + + return config, nil +} + +func loadEnvFile(filename string, config *Config) error { + file, err := os.Open(filename) + if err != nil { + return err + } + defer file.Close() + + scanner := bufio.NewScanner(file) + for scanner.Scan() { + line := scanner.Text() + parts := strings.SplitN(line, "=", 2) + if len(parts) != 2 { + continue + } + key := strings.TrimSpace(parts[0]) + value := strings.TrimSpace(parts[1]) + + switch key { + case "LISTMONK_URL": + config.ListmonkURL = value + case "API_KEY": + config.APIKey = value + case "PORT": + config.Port = value + // Add other configuration fields as needed + } + } + + return scanner.Err() +} + +================ +File: utils/errors.go +================ +package utils + +import "fmt" + +type CustomError struct { + Code string + Message string +} + +func (e *CustomError) Error() string { + return fmt.Sprintf("%s: %s", e.Code, e.Message) +} + +func NewError(code, message string) *CustomError { + return &CustomError{ + Code: code, + Message: message, + } +} + +================ +File: utils/logger.go +================ +package utils + +import ( + "log" + "os" +) + +type Logger struct { + *log.Logger +} + +var ( + InfoLogger *Logger + ErrorLogger *Logger +) + +func init() { + InfoLogger = &Logger{log.New(os.Stdout, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile)} + ErrorLogger = &Logger{log.New(os.Stderr, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile)} +} + +func (l *Logger) Infof(format string, v ...interface{}) { + l.Printf(format, v...) +} + +func (l *Logger) Errorf(format string, v ...interface{}) { + l.Printf(format, v...) +} + +================ +File: utils/pretty_print.go +================ +package utils + +import ( + "encoding/json" +) + +func PrettyPrint(v interface{}) string { + b, err := json.MarshalIndent(v, "", " ") + if err != nil { + return "" + } + return string(b) +} + +================ +File: utils/uuid.go +================ +package utils + +import ( + "github.com/google/uuid" +) + +func GenerateUUID() string { + return uuid.New().String() +} + +================ +File: .env.local +================ +LISTMONK_URL=http://localhost:9000 +NEXT_APP_URL=http://localhost:3000 +BASIC_AUTH_USERNAME=username +BASIC_AUTH_PASSWORD=password +API_KEY=your-api-key + +================ +File: docker-compose.yml +================ +version: '3' +services: + app: + build: . + ports: + - "8808:8808" + environment: + - PORT=8808 + - LISTMONK_URL=http://listmonk:9000 + - BASIC_AUTH_USERNAME=admin + - BASIC_AUTH_PASSWORD=password + depends_on: + - listmonk + + listmonk: + image: listmonk/listmonk:latest + ports: + - "9000:9000" + environment: + - APP_DB_HOST=db + - APP_DB_USER=listmonk + - APP_DB_PASSWORD=listmonk + depends_on: + - db + + db: + image: postgres:13 + environment: + - POSTGRES_USER=listmonk + - POSTGRES_PASSWORD=listmonk + - POSTGRES_DB=listmonk + volumes: + - listmonk-data:/var/lib/postgresql/data + +volumes: + listmonk-data: + +================ +File: Dockerfile +================ +FROM golang:1.17-alpine AS builder + +WORKDIR /app + +COPY go.mod go.sum ./ +RUN go mod download + +COPY . . + +RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main . + +FROM alpine:latest + +RUN apk --no-cache add ca-certificates + +WORKDIR /root/ + +COPY --from=builder /app/main . + +EXPOSE 8808 + +CMD ["./main"] + +================ +File: go.mod +================ +module github.com/troneras/ghost-listmonk-connector + +go 1.21.6 + +require ( + github.com/gin-contrib/cors v1.7.2 + github.com/gin-gonic/gin v1.10.0 + github.com/google/uuid v1.6.0 + github.com/mattn/go-sqlite3 v1.14.22 + github.com/stretchr/testify v1.9.0 +) + +require ( + github.com/bytedance/sonic v1.12.1 // indirect + github.com/bytedance/sonic/loader v0.2.0 // indirect + github.com/cloudwego/base64x v0.1.4 // indirect + github.com/cloudwego/iasm v0.2.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/gabriel-vasile/mimetype v1.4.5 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.22.0 // indirect + github.com/goccy/go-json v0.10.3 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/cpuid/v2 v2.2.8 // indirect + github.com/kr/text v0.2.0 // indirect + github.com/leodido/go-urn v1.4.0 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pelletier/go-toml/v2 v2.2.2 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.2.12 // indirect + golang.org/x/arch v0.9.0 // indirect + golang.org/x/crypto v0.26.0 // indirect + golang.org/x/net v0.28.0 // indirect + golang.org/x/sys v0.24.0 // indirect + golang.org/x/text v0.17.0 // indirect + google.golang.org/protobuf v1.34.2 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) + +================ +File: main.go +================ +package main + +import ( + "github.com/gin-contrib/cors" + "github.com/gin-gonic/gin" + "github.com/troneras/ghost-listmonk-connector/handlers" + "github.com/troneras/ghost-listmonk-connector/middleware" + "github.com/troneras/ghost-listmonk-connector/services" + "github.com/troneras/ghost-listmonk-connector/utils" +) + +func main() { + config, err := utils.LoadConfig() + if err != nil { + utils.ErrorLogger.Fatalf("Failed to load configuration: %v", err) + } + r := gin.Default() + + // CORS configuration + corsConfig := cors.DefaultConfig() + corsConfig.AllowOrigins = []string{"http://localhost:3000"} // Add your frontend URL here + corsConfig.AllowMethods = []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"} + corsConfig.AllowHeaders = []string{"Origin", "Content-Type", "Accept", "Authorization"} + corsConfig.AllowCredentials = true + + r.Use(cors.New(corsConfig)) + + // Initialize services + sonStorage, err := services.NewSonStorage("ghost_listmonk.db") + if err != nil { + utils.ErrorLogger.Fatalf("Failed to initialize son storage: %v", err) + } + + listmonkClient := services.NewListmonkClient(config) + sonExecutor := services.NewSonExecutor(listmonkClient) + sonHandler := handlers.NewSonHandler(sonStorage) + webhookHandler := handlers.NewWebhookHandler(sonStorage, sonExecutor) + listmonkHandler := handlers.NewListmonkHandler(listmonkClient) + + // Webhook endpoint (no auth required) + r.POST("/webhook", webhookHandler.HandleWebhook) + + // Web UI routes (auth required) + authorized := r.Group("/") + authorized.Use(middleware.APIKeyAuth(config.APIKey)) + { + authorized.GET("/", handleHome) + + // Son routes + sons := authorized.Group("/sons") + { + sons.POST("", sonHandler.Create) + sons.GET("", sonHandler.List) + sons.GET("/:id", sonHandler.Get) + sons.PUT("/:id", sonHandler.Update) + sons.DELETE("/:id", sonHandler.Delete) + } + authorized.GET("/lists", listmonkHandler.GetLists) + authorized.GET("/templates", listmonkHandler.GetTemplates) + } + + utils.InfoLogger.Infof("Server starting on port %s", config.Port) + if err := r.Run(":" + config.Port); err != nil { + utils.ErrorLogger.Fatalf("Failed to start server: %v", err) + } +} + +func handleHome(c *gin.Context) { + c.JSON(200, gin.H{"message": "Welcome to the Ghost-Listmonk Connector"}) +} diff --git a/services/database.go b/services/database.go new file mode 100644 index 0000000..3ae8df8 --- /dev/null +++ b/services/database.go @@ -0,0 +1,235 @@ +package services + +import ( + "database/sql" + "encoding/json" + "time" + + _ "github.com/mattn/go-sqlite3" + "github.com/troneras/ghost-listmonk-connector/models" + "github.com/troneras/ghost-listmonk-connector/utils" +) + +type Database struct { + db *sql.DB +} + +func NewDatabase(dbPath string) (*Database, error) { + db, err := sql.Open("sqlite3", dbPath) + if err != nil { + return nil, err + } + + if err := db.Ping(); err != nil { + return nil, err + } + + database := &Database{db: db} + if err := database.createTables(); err != nil { + return nil, err + } + + return database, nil +} + +func (d *Database) createTables() error { + _, err := d.db.Exec(` + CREATE TABLE IF NOT EXISTS sons ( + id TEXT PRIMARY KEY, + name TEXT, + trigger TEXT, + delay INTEGER, + created_at DATETIME, + updated_at DATETIME + ); + CREATE TABLE IF NOT EXISTS actions ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + son_id TEXT, + type TEXT, + parameters TEXT, + FOREIGN KEY (son_id) REFERENCES sons(id) + ); + `) + return err +} + +func (d *Database) CreateSon(son *models.Son) error { + son.ID = utils.GenerateUUID() + son.CreatedAt = time.Now() + son.UpdatedAt = time.Now() + + tx, err := d.db.Begin() + if err != nil { + return err + } + defer tx.Rollback() + + _, err = tx.Exec( + "INSERT INTO sons (id, name, trigger, delay, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)", + son.ID, son.Name, son.Trigger, int64(son.Delay), son.CreatedAt, son.UpdatedAt, + ) + if err != nil { + return err + } + + for _, action := range son.Actions { + parametersJSON, err := json.Marshal(action.Parameters) + if err != nil { + return err + } + _, err = tx.Exec( + "INSERT INTO actions (son_id, type, parameters) VALUES (?, ?, ?)", + son.ID, action.Type, string(parametersJSON), + ) + if err != nil { + return err + } + } + + return tx.Commit() +} + +func (d *Database) GetSon(id string) (*models.Son, error) { + son := &models.Son{} + err := d.db.QueryRow( + "SELECT id, name, trigger, delay, created_at, updated_at FROM sons WHERE id = ?", + id, + ).Scan(&son.ID, &son.Name, &son.Trigger, &son.Delay, &son.CreatedAt, &son.UpdatedAt) + if err != nil { + if err == sql.ErrNoRows { + return nil, ErrSonNotFound + } + return nil, err + } + + rows, err := d.db.Query("SELECT type, parameters FROM actions WHERE son_id = ?", id) + if err != nil { + return nil, err + } + defer rows.Close() + + for rows.Next() { + var action models.Action + var parametersJSON string + err := rows.Scan(&action.Type, ¶metersJSON) + if err != nil { + return nil, err + } + err = json.Unmarshal([]byte(parametersJSON), &action.Parameters) + if err != nil { + return nil, err + } + son.Actions = append(son.Actions, action) + } + + return son, nil +} + +func (d *Database) UpdateSon(son *models.Son) error { + son.UpdatedAt = time.Now() + + tx, err := d.db.Begin() + if err != nil { + return err + } + defer tx.Rollback() + + _, err = tx.Exec( + "UPDATE sons SET name = ?, trigger = ?, delay = ?, updated_at = ? WHERE id = ?", + son.Name, son.Trigger, int64(son.Delay), son.UpdatedAt, son.ID, + ) + if err != nil { + return err + } + + _, err = tx.Exec("DELETE FROM actions WHERE son_id = ?", son.ID) + if err != nil { + return err + } + + for _, action := range son.Actions { + parametersJSON, err := json.Marshal(action.Parameters) + if err != nil { + return err + } + _, err = tx.Exec( + "INSERT INTO actions (son_id, type, parameters) VALUES (?, ?, ?)", + son.ID, action.Type, string(parametersJSON), + ) + if err != nil { + return err + } + } + + return tx.Commit() +} + +func (d *Database) DeleteSon(id string) error { + tx, err := d.db.Begin() + if err != nil { + return err + } + defer tx.Rollback() + + _, err = tx.Exec("DELETE FROM actions WHERE son_id = ?", id) + if err != nil { + return err + } + + result, err := tx.Exec("DELETE FROM sons WHERE id = ?", id) + if err != nil { + return err + } + + affected, err := result.RowsAffected() + if err != nil { + return err + } + if affected == 0 { + return ErrSonNotFound + } + + return tx.Commit() +} + +func (d *Database) ListSons() ([]models.Son, error) { + rows, err := d.db.Query("SELECT id, name, trigger, delay, created_at, updated_at FROM sons") + if err != nil { + return nil, err + } + defer rows.Close() + + var sons []models.Son + for rows.Next() { + var son models.Son + err := rows.Scan(&son.ID, &son.Name, &son.Trigger, &son.Delay, &son.CreatedAt, &son.UpdatedAt) + if err != nil { + return nil, err + } + sons = append(sons, son) + } + + for i, son := range sons { + actionRows, err := d.db.Query("SELECT type, parameters FROM actions WHERE son_id = ?", son.ID) + if err != nil { + return nil, err + } + defer actionRows.Close() + + for actionRows.Next() { + var action models.Action + var parametersJSON string + err := actionRows.Scan(&action.Type, ¶metersJSON) + if err != nil { + return nil, err + } + err = json.Unmarshal([]byte(parametersJSON), &action.Parameters) + if err != nil { + return nil, err + } + sons[i].Actions = append(sons[i].Actions, action) + } + } + + return sons, nil +} diff --git a/services/listmonk_client.go b/services/listmonk_client.go new file mode 100644 index 0000000..0d4d578 --- /dev/null +++ b/services/listmonk_client.go @@ -0,0 +1,174 @@ +package services + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "net/http" + + "github.com/troneras/ghost-listmonk-connector/utils" +) + +type ListmonkList struct { + ID int `json:"id"` + Name string `json:"name"` +} + +type ListmonkTemplate struct { + ID int `json:"id"` + Name string `json:"name"` +} + +type ListmonkClient struct { + baseURL string + client *http.Client +} + +func NewListmonkClient(config *utils.Config) *ListmonkClient { + return &ListmonkClient{ + baseURL: config.ListmonkURL, + client: &http.Client{}, + } +} + +func (c *ListmonkClient) GetLists() ([]ListmonkList, error) { + resp, err := c.client.Get(c.baseURL + "/api/lists?page=1&per_page=100") + if err != nil { + return nil, fmt.Errorf("error fetching lists: %w", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode) + } + + var result struct { + Data struct { + Results []ListmonkList `json:"results"` + } `json:"data"` + } + if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { + return nil, fmt.Errorf("error decoding response: %w", err) + } + + return result.Data.Results, nil +} + +func (c *ListmonkClient) GetTemplates() ([]ListmonkTemplate, error) { + resp, err := c.client.Get(c.baseURL + "/api/templates?page=1&per_page=100") + if err != nil { + return nil, fmt.Errorf("error fetching templates: %w", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode) + } + + var result struct { + Data []ListmonkTemplate `json:"data"` + } + if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { + return nil, fmt.Errorf("error decoding response: %w", err) + } + + return result.Data, nil +} + +func (c *ListmonkClient) SendTransactionalEmail(templateID int, subscriberEmail string, data map[string]interface{}) error { + payload := map[string]interface{}{ + "subscriber_email": subscriberEmail, + "template_id": templateID, + "data": data, + } + + jsonPayload, err := json.Marshal(payload) + if err != nil { + utils.ErrorLogger.Errorf("Failed to marshal payload: %v", err) + return fmt.Errorf("failed to marshal payload: %w", err) + } + + resp, err := c.client.Post(c.baseURL+"/api/tx", "application/json", bytes.NewBuffer(jsonPayload)) + if err != nil { + utils.ErrorLogger.Errorf("Failed to send transactional email: %v", err) + return fmt.Errorf("failed to send transactional email: %w", err) + } + defer resp.Body.Close() + + body, _ := io.ReadAll(resp.Body) + if resp.StatusCode != http.StatusOK { + utils.ErrorLogger.Errorf("Unexpected status code: %d, body: %s", resp.StatusCode, string(body)) + return fmt.Errorf("unexpected status code: %d, body: %s", resp.StatusCode, string(body)) + } + + utils.InfoLogger.Infof("Sent transactional email to %s using template %d", subscriberEmail, templateID) + return nil +} + +func (c *ListmonkClient) ManageSubscriber(email string, name string, status string, lists []int) error { + payload := map[string]interface{}{ + "email": email, + "name": name, + "status": status, + "lists": lists, + "preconfirm_subscriptions": true, + } + + jsonPayload, err := json.Marshal(payload) + if err != nil { + utils.ErrorLogger.Errorf("Failed to marshal payload: %v", err) + return fmt.Errorf("failed to marshal payload: %w", err) + } + + // log the payload + utils.InfoLogger.Infof("Payload: %s", string(jsonPayload)) + + resp, err := c.client.Post(c.baseURL+"/api/subscribers", "application/json", bytes.NewBuffer(jsonPayload)) + if err != nil { + utils.ErrorLogger.Errorf("Failed to manage subscriber: %v", err) + return fmt.Errorf("failed to manage subscriber: %w", err) + } + defer resp.Body.Close() + + body, _ := io.ReadAll(resp.Body) + if resp.StatusCode != http.StatusOK { + utils.ErrorLogger.Errorf("Unexpected status code: %d, body: %s", resp.StatusCode, string(body)) + return fmt.Errorf("unexpected status code: %d, body: %s", resp.StatusCode, string(body)) + } + + utils.InfoLogger.Infof("Managed subscriber %s with status %s", email, status) + return nil +} + +func (c *ListmonkClient) CreateCampaign(name string, subject string, lists []int, templateID int, sendAt string) error { + payload := map[string]interface{}{ + "name": name, + "subject": subject, + "lists": lists, + "template_id": templateID, + "send_at": sendAt, + } + + jsonPayload, err := json.Marshal(payload) + if err != nil { + utils.ErrorLogger.Errorf("Failed to marshal payload: %v", err) + return fmt.Errorf("failed to marshal payload: %w", err) + } + + resp, err := c.client.Post(c.baseURL+"/api/campaigns", "application/json", bytes.NewBuffer(jsonPayload)) + if err != nil { + utils.ErrorLogger.Errorf("Failed to create campaign: %v", err) + return fmt.Errorf("failed to create campaign: %w", err) + } + defer resp.Body.Close() + + body, _ := io.ReadAll(resp.Body) + if resp.StatusCode != http.StatusOK { + utils.ErrorLogger.Errorf("Unexpected status code: %d, body: %s", resp.StatusCode, string(body)) + return fmt.Errorf("unexpected status code: %d, body: %s", resp.StatusCode, string(body)) + } + + utils.InfoLogger.Infof("Created campaign %s with subject %s", name, subject) + return nil +} diff --git a/services/listmonk_client_test.go b/services/listmonk_client_test.go new file mode 100644 index 0000000..ac7b0bf --- /dev/null +++ b/services/listmonk_client_test.go @@ -0,0 +1,47 @@ +package services + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestListmonkClient(t *testing.T) { + // Create a mock server + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case "/api/tx": + w.WriteHeader(http.StatusOK) + w.Write([]byte(`{"data": true}`)) + case "/api/subscribers": + w.WriteHeader(http.StatusOK) + w.Write([]byte(`{"data": {"id": 1}}`)) + case "/api/campaigns": + w.WriteHeader(http.StatusOK) + w.Write([]byte(`{"data": {"id": 1}}`)) + default: + http.Error(w, "Not found", http.StatusNotFound) + } + })) + defer server.Close() + + // Create a client that uses the mock server URL + client := &ListmonkClient{ + baseURL: server.URL, + client: server.Client(), + } + + // Test SendTransactionalEmail + err := client.SendTransactionalEmail(1, "test@example.com", map[string]interface{}{"name": "Test"}) + assert.NoError(t, err) + + // Test ManageSubscriber + err = client.ManageSubscriber("test@example.com", "Test User", "enabled", []int{1}) + assert.NoError(t, err) + + // Test CreateCampaign + err = client.CreateCampaign("Test Campaign", "Test Subject", []int{1}, 1, "2023-01-01T00:00:00Z") + assert.NoError(t, err) +} diff --git a/services/son_executor.go b/services/son_executor.go new file mode 100644 index 0000000..740f426 --- /dev/null +++ b/services/son_executor.go @@ -0,0 +1,100 @@ +package services + +import ( + "fmt" + "time" + + "github.com/troneras/ghost-listmonk-connector/models" + "github.com/troneras/ghost-listmonk-connector/utils" +) + +type SonExecutor struct { + listmonkClient *ListmonkClient +} + +func NewSonExecutor(listmonkClient *ListmonkClient) *SonExecutor { + return &SonExecutor{ + listmonkClient: listmonkClient, + } +} + +func (e *SonExecutor) ExecuteSon(son models.Son, data map[string]interface{}) { + utils.InfoLogger.Infof("Executing Son: %s", son.ID) + + if son.Delay > 0 { + utils.InfoLogger.Infof("Delaying execution of Son %s for %v", son.ID, time.Duration(son.Delay)) + time.Sleep(time.Duration(son.Delay)) + } + + for _, action := range son.Actions { + err := e.executeAction(action, data) + if err != nil { + utils.ErrorLogger.Errorf("Error executing action for Son %s: %v", son.ID, err) + } + } + + utils.InfoLogger.Infof("Finished executing Son: %s", son.ID) +} + +func (e *SonExecutor) executeAction(action models.Action, data map[string]interface{}) error { + utils.InfoLogger.Infof("Executing action: %s", action.Type) + + switch action.Type { + case models.ActionSendTransactionalEmail: + return e.sendTransactionalEmail(action.Parameters, data) + case models.ActionManageSubscriber: + return e.manageSubscriber(action.Parameters, data) + case models.ActionCreateCampaign: + return e.createCampaign(action.Parameters, data) + default: + err := fmt.Errorf("unknown action type: %s", action.Type) + utils.ErrorLogger.Errorf("%v", err) + return err + } +} + +func (e *SonExecutor) sendTransactionalEmail(params map[string]interface{}, data map[string]interface{}) error { + templateID := int(params["template_id"].(float64)) + subscriberEmail := data["member"].(map[string]interface{})["current"].(map[string]interface{})["email"].(string) + + utils.InfoLogger.Infof("Sending transactional email to %s using template %d", subscriberEmail, templateID) + return e.listmonkClient.SendTransactionalEmail(templateID, subscriberEmail, data) +} + +func (e *SonExecutor) manageSubscriber(params map[string]interface{}, data map[string]interface{}) error { + email := data["member"].(map[string]interface{})["current"].(map[string]interface{})["email"].(string) + + utils.InfoLogger.Infof("Managing subscriber %s", email) + utils.InfoLogger.Infof("Params: %v", params) + var name string + + if member, ok := data["member"].(map[string]interface{}); ok { + if current, ok := member["current"].(map[string]interface{}); ok { + if n, ok := current["name"].(string); ok { + name = n + } + } + } + status := "enabled" //params["status"].(string) + lists := []int{} + for _, listID := range params["lists"].([]interface{}) { + lists = append(lists, int(listID.(float64))) + } + + utils.InfoLogger.Infof("Managing subscriber %s with status %s", email, status) + return e.listmonkClient.ManageSubscriber(email, name, status, lists) +} + +func (e *SonExecutor) createCampaign(params map[string]interface{}, data map[string]interface{}) error { + name := params["name"].(string) + subject := params["subject"].(string) + lists := []int{} + for _, listID := range params["lists"].([]interface{}) { + lists = append(lists, int(listID.(float64))) + } + templateID := int(params["template_id"].(float64)) + sendAt := params["send_at"].(string) + + utils.InfoLogger.Infof("Creating campaign %s with subject %s", name, subject) + return e.listmonkClient.CreateCampaign(name, subject, lists, templateID, sendAt) +} diff --git a/services/son_storage.go b/services/son_storage.go new file mode 100644 index 0000000..904e020 --- /dev/null +++ b/services/son_storage.go @@ -0,0 +1,199 @@ +package services + +import ( + "database/sql" + "encoding/json" + "errors" + "time" + + _ "github.com/mattn/go-sqlite3" + "github.com/troneras/ghost-listmonk-connector/models" + "github.com/troneras/ghost-listmonk-connector/utils" +) + +var ( + ErrSonAlreadyExists = errors.New("son with this ID already exists") + ErrSonNotFound = errors.New("son not found") +) + +type SonStorage struct { + db *sql.DB +} + +func NewSonStorage(dbPath string) (*SonStorage, error) { + db, err := sql.Open("sqlite3", dbPath) + if err != nil { + return nil, err + } + + if err := db.Ping(); err != nil { + return nil, err + } + + storage := &SonStorage{db: db} + if err := storage.createTables(); err != nil { + return nil, err + } + + return storage, nil +} + +func (s *SonStorage) createTables() error { + _, err := s.db.Exec(` + CREATE TABLE IF NOT EXISTS sons ( + id TEXT PRIMARY KEY, + name TEXT, + trigger TEXT, + delay INTEGER, + actions TEXT, + created_at DATETIME, + updated_at DATETIME + ) + `) + return err +} + +func (s *SonStorage) Create(son *models.Son) error { + son.ID = utils.GenerateUUID() + son.CreatedAt = time.Now() + son.UpdatedAt = time.Now() + + actionsJSON, err := json.Marshal(son.Actions) + if err != nil { + return err + } + + _, err = s.db.Exec( + "INSERT INTO sons (id, name, trigger, delay, actions, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)", + son.ID, son.Name, son.Trigger, int64(son.Delay), actionsJSON, son.CreatedAt, son.UpdatedAt, + ) + if err != nil { + utils.ErrorLogger.Errorf("Failed to create Son: %v", err) + return err + } + + utils.InfoLogger.Infof("Created new Son with ID: %s", son.ID) + return nil +} + +func (s *SonStorage) Get(id string) (models.Son, error) { + var son models.Son + var actionsJSON []byte + var delayInt int64 + + err := s.db.QueryRow( + "SELECT id, name, trigger, delay, actions, created_at, updated_at FROM sons WHERE id = ?", + id, + ).Scan(&son.ID, &son.Name, &son.Trigger, &delayInt, &actionsJSON, &son.CreatedAt, &son.UpdatedAt) + + if err != nil { + if err == sql.ErrNoRows { + utils.ErrorLogger.Errorf("Failed to get Son: %v", ErrSonNotFound) + return models.Son{}, ErrSonNotFound + } + utils.ErrorLogger.Errorf("Failed to get Son: %v", err) + return models.Son{}, err + } + + son.Delay = models.Duration(time.Duration(delayInt)) + + err = json.Unmarshal(actionsJSON, &son.Actions) + if err != nil { + utils.ErrorLogger.Errorf("Failed to unmarshal actions: %v", err) + return models.Son{}, err + } + + utils.InfoLogger.Infof("Retrieved Son with ID: %s", id) + return son, nil +} + +func (s *SonStorage) Update(son models.Son) error { + son.UpdatedAt = time.Now() + + actionsJSON, err := json.Marshal(son.Actions) + if err != nil { + return err + } + + result, err := s.db.Exec( + "UPDATE sons SET name = ?, trigger = ?, delay = ?, actions = ?, updated_at = ? WHERE id = ?", + son.Name, son.Trigger, int64(son.Delay), actionsJSON, son.UpdatedAt, son.ID, + ) + if err != nil { + utils.ErrorLogger.Errorf("Failed to update Son: %v", err) + return err + } + + rowsAffected, err := result.RowsAffected() + if err != nil { + return err + } + + if rowsAffected == 0 { + utils.ErrorLogger.Errorf("Failed to update Son: %v", ErrSonNotFound) + return ErrSonNotFound + } + + utils.InfoLogger.Infof("Updated Son with ID: %s", son.ID) + return nil +} + +func (s *SonStorage) Delete(id string) error { + result, err := s.db.Exec("DELETE FROM sons WHERE id = ?", id) + if err != nil { + utils.ErrorLogger.Errorf("Failed to delete Son: %v", err) + return err + } + + rowsAffected, err := result.RowsAffected() + if err != nil { + return err + } + + if rowsAffected == 0 { + utils.ErrorLogger.Errorf("Failed to delete Son: %v", ErrSonNotFound) + return ErrSonNotFound + } + + utils.InfoLogger.Infof("Deleted Son with ID: %s", id) + return nil +} + +func (s *SonStorage) List() ([]models.Son, error) { + rows, err := s.db.Query("SELECT id, name, trigger, delay, actions, created_at, updated_at FROM sons") + if err != nil { + utils.ErrorLogger.Errorf("Failed to list Sons: %v", err) + return nil, err + } + defer rows.Close() + + var sons []models.Son + for rows.Next() { + var son models.Son + var actionsJSON []byte + var delayInt int64 + + err := rows.Scan(&son.ID, &son.Name, &son.Trigger, &delayInt, &actionsJSON, &son.CreatedAt, &son.UpdatedAt) + if err != nil { + utils.ErrorLogger.Errorf("Failed to scan Son: %v", err) + continue + } + + son.Delay = models.Duration(time.Duration(delayInt)) + + err = json.Unmarshal(actionsJSON, &son.Actions) + if err != nil { + utils.ErrorLogger.Errorf("Failed to unmarshal actions: %v", err) + continue + } + + sons = append(sons, son) + } + + if sons == nil { + sons = []models.Son{} // Ensure we always return an array, even if empty + } + + utils.InfoLogger.Infof("Retrieved list of %d Sons", len(sons)) + return sons, nil +} diff --git a/services/son_storage_test.go b/services/son_storage_test.go new file mode 100644 index 0000000..a09b1e5 --- /dev/null +++ b/services/son_storage_test.go @@ -0,0 +1,82 @@ +package services + +import ( + "os" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/troneras/ghost-listmonk-connector/models" +) + +func TestSonStorage(t *testing.T) { + // Create a temporary database for testing + tempDBPath := "test_ghost_listmonk.db" + storage, err := NewSonStorage(tempDBPath) + assert.NoError(t, err) + defer os.Remove(tempDBPath) // Clean up the temporary database file after tests + + // Test Create + son := models.Son{ + Name: "Test Son", + Trigger: models.TriggerMemberCreated, + Delay: models.Duration(5 * time.Minute), + Actions: []models.Action{ + { + Type: models.ActionSendTransactionalEmail, + Parameters: map[string]interface{}{ + "template_id": float64(1), // Use float64 to match JSON unmarshaling behavior + }, + }, + }, + } + + err = storage.Create(&son) + assert.NoError(t, err) + assert.NotEmpty(t, son.ID) // Ensure an ID was generated + + // Test Get + retrievedSon, err := storage.Get(son.ID) + assert.NoError(t, err) + assert.Equal(t, son.Name, retrievedSon.Name) + assert.Equal(t, son.Trigger, retrievedSon.Trigger) + assert.Equal(t, son.Delay, retrievedSon.Delay) + assert.Len(t, retrievedSon.Actions, 1) + assert.Equal(t, son.Actions[0].Type, retrievedSon.Actions[0].Type) + assert.Equal(t, son.Actions[0].Parameters["template_id"], retrievedSon.Actions[0].Parameters["template_id"]) + + // Test Update + son.Name = "Updated Test Son" + err = storage.Update(son) + assert.NoError(t, err) + + updatedSon, err := storage.Get(son.ID) + assert.NoError(t, err) + assert.Equal(t, "Updated Test Son", updatedSon.Name) + + // Test List + sons, err := storage.List() + assert.NoError(t, err) + assert.Len(t, sons, 1) + assert.Equal(t, son.ID, sons[0].ID) + + // Test Delete + err = storage.Delete(son.ID) + assert.NoError(t, err) + + _, err = storage.Get(son.ID) + assert.Error(t, err) + assert.Equal(t, ErrSonNotFound, err) + + // Test error cases + err = storage.Create(&son) + assert.NoError(t, err) + + // Attempt to update a non-existent son (should fail) + nonExistentSon := models.Son{ID: "non-existent", Name: "Non-existent Son"} + err = storage.Update(nonExistentSon) + assert.Equal(t, ErrSonNotFound, err) + + err = storage.Delete("non-existent") + assert.Equal(t, ErrSonNotFound, err) +} diff --git a/ui/.eslintrc.json b/ui/.eslintrc.json new file mode 100644 index 0000000..bffb357 --- /dev/null +++ b/ui/.eslintrc.json @@ -0,0 +1,3 @@ +{ + "extends": "next/core-web-vitals" +} diff --git a/ui/.gitignore b/ui/.gitignore new file mode 100644 index 0000000..fd3dbb5 --- /dev/null +++ b/ui/.gitignore @@ -0,0 +1,36 @@ +# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. + +# dependencies +/node_modules +/.pnp +.pnp.js +.yarn/install-state.gz + +# testing +/coverage + +# next.js +/.next/ +/out/ + +# production +/build + +# misc +.DS_Store +*.pem + +# debug +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# local env files +.env*.local + +# vercel +.vercel + +# typescript +*.tsbuildinfo +next-env.d.ts diff --git a/ui/README.md b/ui/README.md new file mode 100644 index 0000000..c403366 --- /dev/null +++ b/ui/README.md @@ -0,0 +1,36 @@ +This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app). + +## Getting Started + +First, run the development server: + +```bash +npm run dev +# or +yarn dev +# or +pnpm dev +# or +bun dev +``` + +Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. + +You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file. + +This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font. + +## Learn More + +To learn more about Next.js, take a look at the following resources: + +- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API. +- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial. + +You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome! + +## Deploy on Vercel + +The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js. + +Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details. diff --git a/ui/app/favicon.ico b/ui/app/favicon.ico new file mode 100644 index 0000000..718d6fe Binary files /dev/null and b/ui/app/favicon.ico differ diff --git a/ui/app/layout.tsx b/ui/app/layout.tsx new file mode 100644 index 0000000..ba249fe --- /dev/null +++ b/ui/app/layout.tsx @@ -0,0 +1,31 @@ +import "@/styles/globals.css"; +import { Inter as FontSans } from "next/font/google"; + +import { cn } from "@/lib/utils"; +import Layout from "@/components/layout"; +import { Toaster } from "@/components/ui/toaster"; + +const fontSans = FontSans({ + subsets: ["latin"], + variable: "--font-sans", +}); + +export default function RootLayout({ + children, +}: Readonly<{ + children: React.ReactNode; +}>) { + return ( + + + {children} + + + + ); +} diff --git a/ui/app/page.tsx b/ui/app/page.tsx new file mode 100644 index 0000000..cb46d5d --- /dev/null +++ b/ui/app/page.tsx @@ -0,0 +1,5 @@ +import Dashboard from "@/components/Dashboard"; + +export default function HomePage() { + return ; +} diff --git a/ui/app/sons/[id]/page.tsx b/ui/app/sons/[id]/page.tsx new file mode 100644 index 0000000..3e77715 --- /dev/null +++ b/ui/app/sons/[id]/page.tsx @@ -0,0 +1,209 @@ +"use client"; +import React, { useEffect } from "react"; +import { useParams, useRouter } from "next/navigation"; +import { useForm, useFieldArray } from "react-hook-form"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { Button } from "@/components/ui/button"; +import { + AlertDialog, + AlertDialogAction, + AlertDialogCancel, + AlertDialogContent, + AlertDialogDescription, + AlertDialogFooter, + AlertDialogHeader, + AlertDialogTitle, + AlertDialogTrigger, +} from "@/components/ui/alert-dialog"; +import { Form } from "@/components/ui/form"; +import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; +import { PlusCircle } from "lucide-react"; +import { useSon } from "@/hooks/useSon"; +import { useSons } from "@/hooks/useSons"; +import { useLists } from "@/hooks/useLists"; +import { useTemplates } from "@/hooks/useTemplates"; +import { useCustomToast } from "@/hooks/useCustomToast"; +import { SonDetailsForm } from "@/components/SonDetailsForm"; +import { ActionForm } from "@/components/ActionForm"; +import { editableSonSchema } from "@/lib/schemas"; +import { EditableSon, Son } from "@/lib/types"; + +// Define a type guard function +function isValidTrigger(trigger: string): trigger is EditableSon["trigger"] { + return [ + "member_created", + "member_deleted", + "member_updated", + "post_published", + "post_scheduled", + ].includes(trigger); +} + +export default function SonDetailPage() { + const params = useParams(); + const router = useRouter(); + const { + son, + loading: sonLoading, + error: sonError, + } = useSon(params.id as string); + const { updateSon, deleteSon } = useSons(); + const { showToast } = useCustomToast(); + const { lists, loading: listsLoading, error: listsError } = useLists(); + const { + templates, + loading: templatesLoading, + error: templatesError, + } = useTemplates(); + + const form = useForm({ + resolver: zodResolver(editableSonSchema), + defaultValues: { + name: "", + trigger: "member_created", + delay: 0, + actions: [], + }, + }); + + const { fields, append, remove } = useFieldArray({ + control: form.control, + name: "actions", + }); + + useEffect(() => { + if (son) { + const { name, trigger, delay, actions } = son; + + if (isValidTrigger(trigger)) { + form.reset({ + name, + trigger, + delay, + actions: actions.map((action) => ({ + type: action.type, + parameters: action.parameters, + })), + }); + } else { + console.error(`Invalid trigger value: ${trigger}`); + form.reset({ + name, + trigger: "member_created", + delay, + actions: actions.map((action) => ({ + type: action.type, + parameters: action.parameters, + })), + }); + } + } + }, [son, form]); + + if (sonLoading || listsLoading || templatesLoading) { + return ( +
Loading...
+ ); + } + + if (sonError || listsError || templatesError) { + return ( +
+ Error:{" "} + {sonError?.message || listsError?.message || templatesError?.message} +
+ ); + } + + if (!son) return
Son not found
; + + const onSubmit = async (data: EditableSon) => { + try { + await updateSon(son.id, data); + showToast("Success", "Son updated successfully"); + router.push("/sons"); + } catch (error) { + showToast("Error", "Failed to update Son", "destructive"); + console.error("Failed to update Son:", error); + } + }; + const handleDelete = async () => { + try { + await deleteSon(son.id); + showToast("Success", "Son deleted successfully"); + router.push("/sons"); + } catch (error) { + showToast("Error", "Failed to delete Son", "destructive"); + console.error("Failed to delete Son:", error); + } + }; + + return ( +
+

Edit Son: {son.name}

+

+ Created at: {new Date(son.created_at).toLocaleString()} +

+

+ Last updated: {new Date(son.updated_at).toLocaleString()} +

+ +
+ + + + + + Actions + + + {fields.map((field, index) => ( + + ))} + + + + + + + + + + + + + + + Are you sure? + + This action cannot be undone. This will permanently delete the + Son. + + + + Cancel + Delete + + + +
+ ); +} diff --git a/ui/app/sons/layout.tsx b/ui/app/sons/layout.tsx new file mode 100644 index 0000000..2a6f6ea --- /dev/null +++ b/ui/app/sons/layout.tsx @@ -0,0 +1,3 @@ +export default function Layout({ children }: { children: React.ReactNode }) { + return
{children}
; +} diff --git a/ui/app/sons/new/page.tsx b/ui/app/sons/new/page.tsx new file mode 100644 index 0000000..49fcb3f --- /dev/null +++ b/ui/app/sons/new/page.tsx @@ -0,0 +1,10 @@ +import SonCreationForm from "@/components/SonCreationForm"; + +export default function NewSonPage() { + return ( +
+

Create New Son

+ +
+ ); +} diff --git a/ui/app/sons/page.tsx b/ui/app/sons/page.tsx new file mode 100644 index 0000000..f5c0761 --- /dev/null +++ b/ui/app/sons/page.tsx @@ -0,0 +1,21 @@ +"use client"; +import Link from "next/link"; +import { Button } from "@/components/ui/button"; + +import { SonList } from "@/components/SonList"; + +export default function SonListPage() { + + return ( +
+
+

Manage Sons

+ + + +
+ + +
+ ); +} diff --git a/ui/components.json b/ui/components.json new file mode 100644 index 0000000..15f2b02 --- /dev/null +++ b/ui/components.json @@ -0,0 +1,17 @@ +{ + "$schema": "https://ui.shadcn.com/schema.json", + "style": "default", + "rsc": true, + "tsx": true, + "tailwind": { + "config": "tailwind.config.ts", + "css": "app/globals.css", + "baseColor": "slate", + "cssVariables": true, + "prefix": "" + }, + "aliases": { + "components": "@/components", + "utils": "@/lib/utils" + } +} \ No newline at end of file diff --git a/ui/components/ActionForm.tsx b/ui/components/ActionForm.tsx new file mode 100644 index 0000000..92e3cbe --- /dev/null +++ b/ui/components/ActionForm.tsx @@ -0,0 +1,148 @@ +import React from "react"; +import { + FormField, + FormItem, + FormLabel, + FormControl, + FormMessage, + FormDescription, +} from "@/components/ui/form"; +import { + Select, + SelectContent, + SelectItem, + SelectTrigger, + SelectValue, +} from "@/components/ui/select"; +import { Textarea } from "@/components/ui/textarea"; +import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; +import { Button } from "@/components/ui/button"; +import { Trash2 } from "lucide-react"; +import { CampaignActionFields } from "./CampaignActionFields"; +import { ManageSubscriberActionFields } from "./ManageSubscriberActionFields"; +import { ActionFormProps } from "@/lib/types"; + +export function ActionForm({ + form, + index, + remove, + lists, + templates, +}: ActionFormProps) { + const actionType = form.watch(`actions.${index}.type`); + const trigger = form.watch("trigger"); + + return ( + + + + Action {index + 1} + + + + + ( + + Action Type + + + {actionType === "manage_subscriber" && ( + + Subscriber management automatically adds or removes + subscribers on listmonk and adds to lists based on the trigger + event and newsletters from ghost blog. + + )} + + )} + /> + + {actionType === "create_campaign" && ( + + )} + + {actionType === "manage_subscriber" && trigger == "member_created" && ( + + )} + + {actionType === "send_transactional_email" && ( + ( + + Parameters + +