@@ -5,58 +5,62 @@ import { InvitationInCompanyEntity } from '../invitation-in-company.entity.js';
55import { IInvitationInCompanyRepository } from './invitation-repository.interface.js' ;
66
77export const invitationInCompanyCustomRepositoryExtension : IInvitationInCompanyRepository = {
8- async createOrUpdateInvitationInCompany (
9- companyInfo : CompanyInfoEntity ,
10- groupId : string | null ,
11- inviterId : string ,
12- newUserEmail : string ,
13- invitedUserRole : UserRoleEnum ,
14- ) : Promise < InvitationInCompanyEntity > {
15- const qb = this . createQueryBuilder ( 'invitation_in_company' )
16- . leftJoinAndSelect ( 'invitation_in_company.company' , 'company' )
17- . where ( 'company.id = :companyId' , { companyId : companyInfo . id } )
18- . andWhere ( 'invitation_in_company.invitedUserEmail = :newUserEmail' , { newUserEmail : newUserEmail ?. toLowerCase ( ) } ) ;
19- const foundInvitation = await qb . getOne ( ) ;
20- if ( foundInvitation ) {
21- await this . remove ( foundInvitation ) ;
22- }
23- const newInvitation = new InvitationInCompanyEntity ( ) ;
24- newInvitation . verification_string = Encryptor . generateRandomString ( ) ;
25- newInvitation . company = companyInfo ;
26- newInvitation . groupId = groupId ? groupId : null ;
27- newInvitation . inviterId = inviterId ;
28- newInvitation . invitedUserEmail = newUserEmail ?. toLowerCase ( ) ;
29- newInvitation . role = invitedUserRole ;
30- return await this . save ( newInvitation ) ;
31- } ,
8+ async createOrUpdateInvitationInCompany (
9+ companyInfo : CompanyInfoEntity ,
10+ groupId : string | null ,
11+ inviterId : string ,
12+ newUserEmail : string ,
13+ invitedUserRole : UserRoleEnum ,
14+ ) : Promise < { entity : InvitationInCompanyEntity ; rawToken : string } > {
15+ const qb = this . createQueryBuilder ( 'invitation_in_company' )
16+ . leftJoinAndSelect ( 'invitation_in_company.company' , 'company' )
17+ . where ( 'company.id = :companyId' , { companyId : companyInfo . id } )
18+ . andWhere ( 'invitation_in_company.invitedUserEmail = :newUserEmail' , {
19+ newUserEmail : newUserEmail ?. toLowerCase ( ) ,
20+ } ) ;
21+ const foundInvitation = await qb . getOne ( ) ;
22+ if ( foundInvitation ) {
23+ await this . remove ( foundInvitation ) ;
24+ }
25+ const rawToken = Encryptor . generateRandomString ( ) ;
26+ const newInvitation = new InvitationInCompanyEntity ( ) ;
27+ newInvitation . verification_string = Encryptor . hashVerificationToken ( rawToken ) ;
28+ newInvitation . company = companyInfo ;
29+ newInvitation . groupId = groupId ? groupId : null ;
30+ newInvitation . inviterId = inviterId ;
31+ newInvitation . invitedUserEmail = newUserEmail ?. toLowerCase ( ) ;
32+ newInvitation . role = invitedUserRole ;
33+ const entity = await this . save ( newInvitation ) ;
34+ return { entity, rawToken } ;
35+ } ,
3236
33- async deleteOldInvitationsInCompany ( companyId : string ) : Promise < void > {
34- const qb = this . createQueryBuilder ( 'invitation_in_company' )
35- . leftJoinAndSelect ( 'invitation_in_company.company' , 'company' )
36- . where ( 'company.id = :companyId' , { companyId } )
37- . andWhere ( "invitation_in_company.createdAt < NOW() - INTERVAL '1 day'" ) ;
38- const foundInvitations = await qb . getMany ( ) ;
39- if ( foundInvitations . length ) {
40- await this . remove ( foundInvitations ) ;
41- }
42- } ,
37+ async deleteOldInvitationsInCompany ( companyId : string ) : Promise < void > {
38+ const qb = this . createQueryBuilder ( 'invitation_in_company' )
39+ . leftJoinAndSelect ( 'invitation_in_company.company' , 'company' )
40+ . where ( 'company.id = :companyId' , { companyId } )
41+ . andWhere ( "invitation_in_company.createdAt < NOW() - INTERVAL '1 day'" ) ;
42+ const foundInvitations = await qb . getMany ( ) ;
43+ if ( foundInvitations . length ) {
44+ await this . remove ( foundInvitations ) ;
45+ }
46+ } ,
4347
44- async findNonExpiredInvitationInCompanyWithUsersByVerificationString (
45- verificationString : string ,
46- ) : Promise < InvitationInCompanyEntity > {
47- const qb = this . createQueryBuilder ( 'invitation_in_company' )
48- . leftJoinAndSelect ( 'invitation_in_company.company' , 'company' )
49- . leftJoinAndSelect ( 'company.users' , 'users' )
50- . where ( "invitation_in_company.createdAt > NOW() - INTERVAL '1 day'" )
51- . where ( 'invitation_in_company.verification_string = :verificationString' , { verificationString } ) ;
52- return await qb . getOne ( ) ;
53- } ,
48+ async findNonExpiredInvitationInCompanyWithUsersByVerificationString (
49+ verificationString : string ,
50+ ) : Promise < InvitationInCompanyEntity > {
51+ const qb = this . createQueryBuilder ( 'invitation_in_company' )
52+ . leftJoinAndSelect ( 'invitation_in_company.company' , 'company' )
53+ . leftJoinAndSelect ( 'company.users' , 'users' )
54+ . where ( "invitation_in_company.createdAt > NOW() - INTERVAL '1 day'" )
55+ . where ( 'invitation_in_company.verification_string = :verificationString' , { verificationString } ) ;
56+ return await qb . getOne ( ) ;
57+ } ,
5458
55- async countNonExpiredInvitationsInCompany ( companyId : string ) : Promise < number > {
56- const qb = this . createQueryBuilder ( 'invitation_in_company' )
57- . leftJoin ( 'invitation_in_company.company' , 'company' )
58- . where ( 'company.id = :companyId' , { companyId } )
59- . andWhere ( "invitation_in_company.createdAt > NOW() - INTERVAL '1 day'" ) ;
60- return await qb . getCount ( ) ;
61- } ,
59+ async countNonExpiredInvitationsInCompany ( companyId : string ) : Promise < number > {
60+ const qb = this . createQueryBuilder ( 'invitation_in_company' )
61+ . leftJoin ( 'invitation_in_company.company' , 'company' )
62+ . where ( 'company.id = :companyId' , { companyId } )
63+ . andWhere ( "invitation_in_company.createdAt > NOW() - INTERVAL '1 day'" ) ;
64+ return await qb . getCount ( ) ;
65+ } ,
6266} ;
0 commit comments