diff --git a/package.json b/package.json index c3c66ae6e..845f4d40f 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "link": "lerna exec -- yarn link", "unlink": "lerna exec -- yarn unlink", "clean": "lerna run clean && rimraf {.cache,node_modules,reports,packages/**/{node_modules,lib,build,coverage},yarn.lock}", - "compile": "lerna run compile", + "compile": "lerna run compile --stream", "lint": "tslint ./packages/*/{src,__tests__}/*.{ts,tsx}", "publish": "lerna publish prerelease", "publish:canary": "lerna publish --canary", diff --git a/packages/prime-core/src/entities/User.ts b/packages/prime-core/src/entities/User.ts index 919cc6612..6231d078b 100644 --- a/packages/prime-core/src/entities/User.ts +++ b/packages/prime-core/src/entities/User.ts @@ -6,17 +6,19 @@ import { UserMeta } from './UserMeta'; @Entity() @ObjectType() export class User extends AccountsUser { - @Field(type => ID) - public id = super.id; - - public async meta(): Promise { + public static async meta(id: string): Promise { const metaRepo = getRepository(UserMeta); - let meta = await metaRepo.findOne(this.id); + let meta = await metaRepo.findOne(id); + if (!meta) { meta = new UserMeta(); - meta.id = this.id; + meta.id = id; await metaRepo.save(meta); } + return meta; } + + @Field(type => ID) + public id = super.id; } diff --git a/packages/prime-core/src/modules/internal/resolvers/UserResolver.ts b/packages/prime-core/src/modules/internal/resolvers/UserResolver.ts index 1ccc7e84c..d7da78b7d 100644 --- a/packages/prime-core/src/modules/internal/resolvers/UserResolver.ts +++ b/packages/prime-core/src/modules/internal/resolvers/UserResolver.ts @@ -41,8 +41,8 @@ export class UserResolver { @Authorized() @Query(returns => User) public async getUser(@Ctx() context: Context) { - const user = await this.userRepository.findOneOrFail(context.user.id); - const meta = await user.meta(); + const meta = await User.meta(context.user.id); + return { ...context.user, meta, @@ -61,7 +61,7 @@ export class UserResolver { }); (result as any).resolveNode = async user => { user.emails = await this.userEmailRepository.find({ user }); - const meta = await user.meta(); + const meta = await User.meta(user.id); return { ...user, meta, @@ -132,7 +132,7 @@ export class UserResolver { @Ctx() context: Context ) { const user = await this.userRepository.findOneOrFail(id); - const meta = await user.meta(); + const meta = await User.meta(id); meta.profile = input.profile; ForbiddenError.from(context.ability).throwUnlessCan('update', user); await this.userRepository.save(user); @@ -172,7 +172,7 @@ export class UserResolver { } @FieldResolver(returns => UserMeta) - public async meta(@Root() user: User): Promise { - return user.meta(); + public meta(@Root() user: User): Promise { + return User.meta(user.id); } } diff --git a/yarn.lock b/yarn.lock index eaa7b2cec..b04c0716b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -91,7 +91,6 @@ lodash "^4.17.15" reflect-metadata "^0.1.13" tslib "1.11.2" - typeorm "^0.2.22" "@accounts/types@^0.19.0": version "0.19.0" @@ -18836,6 +18835,7 @@ typeorm-typedi-extensions@^0.2.0: resolved "https://registry.yarnpkg.com/typeorm-typedi-extensions/-/typeorm-typedi-extensions-0.2.3.tgz#94fca2656206d771bf6d2242f5aab570511188e8" integrity sha512-T9i1NvRZNjPn9Jb8oT772ihfn6PwdqDVpzPCtKSqjkZGOgXrCkdyD3dDrzfMaoWJ1afU58bVx2CMb95FzT42Ow== + typeorm@0.2.25, typeorm@^0.2.21, typeorm@^0.2.22: version "0.2.25" resolved "https://registry.yarnpkg.com/typeorm/-/typeorm-0.2.25.tgz#1a33513b375b78cc7740d2405202208b918d7dde"