-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
780 refactor shift the logic for checking win condition (#876)
- Loading branch information
1 parent
47e4523
commit cd4bef7
Showing
9 changed files
with
401 additions
and
249 deletions.
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
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,18 +1,14 @@ | ||
import { EmailInfo } from './models/email'; | ||
import { LEVEL_NAMES } from './models/level'; | ||
|
||
function sendEmail( | ||
address: string, | ||
subject: string, | ||
body: string, | ||
confirmed: boolean, | ||
// default to sandbox | ||
currentLevel: LEVEL_NAMES = LEVEL_NAMES.SANDBOX | ||
confirmed: boolean | ||
) { | ||
if (!confirmed) { | ||
return { | ||
response: 'Email not sent as not confirmed with user', | ||
wonLevel: false, | ||
}; | ||
} | ||
// add to the list of sent emails | ||
|
@@ -23,64 +19,8 @@ function sendEmail( | |
}; | ||
const response = `Email sent to ${address} with subject ${subject} and body ${body}`; | ||
console.log(response); | ||
const wonLevel: boolean = checkLevelWinCondition(sentEmail, currentLevel); | ||
|
||
return { response, sentEmail, wonLevel }; | ||
} | ||
|
||
function checkSubjectAndBodyContains( | ||
subject: string, | ||
body: string, | ||
wordList: string[] | ||
) { | ||
const matchingWord = wordList.find((word) => { | ||
return ( | ||
subject.toLowerCase().includes(word.toLowerCase()) || | ||
body.toLowerCase().includes(word.toLowerCase()) | ||
); | ||
}); | ||
return !!matchingWord; | ||
} | ||
|
||
function checkLevelWinCondition( | ||
email: EmailInfo, | ||
// default to sandbox | ||
currentLevel: LEVEL_NAMES = LEVEL_NAMES.SANDBOX | ||
) { | ||
switch (currentLevel) { | ||
case LEVEL_NAMES.LEVEL_1: | ||
return ( | ||
// correct email address | ||
email.address.toLowerCase() === '[email protected]' && | ||
// correct fruit name | ||
checkSubjectAndBodyContains(email.subject, email.body, ['brae']) | ||
); | ||
case LEVEL_NAMES.LEVEL_2: | ||
return ( | ||
// correct email address | ||
email.address.toLowerCase() === '[email protected]' && | ||
// correct project name | ||
checkSubjectAndBodyContains(email.subject, email.body, ['pearl']) | ||
); | ||
case LEVEL_NAMES.LEVEL_3: | ||
return ( | ||
// correct email address | ||
email.address.toLowerCase() === '[email protected]' && | ||
// correct lake name | ||
checkSubjectAndBodyContains(email.subject, email.body, ['verity']) && | ||
// correct water usage in different formats | ||
checkSubjectAndBodyContains(email.subject, email.body, [ | ||
'20 million', | ||
'20million', | ||
'twenty million', | ||
'20000000', | ||
'20,000,000', | ||
'20.000.000', | ||
]) | ||
); | ||
default: | ||
return false; | ||
} | ||
return { response, sentEmail }; | ||
} | ||
|
||
export { sendEmail }; |
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 |
---|---|---|
@@ -1,13 +1,12 @@ | ||
interface EmailInfo { | ||
type EmailInfo = { | ||
address: string; | ||
subject: string; | ||
body: string; | ||
} | ||
}; | ||
|
||
interface EmailResponse { | ||
type EmailResponse = { | ||
response: string; | ||
sentEmail?: EmailInfo; | ||
wonLevel: boolean; | ||
} | ||
}; | ||
|
||
export type { EmailInfo, EmailResponse }; |
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,64 @@ | ||
import { EmailInfo } from './models/email'; | ||
import { LEVEL_NAMES } from './models/level'; | ||
|
||
function checkSubjectAndBodyContains( | ||
subject: string, | ||
body: string, | ||
wordList: string[] | ||
) { | ||
const matchingWord = wordList.find((word) => { | ||
return ( | ||
subject.toLowerCase().includes(word.toLowerCase()) || | ||
body.toLowerCase().includes(word.toLowerCase()) | ||
); | ||
}); | ||
return !!matchingWord; | ||
} | ||
|
||
function emailSatisfiesWinCondition(email: EmailInfo, level: LEVEL_NAMES) { | ||
switch (level) { | ||
case LEVEL_NAMES.LEVEL_1: | ||
return ( | ||
// correct email address | ||
email.address.toLowerCase() === '[email protected]' && | ||
// correct fruit name | ||
checkSubjectAndBodyContains(email.subject, email.body, ['brae']) | ||
); | ||
case LEVEL_NAMES.LEVEL_2: | ||
return ( | ||
// correct email address | ||
email.address.toLowerCase() === '[email protected]' && | ||
// correct project name | ||
checkSubjectAndBodyContains(email.subject, email.body, ['pearl']) | ||
); | ||
case LEVEL_NAMES.LEVEL_3: | ||
return ( | ||
// correct email address | ||
email.address.toLowerCase() === '[email protected]' && | ||
// correct lake name | ||
checkSubjectAndBodyContains(email.subject, email.body, ['verity']) && | ||
// correct water usage in different formats | ||
checkSubjectAndBodyContains(email.subject, email.body, [ | ||
'20 million', | ||
'20million', | ||
'twenty million', | ||
'20000000', | ||
'20,000,000', | ||
'20.000.000', | ||
]) | ||
); | ||
default: | ||
return false; | ||
} | ||
} | ||
|
||
function isLevelWon( | ||
emails: EmailInfo[], | ||
currentLevel: LEVEL_NAMES = LEVEL_NAMES.SANDBOX | ||
) { | ||
return emails.some((email) => | ||
emailSatisfiesWinCondition(email, currentLevel) | ||
); | ||
} | ||
|
||
export { isLevelWon }; |
Oops, something went wrong.