Upsert user personalization in db

This commit is contained in:
Hongbo Wu 2022-11-11 16:11:09 +08:00
parent 67b26be547
commit 917396c976
4 changed files with 28 additions and 12 deletions

View file

@ -1936,6 +1936,7 @@ export type SetUserPersonalizationError = {
};
export enum SetUserPersonalizationErrorCode {
NotFound = 'NOT_FOUND',
Unauthorized = 'UNAUTHORIZED'
}

View file

@ -1432,6 +1432,7 @@ type SetUserPersonalizationError {
}
enum SetUserPersonalizationErrorCode {
NOT_FOUND
UNAUTHORIZED
}

View file

@ -3,33 +3,46 @@ import {
GetUserPersonalizationResult,
MutationSetUserPersonalizationArgs,
SetUserPersonalizationError,
SetUserPersonalizationErrorCode,
SetUserPersonalizationSuccess,
SortOrder,
} from '../../generated/graphql'
import { authorized } from '../../utils/helpers'
import { UserPersonalization } from '../../entity/user_personalization'
import { AppDataSource } from '../../server'
import { setClaims } from '../../entity/utils'
import { getRepository, setClaims } from '../../entity/utils'
export const setUserPersonalizationResolver = authorized<
SetUserPersonalizationSuccess,
SetUserPersonalizationError,
MutationSetUserPersonalizationArgs
>(async (_, { input }, { claims: { uid } }) => {
const updatedUserPersonalization =
await AppDataSource.transaction<UserPersonalization>(
async (entityManager) => {
await setClaims(entityManager, uid)
>(async (_, { input }, { claims: { uid }, log }) => {
log.info('setUserPersonalizationResolver', { uid, input })
return entityManager.getRepository(UserPersonalization).save({
user: { id: uid },
...input,
})
}
const result = await AppDataSource.transaction(async (entityManager) => {
await setClaims(entityManager, uid)
return entityManager.getRepository(UserPersonalization).upsert(
{
user: { id: uid },
...input,
},
['user']
)
})
if (result.identifiers.length === 0) {
return {
errorCodes: [SetUserPersonalizationErrorCode.NotFound],
}
}
const updatedUserPersonalization = await getRepository(
UserPersonalization
).findOneBy({ id: result.identifiers[0].id as string })
// Cast SortOrder from string to enum
const librarySortOrder = updatedUserPersonalization.librarySortOrder as
const librarySortOrder = updatedUserPersonalization?.librarySortOrder as
| SortOrder
| null
| undefined

View file

@ -989,6 +989,7 @@ const schema = gql`
| SetUserPersonalizationError
enum SetUserPersonalizationErrorCode {
UNAUTHORIZED
NOT_FOUND
}
type SetUserPersonalizationError {
errorCodes: [SetUserPersonalizationErrorCode!]!