Skip to content

Commit

Permalink
Merge pull request #92 from panoratech/feat/fix-apis
Browse files Browse the repository at this point in the history
Feat/fix apis
  • Loading branch information
naelob authored Nov 23, 2023
2 parents f581d54 + 3ce6683 commit 3f8e148
Show file tree
Hide file tree
Showing 16 changed files with 259 additions and 94 deletions.
5 changes: 5 additions & 0 deletions packages/api/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,8 @@ model linked_users {
@@index([id_project], map: "fk_proectid_linked_users")
}

model crm_users {
id_crm_user String @id(map: "pk_test") @db.Uuid
test String
}
2 changes: 2 additions & 0 deletions packages/api/src/@core/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { JwtStrategy } from './strategies/jwt.strategy';
import { ApiKeyStrategy } from './strategies/auth-header-api-key.strategy';
import { PrismaService } from '../prisma/prisma.service';
import { ConfigService } from '@nestjs/config';
import { LoggerService } from '../logger/logger.service';

@Module({
providers: [
Expand All @@ -16,6 +17,7 @@ import { ConfigService } from '@nestjs/config';
ApiKeyStrategy,
PrismaService,
ConfigService,
LoggerService,
],
imports: [
PassportModule,
Expand Down
22 changes: 12 additions & 10 deletions packages/api/src/@core/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ import { CreateUserDto, LoginCredentials } from './dto/create-user.dto';
import { PrismaService } from '../prisma/prisma.service';
import * as bcrypt from 'bcrypt';
import * as crypto from 'crypto';
import { LoggerService } from '../logger/logger.service';

//TODO: Ensure the JWT is used for user session authentication and that it's short-lived.
@Injectable()
export class AuthService {
constructor(private prisma: PrismaService, private jwtService: JwtService) {}
constructor(
private prisma: PrismaService,
private jwtService: JwtService,
private logger: LoggerService,
) {}

async register(user: CreateUserDto) {
try {
Expand Down Expand Up @@ -97,12 +102,12 @@ export class AuthService {
return Boolean(api_key);
}*/

//must be called only if user is logged in
async generateApiKey(
projectId: number,
userId: number,
): Promise<{ access_token: string }> {
console.log("'ddddd");
const secret = process.env.JWT_SECRET;
const jwtPayload = {
sub: userId,
projectId: projectId,
Expand All @@ -120,9 +125,8 @@ export class AuthService {
projectId: number,
): Promise<{ api_key: string }> {
try {
console.log('here is my userId ', userId);
//tmp:
const resp = await this.prisma.organizations.create({
//tmp create first these 2 :
/*const resp = await this.prisma.organizations.create({
data: {
name: 'org1',
stripe_customer_id: 'oneone',
Expand All @@ -133,7 +137,7 @@ export class AuthService {
name: 'proj',
id_organization: resp.id_organization,
},
});
});*/
//TODO: CHECK IF PROJECT_ID IS EXISTENT
//fetch user_id
const foundUser = await this.prisma.users.findUnique({
Expand Down Expand Up @@ -162,7 +166,6 @@ export class AuthService {
if (!new_api_key) {
throw new UnauthorizedException('api keys issue to add to db');
}
console.log('.....');

return { api_key: access_token };
} catch (error) {
Expand All @@ -186,15 +189,14 @@ export class AuthService {
if (!saved_api_key) {
throw new UnauthorizedException('Failed to fetch API key from DB');
}

if (decoded.projectId !== saved_api_key.id_project) {
if (Number(decoded.projectId) !== Number(saved_api_key.id_project)) {
throw new UnauthorizedException(
'Failed to validate API key: projectId invalid.',
);
}

// Validate that the JWT payload matches the provided userId and projectId
if (decoded.sub !== saved_api_key.id_user) {
if (Number(decoded.sub) !== Number(saved_api_key.id_user)) {
throw new UnauthorizedException(
'Failed to validate API key: userId invalid.',
);
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/@core/connections/connections.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ import { LoggerService } from '../logger/logger.service';
@Module({
controllers: [ConnectionsController],
imports: [CrmConnectionModule],
providers: [LoggerService],
})
export class ConnectionsModule {}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CrmObject } from 'src/crm/@types';
import { CrmObject, FreshsalesContactOutput } from 'src/crm/@types';
import { CrmObjectInput, Unified, UnifySourceType } from '../../../types';
import { mapToFreshsalesContact, mapToUnifiedContact } from './mappers/contact';
import { UnifiedContactInput } from 'src/crm/contact/dto/create-contact.dto';

export async function desunifyFreshsales<T extends Unified>({
sourceObject,
Expand All @@ -11,7 +12,7 @@ export async function desunifyFreshsales<T extends Unified>({
}): Promise<CrmObjectInput> {
switch (targetType_) {
case CrmObject.contact:
return mapToFreshsalesContact(sourceObject);
return mapToFreshsalesContact(sourceObject as UnifiedContactInput);
case CrmObject.deal:
//return mapToHubspotDeal(sourceObject);
case CrmObject.company:
Expand All @@ -26,7 +27,9 @@ export async function unifyFreshsales<
>({ sourceObject, targetType_ }: { sourceObject: T; targetType_: CrmObject }) {
switch (targetType_) {
case CrmObject.contact:
return mapToUnifiedContact(sourceObject);
return mapToUnifiedContact(
sourceObject as FreshsalesContactOutput | FreshsalesContactOutput[],
);
case CrmObject.deal:
//return mapToHubspotDeal(sourceObject);
case CrmObject.company:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,63 @@
import { FreshsalesContactInput, HubspotContactInput } from 'src/crm/@types';
import {
FreshsalesContactInput,
FreshsalesContactOutput,
} from 'src/crm/@types';
import {
UnifiedContactInput,
UnifiedContactOutput,
} from 'src/crm/contact/dto/create-contact.dto';
import { Unified, UnifySourceType } from '../../../../types';

export function mapToFreshsalesContact<T extends Unified>(
source: T,
export function mapToFreshsalesContact(
source: UnifiedContactInput,
): FreshsalesContactInput {
const source_ = source as UnifiedContactInput;
// Assuming 'email_addresses' array contains at least one email and 'phone_numbers' array contains at least one phone number
const primaryEmail = source_.email_addresses?.[0]?.email_address;
const primaryPhone = source_.phone_numbers?.[0]?.phone_number;
const primaryEmail = source.email_addresses?.[0]?.email_address;
const primaryPhone = source.phone_numbers?.[0]?.phone_number;

return {
first_name: source_.first_name,
last_name: source_.last_name,
first_name: source.first_name,
last_name: source.last_name,
mobile_number: primaryPhone,
};
}

//TODO
export function mapToUnifiedContact<
T extends UnifySourceType | UnifySourceType[],
>(source: T): UnifiedContactOutput | UnifiedContactOutput[] {
return;
export function mapToUnifiedContact(
source: FreshsalesContactOutput | FreshsalesContactOutput[],
): UnifiedContactOutput | UnifiedContactOutput[] {
// Handling single FreshsalesContactOutput
if (!Array.isArray(source)) {
return _mapSingleFreshsalesContact(source);
}

// Handling array of FreshsalesContactOutput
return source.map(_mapSingleFreshsalesContact);
}

function _mapSingleFreshsalesContact(
contact: FreshsalesContactOutput,
): UnifiedContactOutput {
// Map email and phone details
const email_addresses = contact.email
? [{ email_address: contact.email, email_address_type: 'primary' }]
: [];
const phone_numbers = [];
if (contact.work_number) {
phone_numbers.push({
phone_number: contact.work_number,
phone_type: 'work',
});
}
if (contact.mobile_number) {
phone_numbers.push({
phone_number: contact.mobile_number,
phone_type: 'mobile',
});
}

return {
first_name: contact.first_name,
last_name: contact.last_name,
email_addresses,
phone_numbers,
};
}
9 changes: 6 additions & 3 deletions packages/api/src/@core/utils/unification/crm/hubspot/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CrmObject } from 'src/crm/@types';
import { CrmObject, HubspotContactOutput } from 'src/crm/@types';
import { CrmObjectInput, Unified, UnifySourceType } from '../../../types';
import { mapToHubspotContact, mapToUnifiedContact } from './mappers/contact';
import { UnifiedContactInput } from 'src/crm/contact/dto/create-contact.dto';

export async function desunifyHubspot<T extends Unified>({
sourceObject,
Expand All @@ -11,7 +12,7 @@ export async function desunifyHubspot<T extends Unified>({
}): Promise<CrmObjectInput> {
switch (targetType_) {
case CrmObject.contact:
return mapToHubspotContact(sourceObject);
return mapToHubspotContact(sourceObject as UnifiedContactInput);
case CrmObject.deal:
//return mapToHubspotDeal(sourceObject);
case CrmObject.company:
Expand All @@ -26,7 +27,9 @@ export async function unifyHubspot<
>({ sourceObject, targetType_ }: { sourceObject: T; targetType_: CrmObject }) {
switch (targetType_) {
case CrmObject.contact:
return mapToUnifiedContact(sourceObject);
return mapToUnifiedContact(
sourceObject as HubspotContactOutput | HubspotContactOutput[],
);
case CrmObject.deal:
//return mapToHubspotDeal(sourceObject);
case CrmObject.company:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,46 @@
import { HubspotContactInput } from 'src/crm/@types';
import { HubspotContactInput, HubspotContactOutput } from 'src/crm/@types';
import {
UnifiedContactInput,
UnifiedContactOutput,
} from 'src/crm/contact/dto/create-contact.dto';
import { Unified, UnifySourceType } from '../../../../types';

export function mapToHubspotContact<T extends Unified>(
source: T,
export function mapToHubspotContact(
source: UnifiedContactInput,
): HubspotContactInput {
const source_ = source as UnifiedContactInput;
// Assuming 'email_addresses' array contains at least one email and 'phone_numbers' array contains at least one phone number
const primaryEmail = source_.email_addresses?.[0]?.email_address;
const primaryPhone = source_.phone_numbers?.[0]?.phone_number;
const primaryEmail = source.email_addresses?.[0]?.email_address;
const primaryPhone = source.phone_numbers?.[0]?.phone_number;

return {
firstname: source_.first_name,
lastname: source_.last_name,
firstname: source.first_name,
lastname: source.last_name,
email: primaryEmail,
phone: primaryPhone,
// Map other fields as needed
// If there are fields such as city, country, etc., in your UnifiedContactInput, map them here
};
}

//TODO
export function mapToUnifiedContact<
T extends UnifySourceType | UnifySourceType[],
>(source: T): UnifiedContactOutput | UnifiedContactOutput[] {
return;
export function mapToUnifiedContact(
source: HubspotContactOutput | HubspotContactOutput[],
): UnifiedContactOutput | UnifiedContactOutput[] {
if (!Array.isArray(source)) {
return _mapSingleContact(source);
}

// Handling array of HubspotContactOutput
return source.map(_mapSingleContact);
}

function _mapSingleContact(
contact: HubspotContactOutput,
): UnifiedContactOutput {
return {
first_name: contact.firstname,
last_name: contact.lastname,
email_addresses: [
{ email_address: contact.email, email_address_type: 'primary' },
],
phone_numbers: [{ phone_number: contact.phone, phone_type: 'primary' }],
};
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CrmObject } from 'src/crm/@types';
import { CrmObject, PipedriveContactOutput } from 'src/crm/@types';
import { CrmObjectInput, Unified, UnifySourceType } from '../../../types';
import { mapToPipedriveContact, mapToUnifiedContact } from './mappers/contact';
import { UnifiedContactInput } from 'src/crm/contact/dto/create-contact.dto';

export async function desunifyPipedrive<T extends Unified>({
sourceObject,
Expand All @@ -11,7 +12,7 @@ export async function desunifyPipedrive<T extends Unified>({
}): Promise<CrmObjectInput> {
switch (targetType_) {
case CrmObject.contact:
return mapToPipedriveContact(sourceObject);
return mapToPipedriveContact(sourceObject as UnifiedContactInput);
case CrmObject.deal:
//return mapToHubspotDeal(sourceObject);
case CrmObject.company:
Expand All @@ -26,7 +27,9 @@ export async function unifyPipedrive<
>({ sourceObject, targetType_ }: { sourceObject: T; targetType_: CrmObject }) {
switch (targetType_) {
case CrmObject.contact:
return mapToUnifiedContact(sourceObject);
return mapToUnifiedContact(
sourceObject as PipedriveContactOutput | PipedriveContactOutput[],
);
case CrmObject.deal:
//return mapToHubspotDeal(sourceObject);
case CrmObject.company:
Expand Down
Loading

0 comments on commit 3f8e148

Please sign in to comment.