1
1
import { Injectable } from '@nestjs/common' ;
2
2
3
- import { EntityManager } from 'typeorm' ;
3
+ import { DataSource , EntityManager } from 'typeorm' ;
4
4
import { v4 } from 'uuid' ;
5
5
6
- import { TypeORMService } from 'src/database/typeorm/typeorm.service' ;
7
6
import { EnvironmentService } from 'src/engine/integrations/environment/environment.service' ;
8
7
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants' ;
9
8
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service' ;
10
- import { DataSourceService } from 'src/engine/metadata-modules/data-source/data-source.service' ;
11
9
import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator' ;
12
10
import {
13
11
GoogleCalendarSyncJobData ,
14
12
GoogleCalendarSyncJob ,
15
13
} from 'src/modules/calendar/jobs/google-calendar-sync.job' ;
16
- import { CalendarChannelRepository } from 'src/modules/calendar/repositories/calendar-channel.repository' ;
17
14
import {
18
15
CalendarChannelWorkspaceEntity ,
19
16
CalendarChannelVisibility ,
@@ -35,12 +32,15 @@ import {
35
32
MessagingMessageListFetchJobData ,
36
33
} from 'src/modules/messaging/message-import-manager/jobs/messaging-message-list-fetch.job' ;
37
34
import { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator' ;
35
+ import { InjectWorkspaceDatasource } from 'src/engine/twenty-orm/decorators/inject-workspace-datasource.decorator' ;
36
+ import { InjectWorkspaceRepository } from 'src/engine/twenty-orm/decorators/inject-workspace-repository.decorator' ;
37
+ import { WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace.repository' ;
38
38
39
39
@Injectable ( )
40
40
export class GoogleAPIsService {
41
41
constructor (
42
- private readonly dataSourceService : DataSourceService ,
43
- private readonly typeORMService : TypeORMService ,
42
+ @ InjectWorkspaceDatasource ( )
43
+ private readonly workspaceDataSource : DataSource ,
44
44
@InjectMessageQueue ( MessageQueue . messagingQueue )
45
45
private readonly messageQueueService : MessageQueueService ,
46
46
@InjectMessageQueue ( MessageQueue . calendarQueue )
@@ -50,8 +50,8 @@ export class GoogleAPIsService {
50
50
private readonly connectedAccountRepository : ConnectedAccountRepository ,
51
51
@InjectObjectMetadataRepository ( MessageChannelWorkspaceEntity )
52
52
private readonly messageChannelRepository : MessageChannelRepository ,
53
- @InjectObjectMetadataRepository ( CalendarChannelWorkspaceEntity )
54
- private readonly calendarChannelRepository : CalendarChannelRepository ,
53
+ @InjectWorkspaceRepository ( CalendarChannelWorkspaceEntity )
54
+ private readonly calendarChannelRepository : WorkspaceRepository < CalendarChannelWorkspaceEntity > ,
55
55
) { }
56
56
57
57
async refreshGoogleRefreshToken ( input : {
@@ -71,14 +71,6 @@ export class GoogleAPIsService {
71
71
messageVisibility,
72
72
} = input ;
73
73
74
- const dataSourceMetadata =
75
- await this . dataSourceService . getLastDataSourceMetadataFromWorkspaceIdOrFail (
76
- workspaceId ,
77
- ) ;
78
-
79
- const workspaceDataSource =
80
- await this . typeORMService . connectToDataSource ( dataSourceMetadata ) ;
81
-
82
74
const isCalendarEnabled = this . environmentService . get (
83
75
'CALENDAR_PROVIDER_GOOGLE_ENABLED' ,
84
76
) ;
@@ -93,65 +85,67 @@ export class GoogleAPIsService {
93
85
const existingAccountId = connectedAccounts ?. [ 0 ] ?. id ;
94
86
const newOrExistingConnectedAccountId = existingAccountId ?? v4 ( ) ;
95
87
96
- await workspaceDataSource ?. transaction ( async ( manager : EntityManager ) => {
97
- if ( ! existingAccountId ) {
98
- await this . connectedAccountRepository . create (
99
- {
100
- id : newOrExistingConnectedAccountId ,
101
- handle,
102
- provider : ConnectedAccountProvider . GOOGLE ,
103
- accessToken : input . accessToken ,
104
- refreshToken : input . refreshToken ,
105
- accountOwnerId : workspaceMemberId ,
106
- } ,
107
- workspaceId ,
108
- manager ,
109
- ) ;
110
-
111
- await this . messageChannelRepository . create (
112
- {
113
- id : v4 ( ) ,
114
- connectedAccountId : newOrExistingConnectedAccountId ,
115
- type : MessageChannelType . EMAIL ,
116
- handle,
117
- visibility :
118
- messageVisibility || MessageChannelVisibility . SHARE_EVERYTHING ,
119
- syncStatus : MessageChannelSyncStatus . ONGOING ,
120
- } ,
121
- workspaceId ,
122
- manager ,
123
- ) ;
88
+ await this . workspaceDataSource ?. transaction (
89
+ async ( manager : EntityManager ) => {
90
+ if ( ! existingAccountId ) {
91
+ await this . connectedAccountRepository . create (
92
+ {
93
+ id : newOrExistingConnectedAccountId ,
94
+ handle,
95
+ provider : ConnectedAccountProvider . GOOGLE ,
96
+ accessToken : input . accessToken ,
97
+ refreshToken : input . refreshToken ,
98
+ accountOwnerId : workspaceMemberId ,
99
+ } ,
100
+ workspaceId ,
101
+ manager ,
102
+ ) ;
124
103
125
- if ( isCalendarEnabled ) {
126
- await this . calendarChannelRepository . create (
104
+ await this . messageChannelRepository . create (
127
105
{
128
106
id : v4 ( ) ,
129
107
connectedAccountId : newOrExistingConnectedAccountId ,
108
+ type : MessageChannelType . EMAIL ,
130
109
handle,
131
110
visibility :
132
- calendarVisibility ||
133
- CalendarChannelVisibility . SHARE_EVERYTHING ,
111
+ messageVisibility || MessageChannelVisibility . SHARE_EVERYTHING ,
112
+ syncStatus : MessageChannelSyncStatus . ONGOING ,
134
113
} ,
135
114
workspaceId ,
136
115
manager ,
137
116
) ;
138
- }
139
- } else {
140
- await this . connectedAccountRepository . updateAccessTokenAndRefreshToken (
141
- input . accessToken ,
142
- input . refreshToken ,
143
- newOrExistingConnectedAccountId ,
144
- workspaceId ,
145
- manager ,
146
- ) ;
147
117
148
- await this . messageChannelRepository . resetSync (
149
- newOrExistingConnectedAccountId ,
150
- workspaceId ,
151
- manager ,
152
- ) ;
153
- }
154
- } ) ;
118
+ if ( isCalendarEnabled ) {
119
+ await this . calendarChannelRepository . save (
120
+ {
121
+ id : v4 ( ) ,
122
+ connectedAccountId : newOrExistingConnectedAccountId ,
123
+ handle,
124
+ visibility :
125
+ calendarVisibility ||
126
+ CalendarChannelVisibility . SHARE_EVERYTHING ,
127
+ } ,
128
+ { } ,
129
+ manager ,
130
+ ) ;
131
+ }
132
+ } else {
133
+ await this . connectedAccountRepository . updateAccessTokenAndRefreshToken (
134
+ input . accessToken ,
135
+ input . refreshToken ,
136
+ newOrExistingConnectedAccountId ,
137
+ workspaceId ,
138
+ manager ,
139
+ ) ;
140
+
141
+ await this . messageChannelRepository . resetSync (
142
+ newOrExistingConnectedAccountId ,
143
+ workspaceId ,
144
+ manager ,
145
+ ) ;
146
+ }
147
+ } ,
148
+ ) ;
155
149
156
150
if ( this . environmentService . get ( 'MESSAGING_PROVIDER_GMAIL_ENABLED' ) ) {
157
151
const messageChannels =
0 commit comments