Skip to content

JIRA-PLAT-346 #75

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 4 commits into from
Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app-constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const EVENT_ORIGINATOR = 'topcoder-member-api'
const EVENT_MIME_TYPE = 'application/json'

const TOPICS = {
MemberCreated: 'member.action.profile.create',
MemberUpdated: 'member.action.profile.update',
EmailChanged: 'member.action.email.profile.emailchange.verification',
MemberTraitCreated: 'member.action.profile.trait.create',
Expand Down
16 changes: 15 additions & 1 deletion src/services/MemberService.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ function omitMemberAttributes (currentUser, mb) {
async function getMember (currentUser, handle, query) {
// validate and parse query parameter
const selectFields = helper.parseCommaSeparatedString(query.fields, MEMBER_FIELDS) || MEMBER_FIELDS

// query member from Elasticsearch
const esQuery = {
index: config.ES.MEMBER_PROFILE_ES_INDEX,
Expand All @@ -80,11 +81,24 @@ async function getMember (currentUser, handle, query) {
}
// Search with constructed query
let members = await esClient.search(esQuery)

if (members.hits.total === 0) {
throw new errors.NotFoundError(`Member with handle: "${handle}" doesn't exist`)
logger.debug(`Member ${handle} not found in ES. Lookup in DynamoDB...`)
try {
// Check if the member handle exists in DynamoDB
members = [ await helper.getMemberByHandle(handle) ]
// Memember was found in DynamoDB but not ES. Send message to member-processor-es
// to index the member in ES. It's safe to use the "create" topic since the processor
// will only create a new item of the item doesn't exist, otherwise it'll perform an update operation.
helper.postBusEvent(constants.TOPICS.MemberCreated, members[0].originalItem())
} catch (e) {
logger.debug(`Member ${handle} not found in DynamoDB.`)
throw new errors.NotFoundError(`Member with handle: "${handle}" doesn't exist`)
}
} else {
members = _.map(members.hits.hits, '_source')
}

// get the 'maxRating' from stats
if (_.includes(selectFields, 'maxRating')) {
for (let i = 0; i < members.length; i += 1) {
Expand Down
23 changes: 23 additions & 0 deletions src/services/MemberTraitService.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,29 @@ async function getTraits (currentUser, handle, query) {
// Search with constructed query
const docs = await esClient.search(esQuery)
let result = _.map(docs.hits.hits, (item) => item._source)

if (result.length == 0) {
logger.debug(`MemberTraits for member ${handle} not found in ES. Lookup in DynamoDB...`)
const resultDynamo = await helper.query('MemberTrait', { userId: { eq: member.userId } })
result = resultDynamo.map(traits => {
traits = traits.originalItem()
traits.traits = JSON.parse(traits.traits)

if (traits.createdAt != null) {
traits.createdAt = new Date(traits.createdAt).getTime()
}

if (traits.updatedAt != null) {
traits.updatedAt = new Date(traits.createdAt).getTime()
}

// index in ES so subsequent API calls pull data from ES
helper.postBusEvent(constants.TOPICS.MemberTraitUpdated, traits)

return traits
})
}

// keep only those of given trait ids
if (traitIds) {
result = _.filter(result, (item) => _.includes(traitIds, item.traitId))
Expand Down