Skip to content

Commit

Permalink
Add new automations triggering rules to automations worker (#1879)
Browse files Browse the repository at this point in the history
Co-authored-by: Loïc Saint-Roch <loicsaintroch@users.noreply.github.com>
  • Loading branch information
garrrikkotua and loicsaintroch authored Nov 24, 2023
1 parent 67b3511 commit d5cc9b4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface IRelevantAutomationData {
settings: AutomationSettings
trigger: AutomationTrigger
slackWebHook: string | null
createdAt: string
}

export interface IDbAutomationExecutionInsertData {
Expand Down Expand Up @@ -91,7 +92,8 @@ export class AutomationRepository extends RepositoryBase<AutomationRepository> {
a.type,
a.trigger,
a.settings,
s."slackWebHook"
s."slackWebHook",
a."createdAt"
from automations a
inner join settings s on s."tenantId" = a."tenantId"
where a."tenantId" = $(tenantId) and a.trigger = $(trigger) and a.state = $(state)
Expand Down
3 changes: 3 additions & 0 deletions services/apps/automations_worker/src/repos/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export interface IMemberData {

lastActivityId: string
lastActivity: IActivityData

joinedAt: string
}

export interface IActivityData {
Expand All @@ -21,6 +23,7 @@ export interface IActivityData {
platform: string
attributes: any
body: string
timestamp: string

// a bunch more but we just passing them as the payload - not needed here for now

Expand Down
54 changes: 35 additions & 19 deletions services/apps/automations_worker/src/services/automation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,18 @@ export class AutomationService {
): Promise<boolean> {
const settings = automation.settings as NewMemberSettings

let process = true
let shouldProcess = true

// check if member joined after automation was created
if (new Date(automation.createdAt) > new Date(member.joinedAt)) {
this.log.warn(
`Ignoring automation ${automation.id} - Member ${member.id} joined before automation!`,
)
shouldProcess = false
}

// check whether member platforms matches
if (settings.platforms && settings.platforms.length > 0) {
if (shouldProcess && settings.platforms && settings.platforms.length > 0) {
const platforms = Object.keys(member.username)
if (!platforms.some((platform) => settings.platforms.includes(platform))) {
this.log.warn(
Expand All @@ -56,11 +64,11 @@ export class AutomationService {
', ',
)}]`,
)
process = false
shouldProcess = false
}
}

if (process) {
if (shouldProcess) {
const hasAlreadyBeenTriggered = await this.automationRepo.hasAlreadyBeenTriggered(
automation.id,
member.id,
Expand All @@ -69,11 +77,11 @@ export class AutomationService {
this.log.warn(
`Ignoring automation ${automation.id} - Member ${member.id} was already processed!`,
)
process = false
shouldProcess = false
}
}

return process
return shouldProcess
}

async shouldProcessActivity(
Expand All @@ -82,65 +90,73 @@ export class AutomationService {
): Promise<boolean> {
const settings = automation.settings as NewActivitySettings

let process = true
let shouldProcess = true

// check if activity created after automation was created
if (new Date(automation.createdAt) > new Date(activity.timestamp)) {
this.log.warn(
`Ignoring automation ${automation.id} - Activity ${activity.id} was created before automation!`,
)
shouldProcess = false
}

// check whether activity type matches
if (settings.types && settings.types.length > 0) {
if (shouldProcess && settings.types && settings.types.length > 0) {
if (!settings.types.includes(activity.type)) {
this.log.warn(
`Ignoring automation ${automation.id} - Activity ${activity.id} type '${
activity.type
}' does not match automation setting types: [${settings.types.join(', ')}]`,
)
process = false
shouldProcess = false
}
}

// check whether activity platform matches
if (process && settings.platforms && settings.platforms.length > 0) {
if (shouldProcess && settings.platforms && settings.platforms.length > 0) {
if (!settings.platforms.includes(activity.platform)) {
this.log.warn(
`Ignoring automation ${automation.id} - Activity ${activity.id} platform '${
activity.platform
}' does not match automation setting platforms: [${settings.platforms.join(', ')}]`,
)
process = false
shouldProcess = false
}
}

// check whether activity content contains any of the keywords
if (process && settings.keywords && settings.keywords.length > 0) {
if (shouldProcess && settings.keywords && settings.keywords.length > 0) {
const body = (activity.body as string).toLowerCase()
if (!settings.keywords.some((keyword) => body.includes(keyword.trim().toLowerCase()))) {
this.log.warn(
`Ignoring automation ${automation.id} - Activity ${
activity.id
} content does not match automation setting keywords: [${settings.keywords.join(', ')}]`,
)
process = false
shouldProcess = false
}
}

if (
process &&
shouldProcess &&
!settings.teamMemberActivities &&
activity.member.attributes.isTeamMember &&
activity.member.attributes.isTeamMember.default
) {
this.log.warn(
`Ignoring automation ${automation.id} - Activity ${activity.id} belongs to a team member!`,
)
process = false
shouldProcess = false
}

if (activity?.member?.attributes?.isBot && activity?.member?.attributes?.isBot.default) {
this.log.warn(
`Ignoring automation ${automation.id} - Activity ${activity.id} belongs to a bot, cannot be processed automaticaly!`,
)
process = false
shouldProcess = false
}

if (process) {
if (shouldProcess) {
const hasAlreadyBeenTriggered = await this.automationRepo.hasAlreadyBeenTriggered(
automation.id,
activity.id,
Expand All @@ -149,11 +165,11 @@ export class AutomationService {
this.log.warn(
`Ignoring automation ${automation.id} - Activity ${activity.id} was already processed!`,
)
process = false
shouldProcess = false
}
}

return process
return shouldProcess
}

/**
Expand Down

0 comments on commit d5cc9b4

Please sign in to comment.