Skip to content

Commit

Permalink
adding linear tag
Browse files Browse the repository at this point in the history
  • Loading branch information
ganimtron-10 committed Aug 8, 2024
1 parent 0ab3e03 commit fe83ba5
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { LinearTagInput, LinearTagOutput } from '@ticketing/tag/services/linear/types';

import { LinearTeamInput, LinearTeamOutput } from '@ticketing/team/services/linear/types';

import { GitlabUserInput, GitlabUserOutput } from '@ticketing/user/services/gitlab/types';
Expand Down Expand Up @@ -168,7 +170,7 @@ export type OriginalTagInput =
| FrontTagInput
| GorgiasTagInput
| JiraTagInput
| GitlabTagInput;
| GitlabTagInput | LinearTagInput;

/* team */
export type OriginalTeamInput =
Expand Down Expand Up @@ -231,7 +233,7 @@ export type OriginalTagOutput =
| FrontTagOutput
| GorgiasTagOutput
| JiraTagOutput
| GitlabTagOutput;
| GitlabTagOutput | LinearTagOutput;

/* team */
export type OriginalTeamOutput =
Expand Down
65 changes: 65 additions & 0 deletions packages/api/src/ticketing/tag/services/linear/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { Injectable } from '@nestjs/common';
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 { TicketingObject } from '@ticketing/@lib/@types';
import { ApiResponse } from '@@core/utils/types';
import axios from 'axios';
import { ActionType, handle3rdPartyServiceError } from '@@core/utils/errors';
import { ServiceRegistry } from '../registry.service';
import { ITagService } from '@ticketing/tag/types';
import { LinearTagOutput } from './types';
import { SyncParam } from '@@core/utils/types/interface';

@Injectable()
export class LinearService implements ITagService {
constructor(
private prisma: PrismaService,
private logger: LoggerService,
private cryptoService: EncryptionService,
private registry: ServiceRegistry,
) {
this.logger.setContext(
TicketingObject.tag.toUpperCase() + ':' + LinearService.name,
);
this.registry.registerService('linear', this);
}

async sync(data: SyncParam): Promise<ApiResponse<LinearTagOutput[]>> {
try {
const { linkedUserId, id_ticket } = data;

const connection = await this.prisma.connections.findFirst({
where: {
id_linked_user: linkedUserId,
provider_slug: 'linear',
vertical: 'ticketing',
},
});

const labelQuery = {
"query": "query { issueLabels { nodes { id name } }}"
};

let resp = await axios.post(
`${connection.account_url}`,
labelQuery, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${this.cryptoService.decrypt(
connection.access_token,
)}`,
},
});
this.logger.log(`Synced linear tags !`);

return {
data: resp.data.data.issueLabels.nodes,
message: 'Linear tags retrieved',
statusCode: 200,
};
} catch (error) {
throw error;
}
}
}
58 changes: 58 additions & 0 deletions packages/api/src/ticketing/tag/services/linear/mappers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { ITagMapper } from '@ticketing/tag/types';
import { LinearTagInput, LinearTagOutput } from './types';
import {
UnifiedTicketingTagInput,
UnifiedTicketingTagOutput,
} from '@ticketing/tag/types/model.unified';
import { MappersRegistry } from '@@core/@core-services/registries/mappers.registry';
import { Injectable } from '@nestjs/common';
import { Utils } from '@ticketing/@lib/@utils';

@Injectable()
export class LinearTagMapper implements ITagMapper {
constructor(private mappersRegistry: MappersRegistry, private utils: Utils) {
this.mappersRegistry.registerService('ticketing', 'tag', 'linear', this);
}
desunify(
source: UnifiedTicketingTagInput,
customFieldMappings?: {
slug: string;
remote_id: string;
}[],
): LinearTagInput {
return;
}

unify(
source: LinearTagOutput | LinearTagOutput[],
connectionId: string,
customFieldMappings?: {
slug: string;
remote_id: string;
}[],
): UnifiedTicketingTagOutput | UnifiedTicketingTagOutput[] {
// If the source is not an array, convert it to an array for mapping
const sourcesArray = Array.isArray(source) ? source : [source];

return sourcesArray.map((tag) =>
this.mapSingleTagToUnified(tag, connectionId, customFieldMappings),
);
}

private mapSingleTagToUnified(
tag: LinearTagOutput,
connectionId: string,
customFieldMappings?: {
slug: string;
remote_id: string;
}[],
): UnifiedTicketingTagOutput {
const unifiedTag: UnifiedTicketingTagOutput = {
remote_id: tag.id,
remote_data: tag,
name: tag.name,
};

return unifiedTag;
}
}
7 changes: 7 additions & 0 deletions packages/api/src/ticketing/tag/services/linear/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
interface LinearTag {
id: string
name: string
}

export type LinearTagInput = Partial<LinearTag>;
export type LinearTagOutput = LinearTagInput;
4 changes: 4 additions & 0 deletions packages/api/src/ticketing/tag/tag.module.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { LinearTagMapper } from './services/linear/mappers';
import { LinearService } from './services/linear';
import { BullQueueModule } from '@@core/@core-services/queues/queue.module';

import { IngestDataService } from '@@core/@core-services/unification/ingest-data.service';
Expand Down Expand Up @@ -41,6 +43,8 @@ import { GitlabTagMapper } from './services/gitlab/mappers';
JiraTagMapper,
GorgiasTagMapper,
GitlabTagMapper,
LinearService,
LinearTagMapper,
],
exports: [SyncService, ServiceRegistry, WebhookService],
})
Expand Down

0 comments on commit fe83ba5

Please sign in to comment.