/home/bdqbpbxa/dev-subdomains/admin.pixory.goodface.com.ua/src/klaviyo/klaviyo.service.ts
import { env } from 'src/config/env';
import { ApiKeySession, DataPrivacyApi, EventsApi, ProfileEnum, ProfilesApi } from 'klaviyo-api'
import { CreateProfileDto } from './types/createProfile.dto';
import { UpdateProfileDto } from './types/updateProfile.dto';
export class KlaviyoService {
private session = new ApiKeySession(env.klaviyo.publicKey);
private profilesApi = new ProfilesApi(this.session);
private dataPrivacyApi = new DataPrivacyApi(this.session);
private eventsApi = new EventsApi(this.session);
public async createProfile(dto: CreateProfileDto) : Promise<string> {
let profile = await this.profilesApi.createProfile({
data: {
type: ProfileEnum.Profile,
attributes: {
email: dto.email,
externalId: dto.externalId,
firstName: dto.firstName,
lastName: dto.lastName
}
}
})
return profile.body.data.id
}
public async updateProfile(id: string, dto: UpdateProfileDto) : Promise<number> {
let profile = await this.profilesApi.updateProfile(id, {
data: {
id: id,
type: "profile",
attributes: {
email: dto.email,
firstName: dto.firstName,
lastName: dto.lastName,
phoneNumber: dto.phoneNumber
}
}
})
return profile.response.status
}
public async deleteProfile(id: string) : Promise<number> {
let profile = await this.profilesApi.getProfile(id)
if(!profile) throw new Error("Unknown Profile provided")
let req = await this.dataPrivacyApi.requestProfileDeletion({
data: {
type: "data-privacy-deletion-job",
attributes: {
profile: {
data: {
id,
attributes: {},
type: "profile"
}
}
}
}
})
return req.response.status
}
public async createEvent(dto) {
let event = await this.eventsApi.createEvent({
data: {
type: "event",
attributes: {
properties: {
action: "Send 3D preview"
},
metric: {
data: {
type: "metric",
attributes: {
name: "Send 3D preview"
}
}
},
profile: {
data: {
attributes: {
email: dto.email
},
type: "profile"
}
}
}
}
})
}
}