Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add integration with Wrike #549

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,083 changes: 1,033 additions & 50 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
},
"dependencies": {
"@changesets/cli": "^2.26.2",
"@nestjs/common": "^10.3.10",
"axios": "^1.7.2",
"gitmoji-cli": "^9.0.0",
"optional": "^0.1.4",
"sharp": "^0.33.2",
Expand Down
66 changes: 66 additions & 0 deletions packages/api/src/ticketing/account/services/wrike/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { EncryptionService } from '@@core/encryption/encryption.service';
import { LoggerService } from '@@core/logger/logger.service';
import { PrismaService } from '@@core/prisma/prisma.service';
import { Injectable } from '@nestjs/common';
import { IAccountService } from '@ticketing/account/types';
import { ServiceRegistry } from '../registry.service';
import { TicketingObject } from '@ticketing/@lib/@types';
import axios from 'axios';
import { ApiResponse } from '@@core/utils/types';
import { ActionType, handle3rdPartyServiceError } from '@@core/utils/errors';
import { WrikeAccountOutput } from './types';


@Injectable()
export class WrikeService implements IAccountService {
constructor(
private prisma: PrismaService,
private logger: LoggerService,
private cryptoService: EncryptionService,
private registry: ServiceRegistry,
) {
this.logger.setContext(
TicketingObject.account.toUpperCase() + ':' + WrikeService.name,
);
this.registry.registerService('wrike', this);
}

async syncAccounts(
linkedUserId: string,
remote_account_id?: string,
): Promise<ApiResponse<WrikeAccountOutput[]>> {
try {
const connection = await this.prisma.connections.findFirst({
where: {
id_linked_user: linkedUserId,
provider_slug: 'wrike',
vertical: 'ticketing',
},
});

const resp = await axios.get(`${connection.account_url}/accounts`, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${this.cryptoService.decrypt(
connection.access_token,
)}`,
},
});
this.logger.log(`Synced wrike accounts !`);

return {
data: resp.data._results,
message: 'Wrike accounts retrieved',
statusCode: 200,
};
} catch (error) {
handle3rdPartyServiceError(
error,
this.logger,
'wrike',
TicketingObject.account,
ActionType.GET,
);
}
}
}
60 changes: 60 additions & 0 deletions packages/api/src/ticketing/account/services/wrike/mappers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { MappersRegistry } from '@@core/utils/registry/mappings.registry';
import { Utils } from '@ticketing/@lib/@utils';
import { Injectable } from '@nestjs/common';
import { IAccountMapper } from '@ticketing/account/types';
import { UnifiedAccountInput, UnifiedAccountOutput } from '@ticketing/account/types/model.unified';
import { WrikeAccountInput, WrikeAccountOutput } from './types';

@Injectable()
export class WrikeAccountMapper implements IAccountMapper {
constructor(private mappersRegistry: MappersRegistry, private utils: Utils) {
this.mappersRegistry.registerService('ticketing', 'account', 'wrike', this);
}
desunify(
source: UnifiedAccountInput,
customFieldMappings?: {
slug: string;
remote_id: string;
}[],
): WrikeAccountInput {
return;
}

unify(
source: WrikeAccountOutput | WrikeAccountOutput[],
customFieldMappings?: {
slug: string;
remote_id: string;
}[],
): UnifiedAccountOutput | UnifiedAccountOutput[] {
const sourcesArray = Array.isArray(source) ? source : [source];

return sourcesArray.map((account) =>
this.mapSingleAccountToUnified(account, customFieldMappings),
);
}

private mapSingleAccountToUnified(
account: WrikeAccountOutput,
customFieldMappings?: {
slug: string;
remote_id: string;
}[],
): UnifiedAccountOutput {
const field_mappings: { [key: string]: any } = {};
if (customFieldMappings) {
for (const mapping of customFieldMappings) {
field_mappings[mapping.slug] = account.custom_fields[mapping.remote_id];
}
}

const unifiedAccount: UnifiedAccountOutput = {
remote_id: account.id,
name: account.name,
domains: account.domains.flat(),
field_mappings: field_mappings,
};

return unifiedAccount;
}
}
25 changes: 25 additions & 0 deletions packages/api/src/ticketing/account/services/wrike/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export type WrikeAccountInput = {
id: string;
};

export type WrikeAccountOutput = {
_links: {
self: string;
related: {
contacts: string;
};
};
id: string;
name: string;
logo_url: string;
description: string;
domains: string[][];
external_id: number;
custom_fields: {
employees: number;
headquarters: string;
};
created_at: number;
updated_at: number;
};

56 changes: 56 additions & 0 deletions packages/api/src/ticketing/attachment/services/wrike/mappers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { MappersRegistry } from "@@core/utils/registry/mappings.registry";
import { Injectable } from "@nestjs/common";
import { Utils } from "@ticketing/@lib/@utils";
import { IAttachmentMapper } from "@ticketing/attachment/types";
import { UnifiedAttachmentInput, UnifiedAttachmentOutput } from "@ticketing/attachment/types/model.unified";
import { WrikeAttachmentOutput } from "./types";

@Injectable()
export class WrikeAttachmentMapper implements IAttachmentMapper {
constructor(private mappersRegistry: MappersRegistry, private utils: Utils) {
this.mappersRegistry.registerService(
'ticketing',
'attachment',
'wrike',
this,
);
}
async desunify(
source: UnifiedAttachmentInput,
customFieldMappings?: {
slug: string;
remote_id: string;
}[],
): Promise<any> {
return;
}

unify(
source: WrikeAttachmentOutput | WrikeAttachmentOutput[],
customFieldMappings?: {
slug: string;
remote_id: string;
}[],
): UnifiedAttachmentOutput | UnifiedAttachmentOutput[] {
if (!Array.isArray(source)) {
return this.mapSingleAttachmentToUnified(source, customFieldMappings);
}
return source.map((attachment) =>
this.mapSingleAttachmentToUnified(attachment, customFieldMappings),
);
}

private mapSingleAttachmentToUnified(
attachment: WrikeAttachmentOutput,
customFieldMappings?: {
slug: string;
remote_id: string;
}[],
): UnifiedAttachmentOutput {
return {
remote_id: attachment.id,
file_name: attachment.filename,
file_url: attachment.url,
};
}
}
13 changes: 13 additions & 0 deletions packages/api/src/ticketing/attachment/services/wrike/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export type WrikeAttachmentOutput = {
id: string;
filename: string;
url: string;
content_type: string;
size: number;
metadata: AttachmentMetadata;
};

type AttachmentMetadata = {
is_inline: boolean;
cid: string;
};
64 changes: 64 additions & 0 deletions packages/api/src/ticketing/collection/services/wrike/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { EncryptionService } from "@@core/encryption/encryption.service";
import { LoggerService } from "@@core/logger/logger.service";
import { PrismaService } from "@@core/prisma/prisma.service";
import { Injectable } from "@nestjs/common";
import { ICollectionService } from "@ticketing/collection/types";
import { ServiceRegistry } from "../registry.service";
import { TicketingObject } from "@ticketing/@lib/@types";
import { ApiResponse } from "@@core/utils/types";
import axios from "axios";
import { ActionType, handle3rdPartyServiceError } from "@@core/utils/errors";
import { WrikeCollectionOutput } from "./types";

@Injectable()
export class WrikeService implements ICollectionService {
constructor(
private prisma: PrismaService,
private logger: LoggerService,
private cryptoService: EncryptionService,
private registry: ServiceRegistry,
) {
this.logger.setContext(
TicketingObject.collection.toUpperCase() + ':' + WrikeService.name,
);
this.registry.registerService('wrike', this);
}

async syncCollections(
linkedUserId: string,
): Promise<ApiResponse<WrikeCollectionOutput[]>> {
try {
const connection = await this.prisma.connections.findFirst({
where: {
id_linked_user: linkedUserId,
provider_slug: 'wrike',
vertical: 'ticketing',
},
});

const resp = await axios.get(`${connection.account_url}/project/search`, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${this.cryptoService.decrypt(
connection.access_token,
)}`,
},
});
this.logger.log(`Synced wrike collections !`);

return {
data: resp.data,
message: 'Wrike collections retrieved',
statusCode: 200,
};
} catch (error) {
handle3rdPartyServiceError(
error,
this.logger,
'wrike',
TicketingObject.collection,
ActionType.GET,
);
}
}
}
58 changes: 58 additions & 0 deletions packages/api/src/ticketing/collection/services/wrike/mappers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { MappersRegistry } from "@@core/utils/registry/mappings.registry";
import { Injectable } from "@nestjs/common";
import { Utils } from "@ticketing/@lib/@utils";
import { ICollectionMapper } from "@ticketing/collection/types";
import { UnifiedCollectionInput, UnifiedCollectionOutput } from "@ticketing/collection/types/model.unified";
import { WrikeCollectionInput, WrikeCollectionOutput } from "./types";

@Injectable()
export class WrikeCollectionMapper implements ICollectionMapper {
constructor(private mappersRegistry: MappersRegistry, private utils: Utils) {
this.mappersRegistry.registerService(
'ticketing',
'collection',
'wrike',
this,
);
}
desunify(
source: UnifiedCollectionInput,
customFieldMappings?: {
slug: string;
remote_id: string;
}[],
): WrikeCollectionInput {
return;
}

unify(
source: WrikeCollectionOutput | WrikeCollectionOutput[],
customFieldMappings?: {
slug: string;
remote_id: string;
}[],
): UnifiedCollectionOutput | UnifiedCollectionOutput[] {
const sourcesArray = Array.isArray(source) ? source : [source];

return sourcesArray.map((collection) =>
this.mapSingleCollectionToUnified(collection, customFieldMappings),
);
}

private mapSingleCollectionToUnified(
collection: WrikeCollectionOutput,
customFieldMappings?: {
slug: string;
remote_id: string;
}[],
): UnifiedCollectionOutput {
const unifiedCollection: UnifiedCollectionOutput = {
remote_id: collection.id,
name: collection.name,
description: collection.name,
collection_type: 'PROJECT',
};

return unifiedCollection;
}
}
33 changes: 33 additions & 0 deletions packages/api/src/ticketing/collection/services/wrike/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export type WrikeCollectionOutput = {
avatarUrls: AvatarUrls;
id: string;
insight: Insight;
key: string;
name: string;
projectCategory: ProjectCategory;
self: string;
simplified: boolean;
style: string;
};

type AvatarUrls = {
'16x16': string;
'24x24': string;
'32x32': string;
'48x48': string;
};

type Insight = {
lastIssueUpdateTime: string;
totalIssueCount: number;
};

type ProjectCategory = {
description: string;
id: string;
name: string;
self: string;
};

export type WrikeCollectionInput = null;

Loading