-
Notifications
You must be signed in to change notification settings - Fork 44
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
Mkaleem.neslit.10140.email digest #10338
Open
KaleemNeslit
wants to merge
9
commits into
master
Choose a base branch
from
mkaleem.neslit.10140.email_Digest
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1a5fe9d
added the community_avatar in notification NewUpvotes
KaleemNeslit c343fef
Merge branch 'master' of https://github.com/hicommonwealth/commonwealth
KaleemNeslit 186f66f
Merge branch 'master' of https://github.com/hicommonwealth/commonwealth
KaleemNeslit 366ba57
updated the Digest email with dynamic data
KaleemNeslit fb81950
fixed the test issues
KaleemNeslit 826595f
remove extra files
KaleemNeslit adec139
added the option for admin to enable digest email
KaleemNeslit 13fd08a
fixed test
KaleemNeslit 408e7e9
remove unused variable
KaleemNeslit 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 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 |
---|---|---|
|
@@ -101,9 +101,15 @@ export const EnrichedThread = Thread.extend({ | |
icon_url: z | ||
.string() | ||
.describe('The icon url of the community that the thread belongs to'), | ||
author: z.string().nullish().optional(), | ||
}); | ||
|
||
export const GetDigestEmailData = { | ||
input: z.object({}), | ||
output: z.record(z.string(), z.array(EnrichedThread)), | ||
input: z.object({ | ||
user_id: z.string(), | ||
}), | ||
output: z.object({ | ||
threads: z.array(EnrichedThread), | ||
numberOfThreads: z.number(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. snake_case as per api convention |
||
}), | ||
}; |
This file contains 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 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,30 @@ | ||
import { type Command } from '@hicommonwealth/core'; | ||
import * as schemas from '@hicommonwealth/schemas'; | ||
import { models } from '../database'; | ||
import { isSuperAdmin } from '../middleware'; | ||
import { mustExist } from '../middleware/guards'; | ||
|
||
export function EnableDigestEmail(): Command<any> { | ||
return { | ||
...schemas.enableEmailDigest, | ||
auth: [isSuperAdmin], | ||
body: async ({ payload }) => { | ||
const community = await models.Community.findOne({ | ||
where: { | ||
id: payload.communityId, | ||
}, | ||
}); | ||
mustExist('community', community); | ||
|
||
const [affectedCount] = await models.Community.update( | ||
{ include_in_digest_email: true }, | ||
{ | ||
where: { id: payload.communityId }, | ||
}, | ||
); | ||
return { | ||
success: affectedCount > 0, | ||
}; | ||
}, | ||
}; | ||
} |
This file contains 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './EnableDigestEmail'; | ||
export * from './TriggerNotificationsWorkflow.command'; |
This file contains 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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import { ExternalServiceUserIds, dispose, query } from '@hicommonwealth/core'; | ||
import { models } from '@hicommonwealth/model'; | ||
import { Community } from '@hicommonwealth/schemas'; | ||
import { Community, User } from '@hicommonwealth/schemas'; | ||
|
||
import { expect } from 'chai'; | ||
import { afterAll, afterEach, beforeAll, describe, test } from 'vitest'; | ||
import { z } from 'zod'; | ||
|
@@ -12,8 +13,13 @@ describe('Digest email lifecycle', () => { | |
let communityOne: z.infer<typeof Community> | undefined; | ||
let communityTwo: z.infer<typeof Community> | undefined; | ||
let communityThree: z.infer<typeof Community> | undefined; | ||
let recipientUser: z.infer<typeof User> | undefined; | ||
|
||
beforeAll(async () => { | ||
[recipientUser] = await seed('User', { | ||
isAdmin: false, | ||
selected_community_id: null, | ||
}); | ||
const [authorUser] = await seed('User', { | ||
isAdmin: false, | ||
selected_community_id: null, | ||
|
@@ -85,9 +91,14 @@ describe('Digest email lifecycle', () => { | |
email: '[email protected]', | ||
}, | ||
}, | ||
payload: {}, | ||
payload: { | ||
user_id: String(recipientUser!.id), | ||
}, | ||
}); | ||
expect(res).to.deep.equal({ | ||
threads: [], | ||
numberOfThreads: 0, | ||
}); | ||
expect(res).to.deep.equal({}); | ||
await models.Community.update( | ||
{ | ||
include_in_digest_email: false, | ||
|
@@ -119,9 +130,14 @@ describe('Digest email lifecycle', () => { | |
email: '[email protected]', | ||
}, | ||
}, | ||
payload: {}, | ||
payload: { | ||
user_id: String(recipientUser!.id), | ||
}, | ||
}); | ||
expect(res).to.deep.equal({ | ||
threads: [], | ||
numberOfThreads: 0, | ||
}); | ||
expect(res).to.deep.equal({}); | ||
}); | ||
|
||
test('should return enriched threads for each community', async () => { | ||
|
@@ -148,11 +164,20 @@ describe('Digest email lifecycle', () => { | |
email: '[email protected]', | ||
}, | ||
}, | ||
payload: {}, | ||
payload: { | ||
user_id: String(recipientUser!.id), | ||
}, | ||
}); | ||
|
||
expect(res![communityOne!.id!]!.length).to.equal(2); | ||
expect(res![communityTwo!.id!]!.length).to.equal(1); | ||
const filtercommunityOne = res?.threads.filter( | ||
(thread) => thread.community_id == communityOne!.id, | ||
); | ||
const filtercommunityTwo = res?.threads.filter( | ||
(thread) => thread.community_id == communityTwo!.id, | ||
); | ||
|
||
expect(filtercommunityOne!.length).to.equal(2); | ||
expect(filtercommunityTwo!.length).to.equal(1); | ||
|
||
delete threadOne?.Address; | ||
delete threadOne?.collaborators; | ||
|
@@ -167,20 +192,30 @@ describe('Digest email lifecycle', () => { | |
delete threadFour?.reactions; | ||
delete threadFour?.ThreadVersionHistories; | ||
|
||
expect(res![communityOne!.id!]![0]!).to.deep.equal({ | ||
name: communityOne!.name, | ||
icon_url: communityOne!.icon_url, | ||
...threadOne, | ||
}); | ||
expect(res![communityOne!.id!]![1]!).to.deep.equal({ | ||
name: communityOne!.name, | ||
icon_url: communityOne!.icon_url, | ||
...threadTwo, | ||
}); | ||
expect(res![communityTwo!.id!]![0]!).to.deep.equal({ | ||
name: communityTwo!.name, | ||
icon_url: communityTwo!.icon_url, | ||
...threadFour, | ||
}); | ||
if (filtercommunityOne && filtercommunityOne.length > 0) { | ||
expect(filtercommunityOne[0]).to.deep.equal({ | ||
name: communityOne!.name, | ||
icon_url: communityOne!.icon_url, | ||
author: null, | ||
...threadOne, | ||
}); | ||
} | ||
|
||
if (filtercommunityOne && filtercommunityOne.length >= 1) { | ||
expect(filtercommunityOne[1]).to.deep.equal({ | ||
name: communityOne!.name, | ||
icon_url: communityOne!.icon_url, | ||
author: null, | ||
...threadTwo, | ||
}); | ||
} | ||
if (filtercommunityTwo && filtercommunityTwo.length > 0) { | ||
expect(filtercommunityTwo[0]).to.deep.equal({ | ||
name: communityTwo!.name, | ||
icon_url: communityTwo!.icon_url, | ||
author: null, | ||
...threadFour, | ||
}); | ||
} | ||
}); | ||
}); |
This file contains 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 |
---|---|---|
|
@@ -17,4 +17,13 @@ export const TriggerNotificationsWorkflow = { | |
}), | ||
}; | ||
|
||
export const enableEmailDigest = { | ||
input: z.object({ | ||
communityId: z.string(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
}), | ||
output: z.object({ | ||
success: z.boolean(), | ||
}), | ||
}; | ||
|
||
export type Type1 = z.infer<typeof TriggerNotificationsWorkflow.input>; |
5 changes: 5 additions & 0 deletions
5
packages/commonwealth/client/scripts/state/api/superAdmin/enableDigestEmail.ts
This file contains 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,5 @@ | ||
import { trpc } from 'utils/trpcClient'; | ||
const useEnableDigestEmail = () => { | ||
return trpc.superAdmin.enableDigestEmail.useMutation({}); | ||
}; | ||
export default useEnableDigestEmail; |
4 changes: 2 additions & 2 deletions
4
packages/commonwealth/client/scripts/state/api/superAdmin/index.ts
This file contains 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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import useEnableDigestEmail from './enableDigestEmail'; | ||
import useTriggerNotificationsWorkflowMutation from './triggerNotificationsWorkflow'; | ||
|
||
export { useTriggerNotificationsWorkflowMutation }; | ||
export { useEnableDigestEmail, useTriggerNotificationsWorkflowMutation }; |
This file contains 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 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
78 changes: 78 additions & 0 deletions
78
packages/commonwealth/client/scripts/views/pages/AdminPanel/EnableDigestEmail.tsx
This file contains 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,78 @@ | ||
import { useEnableDigestEmail } from 'client/scripts/state/api/superAdmin'; | ||
import { notifyError, notifySuccess } from 'controllers/app/notifications'; | ||
import React, { useState } from 'react'; | ||
import { useGetCommunityByIdQuery } from 'state/api/communities'; | ||
import { useDebounce } from 'usehooks-ts'; | ||
import { CWText } from '../../components/component_kit/cw_text'; | ||
import { CWButton } from '../../components/component_kit/new_designs/CWButton'; | ||
import { CWTextInput } from '../../components/component_kit/new_designs/CWTextInput'; | ||
import './AdminPanel.scss'; | ||
|
||
const EnableDigestEmail = () => { | ||
const [communityId, setCommunityId] = useState<string>(''); | ||
const debouncedCommunityLookupId = useDebounce<string | undefined>( | ||
communityId, | ||
500, | ||
); | ||
const { mutateAsync: triggerEnableDigestEmail } = useEnableDigestEmail(); | ||
|
||
const { data: communityLookupData, isLoading: isLoadingCommunityLookupData } = | ||
useGetCommunityByIdQuery({ | ||
id: debouncedCommunityLookupId || '', | ||
enabled: !!debouncedCommunityLookupId, | ||
}); | ||
|
||
const setCommunityIdInput = (e) => { | ||
setCommunityId(e?.target?.value?.trim() || ''); | ||
}; | ||
|
||
const communityNotFound = | ||
!isLoadingCommunityLookupData && | ||
(!communityLookupData || | ||
Object.keys(communityLookupData || {})?.length === 0); | ||
|
||
const communityIdInputError = (() => { | ||
if (communityNotFound) return 'Community not found'; | ||
return ''; | ||
})(); | ||
|
||
const buttonEnabled = | ||
!isLoadingCommunityLookupData && | ||
communityId.length > 0 && | ||
!communityNotFound; | ||
|
||
const update = () => { | ||
if (Object.keys(communityLookupData || {}).length > 0) { | ||
try { | ||
triggerEnableDigestEmail({ communityId }); | ||
notifySuccess('Success'); | ||
} catch (error) { | ||
notifyError('Error'); | ||
console.error(error); | ||
} | ||
} | ||
}; | ||
|
||
return ( | ||
<div className="TaskGroup"> | ||
<CWText type="h4">Enable Digest email for Admin</CWText> | ||
<div className="TaskRow"> | ||
<CWTextInput | ||
label="Community Id" | ||
value={communityId} | ||
onInput={setCommunityIdInput} | ||
customError={communityIdInputError} | ||
placeholder="Enter a community id" | ||
/> | ||
<CWButton | ||
label="Enable" | ||
className="TaskButton" | ||
disabled={!buttonEnabled} | ||
onClick={update} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default EnableDigestEmail; |
This file contains 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
numberOfThreads seems redundant if same as the length of the threads array
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need this to add condition in knock . i did not find way to check array length there