@@ -39,37 +39,15 @@ export class TypeORMAppInstallationDBImpl implements AppInstallationDB {
39
39
await repo . insert ( obj ) ;
40
40
}
41
41
42
- protected async findAndFinishInstallation ( platform : AppInstallationPlatform , installationID : string ) : Promise < AppInstallation | undefined > {
43
- // check if we find the complementary installation entries. If so, finish the installation
42
+ public async findInstallation ( platform : AppInstallationPlatform , installationID : string ) : Promise < AppInstallation | undefined > {
44
43
const repo = await this . getRepo ( ) ;
45
- const installationRecords = await repo . find ( { where : { platform, installationID } } ) ;
46
-
47
- // maybe we're already done and have a finished installation
48
- const finishedInstallation = installationRecords . find ( r => r . state == 'installed' ) ;
49
- if ( ! ! finishedInstallation ) {
50
- return finishedInstallation ;
51
- }
52
-
53
- // maybe we need to finish an existing/ongoing installation
54
- const platformClaim = installationRecords . find ( r => r . state == 'claimed.platform' ) ;
55
- const userClaim = installationRecords . find ( r => r . state == 'claimed.user' ) ;
56
- if ( ! ! platformClaim && ! ! userClaim ) {
57
- const obj = new DBAppInstallation ( ) ;
58
- obj . platform = platform ;
59
- obj . installationID = installationID ;
60
- obj . state = 'installed' ;
61
- obj . ownerUserID = userClaim . ownerUserID || platformClaim . ownerUserID || undefined ;
62
- obj . platformUserID = platformClaim . platformUserID ;
63
- obj . creationTime = new Date ( ) . toISOString ( ) ;
64
- return await repo . save ( obj ) ;
65
- }
66
-
67
- // we do not have a finished installation here
68
- return undefined ;
69
- }
44
+ const qb = repo . createQueryBuilder ( 'installation' )
45
+ . where ( "installation.installationID = :installationID" , { installationID } )
46
+ . andWhere ( 'installation.state != "uninstalled"' )
47
+ . orderBy ( "installation.lastUpdateTime" , "DESC" )
48
+ . limit ( 1 ) ;
70
49
71
- public async findInstallation ( platform : AppInstallationPlatform , installationID : string ) : Promise < AppInstallation | undefined > {
72
- return this . findAndFinishInstallation ( platform , installationID ) ;
50
+ return ( await qb . getMany ( ) ) [ 0 ] ;
73
51
}
74
52
75
53
public async recordUninstallation ( platform : AppInstallationPlatform , source : 'user' | 'platform' , installationID : string ) {
0 commit comments