From bae4a98ffa740afcdb7ac1d2ea9ff1999829c4c3 Mon Sep 17 00:00:00 2001 From: Quang Le Date: Thu, 21 Nov 2024 12:52:38 +0700 Subject: [PATCH] feat: add template email for offboarding that keeps forwarded emails (#762) * fix: correct migrate file view project cost and rev * feat: add template email for offboarding that keeps forwarded emails --- ...30151941-view-project-cost-and-revenue.sql | 16 ++++++--- .../employee/update_employee_status.go | 17 +++++++++ pkg/model/employees.go | 6 ++++ pkg/service/googlemail/google_mail.go | 26 ++++++++++++++ pkg/service/googlemail/interface.go | 1 + pkg/templates/offboarding_keep_fwd_email.tpl | 36 +++++++++++++++++++ 6 files changed, 97 insertions(+), 5 deletions(-) create mode 100644 pkg/templates/offboarding_keep_fwd_email.tpl diff --git a/migrations/schemas/20241030151941-view-project-cost-and-revenue.sql b/migrations/schemas/20241030151941-view-project-cost-and-revenue.sql index d323cef12..72e6bbefa 100644 --- a/migrations/schemas/20241030151941-view-project-cost-and-revenue.sql +++ b/migrations/schemas/20241030151941-view-project-cost-and-revenue.sql @@ -1,11 +1,15 @@ - -- +migrate Up +-- Drop dependent objects first +DROP VIEW IF EXISTS "public"."vw_project_cost_and_revenue"; +DROP TABLE IF EXISTS "public"."cost_projections"; + +-- Now drop and recreate the types DROP TYPE IF EXISTS "public"."cost_types"; CREATE TYPE "public"."cost_types" AS ENUM ('individual', 'sum'); DROP TYPE IF EXISTS "public"."amount_types"; CREATE TYPE "public"."amount_types" AS ENUM ('percentage', 'flat'); --- Table Definition +-- Recreate the table with the new type CREATE TABLE "public"."cost_projections" ( "id" uuid NOT NULL DEFAULT uuid(), "name" varchar(255) NOT NULL, @@ -15,6 +19,7 @@ CREATE TABLE "public"."cost_projections" ( PRIMARY KEY ("id") ); +-- Recreate the view CREATE OR REPLACE VIEW vw_project_cost_and_revenue AS WITH project_data AS ( -- Summarize project salary and charge rate @@ -88,7 +93,8 @@ SELECT FROM project_data pd LEFT JOIN cost_projection_calculations cpc ON cpc.project_id = pd.project_id; - -- +migrate Down -DROP VIEW IF EXISTS "vw_project_cost_and_revenue"; -DROP TABLE IF EXISTS "cost_projections"; +DROP VIEW IF EXISTS "public"."vw_project_cost_and_revenue"; +DROP TABLE IF EXISTS "public"."cost_projections"; +DROP TYPE IF EXISTS "public"."cost_types"; +DROP TYPE IF EXISTS "public"."amount_types"; diff --git a/pkg/controller/employee/update_employee_status.go b/pkg/controller/employee/update_employee_status.go index ae6b660f0..4930c72c6 100644 --- a/pkg/controller/employee/update_employee_status.go +++ b/pkg/controller/employee/update_employee_status.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "strings" "time" "gorm.io/gorm" @@ -61,6 +62,22 @@ func (r *controller) UpdateEmployeeStatus(employeeID string, body UpdateWorkingS // Do Off-boarding process r.processOffBoardingEmployee(l, e) + if e.IsKeepFwdEmail { + name := e.FullName + if e.DisplayName != "" { + name = strings.Split(e.DisplayName, " ")[0] + } + offboardingEmail := model.OffboardingEmail{ + Name: name, + TeamEmail: e.TeamEmail, + PersonalEmail: e.PersonalEmail, + } + + if err := r.service.GoogleMail.SendOffboardingMail(&offboardingEmail); err != nil { + l.Errorf(err, "failed to send offboard mail", "employeeID", e.ID.String()) + return nil, done(err) + } + } } return e, err diff --git a/pkg/model/employees.go b/pkg/model/employees.go index 20b2d0302..93b05e89c 100644 --- a/pkg/model/employees.go +++ b/pkg/model/employees.go @@ -173,3 +173,9 @@ func (e Employees) ToTeamEmailIDMap() map[string]UUID { return rs } + +type OffboardingEmail struct { + Name string `json:"name"` + PersonalEmail string `json:"personalEmail"` + TeamEmail string `json:"teamEmail"` +} diff --git a/pkg/service/googlemail/google_mail.go b/pkg/service/googlemail/google_mail.go index 906a77eaf..ab4376f3d 100644 --- a/pkg/service/googlemail/google_mail.go +++ b/pkg/service/googlemail/google_mail.go @@ -457,3 +457,29 @@ func (g *googleService) SendInvitationMail(invitation *model.InvitationEmail) (e _, err = g.sendEmail(encodedEmail, id) return err } + +// SendOffboardingMail ... +func (g *googleService) SendOffboardingMail(offboarding *model.OffboardingEmail) (err error) { + if err := g.ensureToken(g.appConfig.Google.TeamGoogleRefreshToken); err != nil { + return err + } + + if err := g.prepareService(); err != nil { + return err + } + + encodedEmail, err := composeMailContent(g.appConfig, + &MailParseInfo{ + teamEmail, + "offboarding_keep_fwd_email.tpl", + &offboarding, + map[string]interface{}{}, + }) + if err != nil { + return err + } + id := g.appConfig.Google.TeamEmailID + + _, err = g.sendEmail(encodedEmail, id) + return err +} diff --git a/pkg/service/googlemail/interface.go b/pkg/service/googlemail/interface.go index 2971b3c26..a45a0e1a9 100644 --- a/pkg/service/googlemail/interface.go +++ b/pkg/service/googlemail/interface.go @@ -20,4 +20,5 @@ type IService interface { SendInvoiceOverdueMail(invoice *model.Invoice) (err error) SendInvoiceThankYouMail(invoice *model.Invoice) (err error) SendPayrollPaidMail(p *model.Payroll) (err error) + SendOffboardingMail(offboarding *model.OffboardingEmail) (err error) } diff --git a/pkg/templates/offboarding_keep_fwd_email.tpl b/pkg/templates/offboarding_keep_fwd_email.tpl new file mode 100644 index 000000000..e2230fa83 --- /dev/null +++ b/pkg/templates/offboarding_keep_fwd_email.tpl @@ -0,0 +1,36 @@ +Mime-Version: 1.0 +From: "Team @ Dwarves Foundation" +To: {{.PersonalEmail}} +Subject: Continue Using Your DF Email +Content-Type: multipart/mixed; boundary=main + +--main +Content-Type: text/html; charset="UTF-8" +Content-Transfer-Encoding: quoted-printable + +
+
Hi {{.Name}},
+
+

+
+ We appreciate the efforts you put into your role at Dwarves Foundation. + While your journey with DF has concluded, we'd like to offer a small way to stay connected: you can retain your DF email alias {{.TeamEmail}}. +
+

+

+
+ This alias will forward incoming messages to your personal email {{.PersonalEmail}}, + ensuring continuity and a professional touch for your correspondence.

+

+
+
If you have any questions, please feel free to get in touch.
+

+
Wishing you all the best,
+
+
+
+

--
+ {{ template "signature.tpl" }} + + +--main-- \ No newline at end of file