diff --git a/backend/src/routes/website-api/index.ts b/backend/src/routes/website-api/index.ts index 88ae131..5799d99 100644 --- a/backend/src/routes/website-api/index.ts +++ b/backend/src/routes/website-api/index.ts @@ -11,7 +11,7 @@ import CreateUserResolver from './resolvers/authenticate-prompt/create-user-reso import LoginResolver from './resolvers/authenticate-prompt/login-resolver'; import PromptInfoResolver from './resolvers/authenticate-prompt/prompt-info-resolver'; import SetSymbolResolver from './resolvers/authenticate-prompt/set-symbol-resolver'; -import CreateApplicationResolver from './resolvers/developer/create-application'; +import ApplicationResolver from './resolvers/developer/application'; import LoginStateResolver from './resolvers/developer/get-login-state'; import type { WebsiteAPIContext } from './types'; @@ -24,7 +24,7 @@ export default async function registerWebsiteApi(server: MyFastifyInstance): Pro SetSymbolResolver, CreateUserResolver, LoginStateResolver, - CreateApplicationResolver, + ApplicationResolver, ], }); const apolloServer = new ApolloServer({ diff --git a/backend/src/routes/website-api/models/application-info.ts b/backend/src/routes/website-api/models/application-info.ts index 543c3a5..8dacb31 100644 --- a/backend/src/routes/website-api/models/application-info.ts +++ b/backend/src/routes/website-api/models/application-info.ts @@ -1,7 +1,28 @@ import { Field, ObjectType } from 'type-graphql'; +import type Application from '../../../database/entities/application'; @ObjectType() export default class ApplicationInfo { @Field(() => String) public id!: string; + + @Field(() => String) + public name!: string; + + @Field(() => String, { + nullable: true, + }) + public iconUrl!: string | null; + + @Field(() => String) + public iconColor!: string; + + public static fromEntity(entity: Application): ApplicationInfo { + return { + id: entity._id.toHexString(), + iconColor: entity.iconColor, + iconUrl: entity.iconUrl, + name: entity.name, + }; + } } diff --git a/backend/src/routes/website-api/resolvers/developer/create-application.ts b/backend/src/routes/website-api/resolvers/developer/application.ts similarity index 50% rename from backend/src/routes/website-api/resolvers/developer/create-application.ts rename to backend/src/routes/website-api/resolvers/developer/application.ts index 96ba598..cd93c45 100644 --- a/backend/src/routes/website-api/resolvers/developer/create-application.ts +++ b/backend/src/routes/website-api/resolvers/developer/application.ts @@ -1,14 +1,14 @@ /* eslint-disable class-methods-use-this */ import { UserInputError } from 'apollo-server-fastify'; import { - Arg, Ctx, Mutation, Resolver, UnauthorizedError, + Arg, Ctx, Mutation, Query, Resolver, UnauthorizedError, } from 'type-graphql'; import Application from '../../../../database/entities/application'; import ApplicationInfo from '../../models/application-info'; import type { WebsiteAPIContext } from '../../types'; -@Resolver(() => ApplicationInfo) -export default class CreateApplicationResolver { +@Resolver() +export default class ApplicationResolver { @Mutation(() => ApplicationInfo) public async createApplication( @Arg('name') name: string, @@ -28,8 +28,33 @@ export default class CreateApplicationResolver { application.name = name; await application.save(); - return { - id: application._id.toHexString(), - }; + return ApplicationInfo.fromEntity(application); + } + + @Query(() => [ApplicationInfo]) + public async applications( + @Ctx() { sessionData }: WebsiteAPIContext, + ): Promise { + if (!sessionData.loginState) throw new UnauthorizedError(); + const applications = await Application.find({ + where: { + developerId: sessionData.loginState.developerId, + }, + }); + return applications.map((app) => ApplicationInfo.fromEntity(app)); + } + + @Query(() => ApplicationInfo, { + nullable: true, + }) + public async application( + @Arg('id') id: string, + @Ctx() { sessionData }: WebsiteAPIContext, + ): Promise { + if (!sessionData.loginState) throw new UnauthorizedError(); + const application = await Application.findOne(id); + if (!application) return null; + if (!application.developerId.equals(sessionData.loginState.developerId)) throw new UnauthorizedError(); + return ApplicationInfo.fromEntity(application); } } diff --git a/website/src/compontents/developer/new-app-dialog.vue b/website/src/compontents/developer/new-app-dialog.vue index 1dcb0d9..07a63a8 100644 --- a/website/src/compontents/developer/new-app-dialog.vue +++ b/website/src/compontents/developer/new-app-dialog.vue @@ -64,7 +64,9 @@ export default class NewAppDialog extends Vue { @Watch('value') valueChanged(value: boolean) { - if (!value) this.name = ''; + if (value) return; + this.name = ''; + this.error = false; } get valid() {