Skip to content

Commit

Permalink
✅ Ticket & Comment unified write
Browse files Browse the repository at this point in the history
  • Loading branch information
naelob committed Jul 17, 2024
1 parent cfd0336 commit 715911a
Show file tree
Hide file tree
Showing 20 changed files with 178 additions and 517 deletions.
1 change: 1 addition & 0 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"passport-jwt": "^4.0.1",
"passport-local": "^1.0.0",
"pino-pretty": "^10.2.3",
"qs": "^6.12.3",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1",
"stytch": "^10.5.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ export class OAuthTokenRefreshService implements OnModuleInit {

@Cron(CronExpression.EVERY_HOUR)
async handleCron() {
// refresh all tokens that expire in less than 10 hours
const tenHoursFromNow = new Date();
tenHoursFromNow.setHours(tenHoursFromNow.getHours() + 10);
const now = new Date();

const tenHoursFromNow = new Date(now.getTime() + 10 * 60 * 60 * 1000);

const connectionsToRefresh = await this.prisma.connections.findMany({
where: {
expiration_timestamp: {
lte: tenHoursFromNow,
lte: tenHoursFromNow.toISOString(),
},
},
});
Expand All @@ -34,7 +35,8 @@ export class OAuthTokenRefreshService implements OnModuleInit {
try {
if (connection.refresh_token) {
const account_url =
connection.provider_slug == 'zoho' ? connection.account_url : '';
connection.provider_slug === 'zoho' ? connection.account_url : '';

await this.categoryConnectionRegistry
.getService(connection.vertical.toLowerCase())
.handleTokensRefresh(
Expand All @@ -46,7 +48,10 @@ export class OAuthTokenRefreshService implements OnModuleInit {
);
}
} catch (error) {
this.logger.error('failed to refresh token ', error);
this.logger.error(
`Failed to refresh token for connection: ${connection.id_connection}`,
error,
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
OAuth2AuthData,
providerToType,
} from '@panora/shared';
import qs from 'qs';
import axios from 'axios';
import { v4 as uuidv4 } from 'uuid';
import { ITicketingConnectionService } from '../../types';
Expand Down Expand Up @@ -145,26 +146,30 @@ export class FrontConnectionService implements ITicketingConnectionService {
async handleTokenRefresh(opts: RefreshParams) {
try {
const { connectionId, refreshToken, projectId } = opts;
const formData = new URLSearchParams({
grant_type: 'refresh_token',
refresh_token: this.cryptoService.decrypt(refreshToken),
});
const CREDENTIALS = (await this.cService.getCredentials(
projectId,
this.type,
)) as OAuth2AuthData;
const res = await axios.post(
`https://app.frontapp.com/oauth/token`,
formData.toString(),
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
Authorization: `Basic ${Buffer.from(
`${CREDENTIALS.CLIENT_ID}:${CREDENTIALS.CLIENT_SECRET}`,
).toString('base64')}`,
},

const DATA = new URLSearchParams({
grant_type: 'refresh_token',
refresh_token: this.cryptoService.decrypt(refreshToken),
}).toString();

const config = {
method: 'post',
url: 'https://app.frontapp.com/oauth/token',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Authorization: `Basic ${Buffer.from(
`${CREDENTIALS.CLIENT_ID}:${CREDENTIALS.CLIENT_SECRET}`,
).toString('base64')}`,
},
);
data: DATA,
};

const res = await axios(config);

const data: FrontOAuthResponse = res.data;
await this.prisma.connections.update({
where: {
Expand Down
15 changes: 7 additions & 8 deletions packages/api/src/ticketing/comment/services/front/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { Injectable } from '@nestjs/common';
import { EncryptionService } from '@@core/@core-services/encryption/encryption.service';
import { LoggerService } from '@@core/@core-services/logger/logger.service';
import { PrismaService } from '@@core/@core-services/prisma/prisma.service';
import { EncryptionService } from '@@core/@core-services/encryption/encryption.service';
import { ApiResponse } from '@@core/utils/types';
import axios from 'axios';
import { ActionType, handle3rdPartyServiceError } from '@@core/utils/errors';
import { ICommentService } from '@ticketing/comment/types';
import { SyncParam } from '@@core/utils/types/interface';
import { Injectable } from '@nestjs/common';
import { TicketingObject } from '@ticketing/@lib/@types';
import { FrontCommentInput, FrontCommentOutput } from './types';
import { ServiceRegistry } from '../registry.service';
import { Utils } from '@ticketing/@lib/@utils';
import { SyncParam } from '@@core/utils/types/interface';
import { ICommentService } from '@ticketing/comment/types';
import axios from 'axios';
import * as FormData from 'form-data';
import { ServiceRegistry } from '../registry.service';
import { FrontCommentInput, FrontCommentOutput } from './types';

@Injectable()
export class FrontService implements ICommentService {
Expand Down
22 changes: 11 additions & 11 deletions packages/api/src/ticketing/comment/services/gitlab/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import { Injectable } from '@nestjs/common';
import { EncryptionService } from '@@core/@core-services/encryption/encryption.service';
import { LoggerService } from '@@core/@core-services/logger/logger.service';
import { PrismaService } from '@@core/@core-services/prisma/prisma.service';
import { EncryptionService } from '@@core/@core-services/encryption/encryption.service';
import { ApiResponse } from '@@core/utils/types';
import axios from 'axios';
import { ActionType, handle3rdPartyServiceError } from '@@core/utils/errors';
import { ICommentService } from '@ticketing/comment/types';
import { SyncParam } from '@@core/utils/types/interface';
import { Injectable } from '@nestjs/common';
import { TicketingObject } from '@ticketing/@lib/@types';
import { GitlabCommentInput, GitlabCommentOutput } from './types';
import { ServiceRegistry } from '../registry.service';
import { Utils } from '@ticketing/@lib/@utils';
import { ICommentService } from '@ticketing/comment/types';
import axios from 'axios';
import * as fs from 'fs';
import { SyncParam } from '@@core/utils/types/interface';
import { OriginalCommentOutput } from '@@core/utils/types/original/original.ticketing';
import { ServiceRegistry } from '../registry.service';
import { GitlabCommentInput, GitlabCommentOutput } from './types';

@Injectable()
export class GitlabService implements ICommentService {
Expand Down Expand Up @@ -72,10 +70,12 @@ export class GitlabService implements ICommentService {

// Assuming you want to modify the comment object here
// For now, we'll just add the uploads to the comment
const data = {
const data: any = {
...commentData,
attachments: uploads,
};
if (uploads.length > 0) {
data.attachments = uploads;
}

const ticket = await this.prisma.tcg_tickets.findFirst({
where: {
Expand Down
16 changes: 7 additions & 9 deletions packages/api/src/ticketing/comment/services/gorgias/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { Injectable } from '@nestjs/common';
import { EncryptionService } from '@@core/@core-services/encryption/encryption.service';
import { LoggerService } from '@@core/@core-services/logger/logger.service';
import { PrismaService } from '@@core/@core-services/prisma/prisma.service';
import { EncryptionService } from '@@core/@core-services/encryption/encryption.service';
import { ApiResponse } from '@@core/utils/types';
import axios from 'axios';
import { ActionType, handle3rdPartyServiceError } from '@@core/utils/errors';
import { ICommentService } from '@ticketing/comment/types';
import { SyncParam } from '@@core/utils/types/interface';
import { Injectable } from '@nestjs/common';
import { TicketingObject } from '@ticketing/@lib/@types';
import { GorgiasCommentInput, GorgiasCommentOutput } from './types';
import { ServiceRegistry } from '../registry.service';
import { Utils } from '@ticketing/@lib/@utils';
import * as fs from 'fs';
import { SyncParam } from '@@core/utils/types/interface';
import { ICommentService } from '@ticketing/comment/types';
import axios from 'axios';
import { ServiceRegistry } from '../registry.service';
import { GorgiasCommentInput, GorgiasCommentOutput } from './types';

@Injectable()
export class GorgiasService implements ICommentService {
Expand Down
15 changes: 7 additions & 8 deletions packages/api/src/ticketing/comment/services/jira/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { Injectable } from '@nestjs/common';
import { EncryptionService } from '@@core/@core-services/encryption/encryption.service';
import { LoggerService } from '@@core/@core-services/logger/logger.service';
import { PrismaService } from '@@core/@core-services/prisma/prisma.service';
import { EncryptionService } from '@@core/@core-services/encryption/encryption.service';
import { ApiResponse } from '@@core/utils/types';
import axios from 'axios';
import { ActionType, handle3rdPartyServiceError } from '@@core/utils/errors';
import { ICommentService } from '@ticketing/comment/types';
import { SyncParam } from '@@core/utils/types/interface';
import { Injectable } from '@nestjs/common';
import { TicketingObject } from '@ticketing/@lib/@types';
import { JiraCommentInput, JiraCommentOutput } from './types';
import { ServiceRegistry } from '../registry.service';
import { Utils } from '@ticketing/@lib/@utils';
import { SyncParam } from '@@core/utils/types/interface';
import { ICommentService } from '@ticketing/comment/types';
import axios from 'axios';
import * as FormData from 'form-data';
import * as fs from 'fs';
import { ServiceRegistry } from '../registry.service';
import { JiraCommentInput, JiraCommentOutput } from './types';

@Injectable()
export class JiraService implements ICommentService {
Expand Down
17 changes: 16 additions & 1 deletion packages/api/src/ticketing/comment/services/jira/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
UnifiedCommentOutput,
} from '@ticketing/comment/types/model.unified';
import { JiraCommentInput, JiraCommentOutput } from './types';
import { OriginalCommentOutput } from '@@core/utils/types/original/original.ticketing';
@Injectable()
export class JiraCommentMapper implements ICommentMapper {
constructor(private mappersRegistry: MappersRegistry, private utils: Utils) {
Expand All @@ -21,7 +22,21 @@ export class JiraCommentMapper implements ICommentMapper {
}[],
): Promise<JiraCommentInput> {
const result: JiraCommentInput = {
body: source.body,
body: {
content: [
{
content: [
{
text: source.body,
type: 'text',
},
],
type: 'paragraph',
},
],
type: 'doc',
version: 1,
},
};
if (source.attachments) {
result.attachments = source.attachments as string[];
Expand Down
17 changes: 8 additions & 9 deletions packages/api/src/ticketing/comment/services/zendesk/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { Injectable } from '@nestjs/common';
import { EncryptionService } from '@@core/@core-services/encryption/encryption.service';
import { EnvironmentService } from '@@core/@core-services/environment/environment.service';
import { LoggerService } from '@@core/@core-services/logger/logger.service';
import { PrismaService } from '@@core/@core-services/prisma/prisma.service';
import { EncryptionService } from '@@core/@core-services/encryption/encryption.service';
import { ApiResponse } from '@@core/utils/types';
import axios from 'axios';
import { ActionType, handle3rdPartyServiceError } from '@@core/utils/errors';
import { ICommentService } from '@ticketing/comment/types';
import { TicketingObject } from '@ticketing/@lib/@types';
import { SyncParam } from '@@core/utils/types/interface';
import { OriginalCommentOutput } from '@@core/utils/types/original/original.ticketing';
import { Injectable } from '@nestjs/common';
import { TicketingObject } from '@ticketing/@lib/@types';
import { Utils } from '@ticketing/@lib/@utils';
import { ICommentService } from '@ticketing/comment/types';
import axios from 'axios';
import { ServiceRegistry } from '../registry.service';
import { ZendeskCommentInput, ZendeskCommentOutput } from './types';
import { EnvironmentService } from '@@core/@core-services/environment/environment.service';
import { SyncParam } from '@@core/utils/types/interface';
import { Utils } from '@ticketing/@lib/@utils';
@Injectable()
export class ZendeskService implements ICommentService {
constructor(
Expand Down
5 changes: 1 addition & 4 deletions packages/api/src/ticketing/comment/types/model.unified.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import {
UnifiedAttachmentInput,
UnifiedAttachmentOutput,
} from '@ticketing/attachment/types/model.unified';
import { UnifiedAttachmentOutput } from '@ticketing/attachment/types/model.unified';
import { IsBoolean, IsIn, IsOptional, IsString, IsUUID } from 'class-validator';

export type CommentCreatorType = 'USER' | 'CONTACT';
Expand Down
96 changes: 0 additions & 96 deletions packages/api/src/ticketing/ticket/services/github/index.ts

This file was deleted.

Loading

0 comments on commit 715911a

Please sign in to comment.