Skip to content
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

cascade language changes to graph. #87

Merged
merged 2 commits into from
Jun 8, 2023
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
24 changes: 12 additions & 12 deletions src/lib/graph/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const personalInformationFields = `
family_name
gender
given_names
language_code
location_name
year_of_birth`

Expand Down Expand Up @@ -289,28 +290,27 @@ export async function updateGraphPersonalInformation(
graphql: GraphQLClient,
personal_information: Partial<PersonalInformation>,
phoneNumber: string,
redis: RedisClient
redis: RedisClient,
user_identifier: number
) {
const updatedFields = getRequestedFields(personal_information);
const constraint = 'personal_information_user_identifier_key';
const query = `
mutation UpdatePersonalInformation($personal_information: personal_information_insert_input!) {
insert_personal_information_one(
object: $personal_information,
on_conflict: { constraint: ${constraint}, update_columns: [${updatedFields}] }
) { ${updatedFields} }
mutation UpdatePersonalInformation($personal_information: personal_information_set_input!, $user_identifier: Int!) {
update_personal_information(where: {user_identifier: {_eq: $user_identifier}}, _set: $personal_information) {
returning { ${updatedFields} }
}
}`;

const variables = { personal_information };
const result = await graphql.request<{ insert_personal_information_one: Partial<PersonalInformation> }>(query, variables);
const personalInformation = result.insert_personal_information_one
const variables = { personal_information, user_identifier };
const result = await graphql.request<{ update_personal_information: { returning: Partial<PersonalInformation>[] } }>(query, variables);
const personalInformation = result.update_personal_information.returning[0]
const tag = (personalInformation?.given_names && personalInformation?.family_name) ? `${personalInformation.given_names} ${personalInformation.family_name} ${phoneNumber}`: phoneNumber

await new UserService(phoneNumber, redis).update({
graph:{
personalInformation: result.insert_personal_information_one
personalInformation
},
tag
})
return result.insert_personal_information_one;
return personalInformation;
}
9 changes: 7 additions & 2 deletions src/machines/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,12 @@ const stateMachine = createMachine<LanguagesContext, MachineEvent>({
})

async function initiateLanguageChange(context: LanguagesContext, event: any) {
const { connections: { db, redis }, user: { account: { phone_number } }, data } = context
const {
connections: {
db, graphql, redis
},
user: { account: { address, phone_number }, graph: { user: { id } } },
data } = context
const { input } = event

await validatePin(context, input)
Expand All @@ -166,7 +171,7 @@ async function initiateLanguageChange(context: LanguagesContext, event: any) {
}

try {
await new AccountService(db, redis.persistent).updateLanguage(data.selectedLanguage, phone_number)
await new AccountService(db, redis.persistent).updateLanguage(address, graphql, id, data.selectedLanguage, phone_number)
return { success: true }
} catch (error: any) {
throw new MachineError(LanguageError.CHANGE_ERROR, `${error.message} - ${error.stack}`)
Expand Down
3 changes: 1 addition & 2 deletions src/machines/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,8 @@ async function initiateProfileChange(context: ProfileContext, event: any) {
if (context?.data?.personalInformation){
let updatedProfile: Partial<PersonalInformation> = {
...context.data.personalInformation,
user_identifier: graphUserId
}
await updateGraphPersonalInformation(address, graphql, updatedProfile, phone_number, redis.persistent)
await updateGraphPersonalInformation(address, graphql, updatedProfile, phone_number, redis.persistent, graphUserId)
}

if (context?.data?.marketplaceName) {
Expand Down
1 change: 0 additions & 1 deletion src/machines/statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ async function authorizeStatementView(context: StatementContext, event: any) {

try {
const formattedStatement = await formatStatement(graphql, language, redis.persistent, statement || [])
console.log(`Formatted statement: ${JSON.stringify(formattedStatement)}`)
return { success: true, statement: formattedStatement }
} catch (error) {
throw new MachineError(StatementError.LOAD_ERROR, `Error loading statement.`)
Expand Down
7 changes: 4 additions & 3 deletions src/services/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { DeepPartial } from 'ts-essentials';
import { Account, AccountInterface, AccountStatus } from '@db/models/account';
import { User, UserService } from '@services/user';
import { GraphQLClient } from 'graphql-request';
import { updateGraphUser } from '@lib/graph/user';
import { updateGraphPersonalInformation, updateGraphUser } from '@lib/graph/user';
import { Guardian } from '@db/models/guardian';

export class AccountService {
Expand Down Expand Up @@ -136,10 +136,11 @@ export class AccountService {
await new UserService(phoneNumber, this.redis).update(data)
}

public async updateLanguage(language: Locales, phoneNumber: string) {
public async updateLanguage(address: string, graphql: GraphQLClient, graphUserId: number, language: Locales, phoneNumber: string) {
const results = await Promise.allSettled([
new Account(this.db).setLanguage(phoneNumber, language),
this.updateCache(phoneNumber, { account: { language } })
this.updateCache(phoneNumber, { account: { language } }),
updateGraphPersonalInformation(address, graphql, { language_code: language }, phoneNumber, this.redis, graphUserId)
])
await handleResults(results)
}
Expand Down