mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
Upsert user personalization in db
This commit is contained in:
parent
67b26be547
commit
917396c976
4 changed files with 28 additions and 12 deletions
|
|
@ -1936,6 +1936,7 @@ export type SetUserPersonalizationError = {
|
|||
};
|
||||
|
||||
export enum SetUserPersonalizationErrorCode {
|
||||
NotFound = 'NOT_FOUND',
|
||||
Unauthorized = 'UNAUTHORIZED'
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1432,6 +1432,7 @@ type SetUserPersonalizationError {
|
|||
}
|
||||
|
||||
enum SetUserPersonalizationErrorCode {
|
||||
NOT_FOUND
|
||||
UNAUTHORIZED
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -989,6 +989,7 @@ const schema = gql`
|
|||
| SetUserPersonalizationError
|
||||
enum SetUserPersonalizationErrorCode {
|
||||
UNAUTHORIZED
|
||||
NOT_FOUND
|
||||
}
|
||||
type SetUserPersonalizationError {
|
||||
errorCodes: [SetUserPersonalizationErrorCode!]!
|
||||
|
|
|
|||
Loading…
Reference in a new issue