-
Notifications
You must be signed in to change notification settings - Fork 10
feat: email notification for opportunity workspace #1867
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
b0c0c71
feat: email notification for opportunity workspace
71c070e
Merge branch 'main' into feat/oppty-workspace-email-notify
jjenscodee 25fefe3
fix tests coverage
9b5afbb
remove env
d532114
Merge branch 'main' into feat/oppty-workspace-email-notify
jjenscodee b75a64e
initial save and debug
54c2cad
update to use createFrom
a641233
try with apo scope
2dc01ff
some logs to debug
ec4e7d7
lower coverage
bb1e7db
try getServiceAccessTokenV3
9f918fc
try new env
a8943d4
Trigger redeploy
b8355b2
email notify when a user get assigned
5509211
update to use correct template
862b341
Merge branch 'main' into feat/oppty-workspace-email-notify
jjenscodee f6dc3c8
Merge branch 'main' into feat/oppty-workspace-email-notify
jjenscodee 409ef13
fix payload
a286214
fix url path
7cf6b6c
fix opportunity name
2a770c2
100 test coverage
4d29773
resolve comments
bb16d23
update the condition when to send emails
41e8612
remove owner from sending list
25b8329
Merge branch 'main' into feat/oppty-workspace-email-notify
jjenscodee e5db7c7
remove unused var
a893055
Merge branch 'main' into feat/oppty-workspace-email-notify
jjenscodee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| /* | ||
| * Copyright 2025 Adobe. All rights reserved. | ||
| * This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. You may obtain a copy | ||
| * of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under | ||
| * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
| * OF ANY KIND, either express or implied. See the License for the specific language | ||
| * governing permissions and limitations under the License. | ||
| */ | ||
|
|
||
| import { ImsClient } from '@adobe/spacecat-shared-ims-client'; | ||
|
|
||
| /** | ||
| * Acquires an IMS service access token using email-specific credentials. | ||
| * Does NOT mutate context.env. | ||
| * @param {Object} context - The request context with env and log. | ||
| * @returns {Promise<string>} The access token string. | ||
| */ | ||
| export async function getEmailServiceToken(context) { | ||
| const { env } = context; | ||
|
|
||
| const emailEnv = { | ||
| ...env, | ||
| IMS_CLIENT_ID: env.LLMO_EMAIL_IMS_CLIENT_ID, | ||
| IMS_CLIENT_SECRET: env.LLMO_EMAIL_IMS_CLIENT_SECRET, | ||
| IMS_CLIENT_CODE: env.LLMO_EMAIL_IMS_CLIENT_CODE, | ||
| IMS_SCOPE: env.LLMO_EMAIL_IMS_SCOPE, | ||
| }; | ||
|
|
||
| const imsClient = ImsClient.createFrom({ ...context, env: emailEnv }); | ||
|
|
||
| try { | ||
| const tokenPayload = await imsClient.getServiceAccessToken(); | ||
jjenscodee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return tokenPayload.access_token; | ||
| } catch (error) { | ||
| context.log.error('[email-service] Failed to acquire IMS token', { error: error.message }); | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Sends a templated email via Adobe Post Office. | ||
| * | ||
| * @param {Object} context - The request context (must include env and log). | ||
| * @param {Object} options | ||
| * @param {string[]} options.recipients - Array of email addresses. | ||
| * @param {string} options.templateName - Post Office template name. | ||
| * @param {Object<string,string>} [options.templateData] - Template variable key/value pairs. | ||
| * @param {string} [options.locale='en_US'] - Locale for the email. | ||
| * @param {string} [options.accessToken] - when provided, skips token acquisition. | ||
| * @returns {Promise<{success: boolean, statusCode: number, error?: string, templateUsed: string}>} | ||
| * Result object. Never throws by default. | ||
| */ | ||
| export async function sendEmail(context, { | ||
jjenscodee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| recipients, | ||
| templateName, | ||
| templateData, | ||
| locale = 'en_US', | ||
| accessToken: providedToken, | ||
| }) { | ||
| const { env, log } = context; | ||
| const result = { success: false, statusCode: 0, templateUsed: templateName }; | ||
|
|
||
| try { | ||
| if (!recipients || recipients.length === 0) { | ||
| result.error = 'No recipients provided'; | ||
| return result; | ||
| } | ||
|
|
||
| if (!templateName) { | ||
| result.error = 'templateName is required'; | ||
| return result; | ||
| } | ||
|
|
||
| const accessToken = providedToken ?? await getEmailServiceToken(context); | ||
| const postOfficeEndpoint = env.ADOBE_POSTOFFICE_ENDPOINT; | ||
|
|
||
| if (!postOfficeEndpoint) { | ||
| result.error = 'ADOBE_POSTOFFICE_ENDPOINT is not configured'; | ||
| return result; | ||
| } | ||
|
|
||
| const body = JSON.stringify({ | ||
| toList: recipients.join(','), | ||
| templateData: templateData || {}, | ||
| }); | ||
| const url = `${postOfficeEndpoint}/po-server/message?templateName=${encodeURIComponent(templateName)}&locale=${encodeURIComponent(locale)}`; | ||
|
|
||
| log.info(`[email-service] Sending ${templateName} email to ${recipients.length} recipient(s)`); | ||
|
|
||
| const response = await fetch(url, { | ||
| method: 'POST', | ||
| headers: { | ||
| Accept: 'application/json', | ||
| Authorization: `IMS ${accessToken}`, | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| body, | ||
| }); | ||
|
|
||
| result.statusCode = response.status; | ||
| result.success = response.status === 200; | ||
|
|
||
| if (!result.success) { | ||
| const responseText = await response.text().catch(() => '(unable to read response body)'); | ||
| result.error = `Post Office returned ${response.status}: ${responseText}`; | ||
| log.error(`Email send failed for template ${templateName}: ${result.error}`); | ||
| } | ||
| } catch (error) { | ||
| result.error = error.message; | ||
| log.error(`Email send error for template ${templateName}: ${error.message}`); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.