@@ -31,6 +31,7 @@ import {
3131} from '@crowd/data-access-layer/src/member_identities'
3232import { getMemberNoMerge } from '@crowd/data-access-layer/src/member_merge'
3333import {
34+ IActivityRelationCreateOrUpdateData ,
3435 IDbActivity ,
3536 IDbActivityCreateData ,
3637 IDbActivityUpdateData ,
@@ -647,11 +648,23 @@ export default class ActivityService extends LoggerBase {
647648 'timestamp' ,
648649 'memberId' ,
649650 'objectMemberId' ,
651+ 'organizationId' ,
652+ 'conversationId' ,
653+ 'parentId' ,
650654 'type' ,
651655 'sourceId' ,
656+ 'sourceParentId' ,
652657 'channel' ,
653658 'segmentId' ,
654659 'platform' ,
660+ 'username' ,
661+ 'objectMemberUsername' ,
662+ 'sentimentScore' ,
663+ 'gitInsertions' ,
664+ 'gitDeletions' ,
665+ 'score' ,
666+ 'isContribution' ,
667+ 'pullRequestReviewState' ,
655668 ] ,
656669 ) ,
657670 this . log ,
@@ -1489,10 +1502,11 @@ export default class ActivityService extends LoggerBase {
14891502 await insertActivities ( this . client , toUpsert )
14901503 }
14911504
1492- await createOrUpdateRelations (
1493- this . pgQx ,
1494- preparedForUpsert . map ( ( a ) => {
1495- return {
1505+ // Filter out relations that don't need updates to avoid unnecessary database writes
1506+ let skippedCount = 0
1507+ const relationsToUpsert = preparedForUpsert
1508+ . map ( ( a ) => {
1509+ const relationData : IActivityRelationCreateOrUpdateData = {
14961510 activityId : a . payload . id ,
14971511 segmentId : a . payload . segmentId ,
14981512 memberId : a . payload . memberId ,
@@ -1515,8 +1529,35 @@ export default class ActivityService extends LoggerBase {
15151529 isContribution : a . payload . isContribution ,
15161530 pullRequestReviewState : a . payload . attributes ?. reviewState as string ,
15171531 }
1518- } ) ,
1519- )
1532+
1533+ return {
1534+ relation : relationData ,
1535+ activity : a ,
1536+ }
1537+ } )
1538+ . filter ( ( data ) => {
1539+ // If we have an existing relation, check if it actually needs updating
1540+ if ( data . activity . dbActivityRelation ) {
1541+ if ( ! this . needsActivityRelationUpdate ( data . activity . dbActivityRelation , data . relation ) ) {
1542+ skippedCount ++
1543+ return false
1544+ }
1545+ }
1546+
1547+ return true
1548+ } )
1549+ . map ( ( d ) => d . relation )
1550+
1551+ if ( relationsToUpsert . length > 0 ) {
1552+ this . log . trace (
1553+ `[ACTIVITY] Upserting ${ relationsToUpsert . length } activity relations (filtered from ${ preparedForUpsert . length } , skipped ${ skippedCount } )` ,
1554+ )
1555+ await createOrUpdateRelations ( this . pgQx , relationsToUpsert )
1556+ } else {
1557+ this . log . trace (
1558+ `[ACTIVITY] No activity relations need updating (all ${ preparedForUpsert . length } would only update updatedAt)` ,
1559+ )
1560+ }
15201561
15211562 const createdTypes = new Set < string > ( )
15221563 const createdChannels = new Set < string > ( )
@@ -1728,6 +1769,32 @@ export default class ActivityService extends LoggerBase {
17281769 return undefined
17291770 }
17301771
1772+ private needsActivityRelationUpdate (
1773+ existing : IDbActivityRelation ,
1774+ newData : IActivityRelationCreateOrUpdateData ,
1775+ ) : boolean {
1776+ // Compare all fields that would be updated in the ON CONFLICT clause
1777+ return (
1778+ existing . memberId !== newData . memberId ||
1779+ existing . objectMemberId !== ( newData . objectMemberId ?? null ) ||
1780+ existing . organizationId !== ( newData . organizationId ?? null ) ||
1781+ existing . platform !== newData . platform ||
1782+ existing . username !== newData . username ||
1783+ existing . objectMemberUsername !== ( newData . objectMemberUsername ?? null ) ||
1784+ existing . sourceId !== newData . sourceId ||
1785+ existing . sourceParentId !== ( newData . sourceParentId ?? null ) ||
1786+ existing . type !== newData . type ||
1787+ existing . timestamp !== newData . timestamp ||
1788+ existing . channel !== newData . channel ||
1789+ existing . sentimentScore !== newData . sentimentScore ||
1790+ existing . gitInsertions !== newData . gitInsertions ||
1791+ existing . gitDeletions !== newData . gitDeletions ||
1792+ existing . score !== newData . score ||
1793+ existing . isContribution !== newData . isContribution ||
1794+ existing . pullRequestReviewState !== ( newData . pullRequestReviewState ?? null )
1795+ )
1796+ }
1797+
17311798 private areTheSameMember (
17321799 platform : PlatformType ,
17331800 member1 : IMemberData ,
0 commit comments