Skip to content

Commit

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

import { GitlabUserInput, GitlabUserOutput } from '@ticketing/user/services/gitlab/types';
import { LinearUserInput, LinearUserOutput } from '@ticketing/user/services/linear/types';

Expand Down Expand Up @@ -173,7 +175,7 @@ export type OriginalTeamInput =
| ZendeskTeamInput
| FrontTeamInput
| GorgiasTeamInput
| JiraTeamInput;
| JiraTeamInput | LinearTeamInput;

/* attachment */
export type OriginalAttachmentInput = null;
Expand Down Expand Up @@ -236,7 +238,7 @@ export type OriginalTeamOutput =
| ZendeskTeamOutput
| FrontTeamOutput
| GorgiasTeamOutput
| JiraTeamOutput;
| JiraTeamOutput | LinearTeamOutput;

/* attachment */
export type OriginalAttachmentOutput =
Expand Down
67 changes: 67 additions & 0 deletions packages/api/src/ticketing/team/services/linear/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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 { ITeamService } from '@ticketing/team/types';
import { LinearTeamOutput } from './types';
import { SyncParam } from '@@core/utils/types/interface';

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

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

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

const teamQuery = {
"query": "query { teams { nodes { id, name, description } }}"
};

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

return {
data: resp.data.data.teams.nodes,
message: 'Linear teams retrieved',
statusCode: 200,
};
} catch (error) {
throw error;
}
}
}


59 changes: 59 additions & 0 deletions packages/api/src/ticketing/team/services/linear/mappers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { ITeamMapper } from '@ticketing/team/types';
import { LinearTeamInput, LinearTeamOutput } from './types';
import {
UnifiedTicketingTeamInput,
UnifiedTicketingTeamOutput,
} from '@ticketing/team/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 LinearTeamMapper implements ITeamMapper {
constructor(private mappersRegistry: MappersRegistry, private utils: Utils) {
this.mappersRegistry.registerService('ticketing', 'team', 'linear', this);
}
desunify(
source: UnifiedTicketingTeamInput,
customFieldMappings?: {
slug: string;
remote_id: string;
}[],
): LinearTeamInput {
return;
}

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

return sourcesArray.map((team) =>
this.mapSingleTeamToUnified(team, connectionId, customFieldMappings),
);
}

private mapSingleTeamToUnified(
team: LinearTeamOutput,
connectionId: string,
customFieldMappings?: {
slug: string;
remote_id: string;
}[],
): UnifiedTicketingTeamOutput {
const unifiedTeam: UnifiedTicketingTeamOutput = {
remote_id: team.id,
remote_data: team,
name: team.name,
description: team.description,
};

return unifiedTeam;
}
}
8 changes: 8 additions & 0 deletions packages/api/src/ticketing/team/services/linear/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export type LinearTeam = {
id: string
name: string
description: string
};

export type LinearTeamInput = Partial<LinearTeam>;
export type LinearTeamOutput = LinearTeamInput;
4 changes: 4 additions & 0 deletions packages/api/src/ticketing/team/team.module.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { LinearTeamMapper } 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';
import { WebhookService } from '@@core/@core-services/webhooks/panora-webhooks/webhook.service';
Expand Down Expand Up @@ -36,6 +38,8 @@ import { TeamController } from './team.controller';
FrontTeamMapper,
JiraTeamMapper,
GorgiasTeamMapper,
LinearService,
LinearTeamMapper,
],
exports: [SyncService, ServiceRegistry, WebhookService],
})
Expand Down

0 comments on commit 0ab3e03

Please sign in to comment.