(
channel: { id: string; accountId: string },
userId: string
)
| 157 | } |
| 158 | |
| 159 | async function handleUser( |
| 160 | channel: { id: string; accountId: string }, |
| 161 | userId: string |
| 162 | ) { |
| 163 | let user = await linenSdk |
| 164 | .findUser({ |
| 165 | accountsId: channel.accountId, |
| 166 | externalUserId: userId, |
| 167 | }) |
| 168 | .catch(console.error); |
| 169 | if (!user) { |
| 170 | // create user |
| 171 | const integration = await linenSdk.getChannelIntegration({ |
| 172 | channelId: channel.id, |
| 173 | type: channelsIntegrationType.LINEAR, |
| 174 | }); |
| 175 | if (!integration) { |
| 176 | throw new Error('integration not found'); |
| 177 | } |
| 178 | const newUser = await fetchUser({ |
| 179 | accessToken: integration.data.access_token, |
| 180 | userId: userId, |
| 181 | }); |
| 182 | if (!newUser) { |
| 183 | throw new Error('user not found'); |
| 184 | } |
| 185 | user = await linenSdk.findOrCreateUser({ |
| 186 | accountsId: channel.accountId, |
| 187 | externalUserId: userId, |
| 188 | displayName: newUser.displayName, |
| 189 | profileImageUrl: newUser.avatarUrl, |
| 190 | }); |
| 191 | if (!user) { |
| 192 | throw new Error('user not found'); |
| 193 | } |
| 194 | } |
| 195 | return user; |
| 196 | } |
no test coverage detected