Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

109 lines
3.7KB

  1. import { Args, Context, Mutation, Resolver } from '@nestjs/graphql';
  2. import { Person } from '../models/Person';
  3. import { Client } from '../../client';
  4. import { PersonService } from '../person.service';
  5. import { HttpException } from '@nestjs/common';
  6. import {UUID} from '../../global/scalars/UUID'
  7. import {OrganizerService} from '../../organizer/organizer.service'
  8. import {EmailAddress} from '../../global/scalars/EmailAddress'
  9. import {checkPassword, secureHash} from '../../generate'
  10. @Resolver(() => Person)
  11. export class PersonResolverM {
  12. constructor(
  13. private readonly service: PersonService,
  14. ) {}
  15. @Mutation(() => Person, { nullable: false })
  16. async login(
  17. @Context('client') client: Client,
  18. @Args('email', { nullable: true }) email?: string,
  19. @Args('passwort', { nullable: true }) passwort?: string,
  20. @Args('token', { nullable: true }) token?: string
  21. ): Promise<Person> {
  22. await client.login({email, passwort, token});
  23. const newtoken: string = client.getToken();
  24. if (email && passwort && !newtoken) {
  25. throw new HttpException('Logindaten falsch', 403);
  26. }
  27. const tmp = await this.service.findOneById(client.getUser()?._id);
  28. if (!!(tmp as unknown as any).confirmCode) {
  29. throw new HttpException('E-Mail-Adresse noch nicht bestätigt!', 403);
  30. }
  31. return tmp;
  32. }
  33. @Mutation(() => Person, { nullable: false })
  34. async PersonRegister(
  35. @Context('client') client: Client,
  36. @Args('organizer', { type: () => UUID, nullable: false }) organizer: UUID,
  37. @Args('givenName', { nullable: false }) givenName: string,
  38. @Args('familyName', { nullable: false }) familyName: string,
  39. @Args('email', { type: () => EmailAddress, nullable: false }) email: EmailAddress,
  40. @Args('passwort', { nullable: false }) passwort: string,
  41. ): Promise<Person> {
  42. const organizerService = new OrganizerService();
  43. const o = await organizerService.findOneById(organizer);
  44. if (!o) {
  45. throw new HttpException('Organizer-ID not found!', 404);
  46. }
  47. const tmp = await this.service.create(client, givenName, familyName, email, passwort);
  48. if (!o._admins) {
  49. organizerService.update(client, o._id, {$set: {_admins: [ tmp._id ] }}, {});
  50. } else if (o._admins.length === 0) {
  51. organizerService.update(client, o._id, {$push: {_admins: tmp._id }}, {});
  52. } else if (!o._pending) {
  53. organizerService.update(client, o._id, {$set: {_pending: [ tmp._id ] }}, {});
  54. } else {
  55. organizerService.update(client, o._id, {$push: {_pending: tmp._id }}, {});
  56. }
  57. // TODO: Mail verschicken
  58. return tmp;
  59. }
  60. @Mutation(() => Person, { nullable: true })
  61. async PersonConfirmMail(
  62. @Context('client') client: Client,
  63. @Args('email') email: string,
  64. @Args('confirmCode') confirmCode: string,
  65. ): Promise<Person> {
  66. const tmp = await this.service.find({email, confirmCode});
  67. if (tmp.length !== 1) {
  68. throw new HttpException('confirmCode not correct', 403);
  69. }
  70. this.service.update(client, tmp[0]._id, { $unset: { confirmCode } })
  71. return tmp[0];
  72. }
  73. @Mutation(() => Boolean, { nullable: false })
  74. async ChangePassword(
  75. @Context('client') client: Client,
  76. @Args('oldPassword', { nullable: false }) oldPassword: string,
  77. @Args('newPassword', { nullable: false }) newPassword: string,
  78. ): Promise<boolean> {
  79. if (!client.getUser()) {
  80. throw new HttpException('you need to be logged in to change your password!', 403);
  81. }
  82. const tmp = await this.service.findOneById(client.getUser()._id);
  83. if (!(await checkPassword(oldPassword, (tmp as unknown as any).passwort))) {
  84. throw new HttpException('old password wrong!', 403);
  85. }
  86. this.service.update(client, tmp._id, {$set: { passwort: await secureHash(newPassword) }});
  87. return true;
  88. }
  89. }