Skip to content

Commit 112a157

Browse files
committed
restructure platform
1 parent 0f11bf9 commit 112a157

10 files changed

+340
-37
lines changed

server/prisma/schema.prisma

+4-29
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ model User {
1616
created_at DateTime? @default(now())
1717
updated_at DateTime? @updatedAt
1818
wallets Wallet[]
19-
postsTweets PostTweets[]
20-
postsInstagram PostInstagram[]
21-
postsLinkedIn PostLinkedIn[]
19+
posts Posts[]
2220
platformAccounts PlatformAccount[]
2321
}
2422

@@ -62,36 +60,13 @@ enum PostStatus {
6260
CANCELED
6361
}
6462

65-
model PostTweets {
63+
model Posts {
6664
id Int @id @default(autoincrement())
6765
content String
68-
img String?
69-
scheduled_at DateTime
70-
status PostStatus
71-
created_at DateTime? @default(now())
72-
updated_at DateTime? @updatedAt
73-
user_id Int
74-
user User? @relation(fields: [user_id], references: [id])
75-
}
76-
77-
model PostInstagram {
78-
id Int @id @default(autoincrement())
79-
content String
80-
img String?
81-
scheduled_at DateTime
82-
status PostStatus
83-
created_at DateTime? @default(now())
84-
updated_at DateTime? @updatedAt
85-
user_id Int
86-
user User? @relation(fields: [user_id], references: [id])
87-
}
88-
89-
model PostLinkedIn {
90-
id Int @id @default(autoincrement())
91-
content String
92-
img String?
66+
image String?
9367
scheduled_at DateTime
9468
status PostStatus
69+
platform Platforms
9570
created_at DateTime? @default(now())
9671
updated_at DateTime? @updatedAt
9772
user_id Int

server/src/lib/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./input";

server/src/lib/input.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { ApiProperty } from "@nestjs/swagger";
2+
3+
export class Input<T> {
4+
@ApiProperty()
5+
public readonly payload?: T;
6+
7+
public constructor(payload: T) {
8+
this.payload = payload;
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
import { Injectable } from "@nestjs/common";
2+
import { LoggerService, PrismaService } from "../common";
3+
import { $Enums } from "@prisma/client";
4+
import { Input } from "../../lib";
5+
6+
type ICreateAccount = {
7+
platform: $Enums.Platforms;
8+
name: string;
9+
email?: string;
10+
password?: string;
11+
username?: string;
12+
api_key?: string;
13+
api_secret?: string;
14+
userId?: number;
15+
};
16+
type IUpdateAccount = {
17+
platform: $Enums.Platforms;
18+
name: string;
19+
email?: string;
20+
password?: string;
21+
username?: string;
22+
api_key?: string;
23+
api_secret?: string;
24+
userId?: number;
25+
platformId?: number;
26+
};
27+
type IGetAccount = {
28+
platform: $Enums.Platforms;
29+
userId?: number;
30+
};
31+
type IGetOneAccount = {
32+
platform: $Enums.Platforms;
33+
userId?: number;
34+
platformId?: number;
35+
};
36+
type IDeleteAccount = {
37+
userId?: number;
38+
platformId?: number;
39+
};
40+
41+
@Injectable()
42+
export class PlatformManagerService {
43+
private prisma: PrismaService;
44+
45+
public constructor(private readonly logger: LoggerService) {}
46+
47+
public async createAccount(payload: Input<ICreateAccount>) {
48+
this.logger.info("Creating platform account ...");
49+
const p = payload.payload!;
50+
try {
51+
await this.prisma.platformAccount.create({
52+
data: {
53+
user_id: p.userId,
54+
name: p.name,
55+
platform: p.platform,
56+
api_key: p.api_key!,
57+
api_secret: p.api_secret,
58+
email: p.email,
59+
username: p.username,
60+
password: p.password,
61+
},
62+
});
63+
} catch (error: any) {
64+
this.logger.error(
65+
"Could not create platform account: " + error.message
66+
);
67+
}
68+
}
69+
70+
public async getAccounts(params: Input<IGetAccount>) {
71+
this.logger.info("Fetching platform accounts");
72+
const p = params.payload!;
73+
try {
74+
const accounts = await this.prisma.platformAccount.findMany({
75+
where: {
76+
user_id: p.userId!,
77+
platform: p.platform,
78+
},
79+
});
80+
return accounts;
81+
} catch (error: any) {
82+
this.logger.error(
83+
"Could not fetch platform account: " + error.message
84+
);
85+
}
86+
}
87+
public async getOneAccount(params: Input<IGetOneAccount>) {
88+
this.logger.info("Fetching platform one account");
89+
const p = params.payload!;
90+
try {
91+
const accounts = await this.prisma.platformAccount.findMany({
92+
where: {
93+
user_id: p.userId!,
94+
platform: p.platform,
95+
id: p.platformId,
96+
},
97+
});
98+
return accounts;
99+
} catch (error: any) {
100+
this.logger.error(
101+
"Could not fetch platform account: " + error.message
102+
);
103+
}
104+
}
105+
public async updateAccount(params: Input<IUpdateAccount>) {
106+
this.logger.info("Updating platform one account");
107+
const p = params.payload!;
108+
try {
109+
const accounts = await this.prisma.platformAccount.update({
110+
where: {
111+
id: p.platformId,
112+
},
113+
data: {
114+
user_id: p.userId,
115+
name: p.name,
116+
platform: p.platform,
117+
api_key: p.api_key!,
118+
api_secret: p.api_secret,
119+
email: p.email,
120+
username: p.username,
121+
password: p.password,
122+
},
123+
});
124+
return accounts;
125+
} catch (error: any) {
126+
this.logger.error(
127+
"Could not fetch platform account: " + error.message
128+
);
129+
}
130+
}
131+
public async deleteAccount(params: Input<IDeleteAccount>) {
132+
this.logger.info("Deleting platform");
133+
const p = params.payload!;
134+
try {
135+
const accounts = await this.prisma.platformAccount.delete({
136+
where: {
137+
user_id: p.userId!,
138+
id: p.platformId,
139+
},
140+
});
141+
return accounts;
142+
} catch (error: any) {
143+
this.logger.error(
144+
"Could not delete platform account: " + error.message
145+
);
146+
}
147+
}
148+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
import { Injectable } from "@nestjs/common";
2+
import { LoggerService, PrismaService } from "../common";
3+
import { $Enums } from "@prisma/client";
4+
import { Input } from "../../lib";
5+
6+
type ICreatePost = {
7+
platform: $Enums.Platforms;
8+
content: string;
9+
imageUrl?: string;
10+
scheduled_at?: Date;
11+
userId?: number;
12+
};
13+
type IUpdatePost = {
14+
platform: $Enums.Platforms;
15+
content: string;
16+
imageUrl?: string;
17+
scheduled_at?: Date | "INSTANT";
18+
userId?: number;
19+
postId?: number;
20+
};
21+
type IGetPosts = {
22+
platform: $Enums.Platforms;
23+
userId?: number;
24+
};
25+
type IGetOnePost = {
26+
platform: $Enums.Platforms;
27+
userId?: number;
28+
postId?: number;
29+
};
30+
type IDeletePost = {
31+
userId?: number;
32+
postId?: number;
33+
};
34+
35+
@Injectable()
36+
export class PlatformPostsService {
37+
private prisma: PrismaService;
38+
39+
public constructor(private readonly logger: LoggerService) {}
40+
41+
public async instantPost(payload: Input<ICreatePost>) {
42+
this.logger.info("Creating platform account ...");
43+
const p = payload.payload!;
44+
try {
45+
await this.prisma.posts.create({
46+
data: {
47+
user_id: p.userId,
48+
platform: p.platform,
49+
content: p.content!,
50+
image: p.imageUrl,
51+
scheduled_at: "new Date.now()",
52+
status: "COMPLETED",
53+
},
54+
});
55+
} catch (error: any) {
56+
this.logger.error(
57+
"Could not create platform account: " + error.message
58+
);
59+
}
60+
}
61+
public async schedulePost(payload: Input<ICreatePost>) {
62+
this.logger.info("Creating platform account ...");
63+
const p = payload.payload!;
64+
try {
65+
await this.prisma.platformAccount.create({
66+
data: {
67+
user_id: p.userId,
68+
platform: p.platform,
69+
content: p.content!,
70+
image: p.imageUrl,
71+
scheduled_at: "new Date.now()",
72+
status: "",
73+
},
74+
});
75+
} catch (error: any) {
76+
this.logger.error(
77+
"Could not create platform account: " + error.message
78+
);
79+
}
80+
}
81+
82+
public async getAll(params: Input<IGetPosts>) {
83+
this.logger.info("Fetching platform accounts");
84+
const p = params.payload!;
85+
try {
86+
const accounts = await this.prisma.platformAccount.findMany({
87+
where: {
88+
user_id: p.userId!,
89+
platform: p.platform,
90+
},
91+
});
92+
return accounts;
93+
} catch (error: any) {
94+
this.logger.error(
95+
"Could not fetch platform account: " + error.message
96+
);
97+
}
98+
}
99+
public async getOne(params: Input<IGetOnePost>) {
100+
this.logger.info("Fetching platform one account");
101+
const p = params.payload!;
102+
try {
103+
const accounts = await this.prisma.platformAccount.findMany({
104+
where: {
105+
user_id: p.userId!,
106+
platform: p.platform,
107+
id: p.postId,
108+
},
109+
});
110+
return accounts;
111+
} catch (error: any) {
112+
this.logger.error(
113+
"Could not fetch platform account: " + error.message
114+
);
115+
}
116+
}
117+
public async updateAccount(params: Input<IUpdatePost>) {
118+
this.logger.info("Updating platform one account");
119+
const p = params.payload!;
120+
try {
121+
const accounts = await this.prisma.platformAccount.update({
122+
where: {
123+
id: p.postId,
124+
},
125+
data: {
126+
user_id: p.userId,
127+
name: p.name,
128+
platform: p.platform,
129+
api_key: p.api_key!,
130+
api_secret: p.api_secret,
131+
email: p.email,
132+
username: p.username,
133+
password: p.password,
134+
},
135+
});
136+
return accounts;
137+
} catch (error: any) {
138+
this.logger.error(
139+
"Could not fetch platform account: " + error.message
140+
);
141+
}
142+
}
143+
public async deleteAccount(params: Input<IDeletePost>) {
144+
this.logger.info("Deleting platform");
145+
const p = params.payload!;
146+
try {
147+
const accounts = await this.prisma.platformAccount.delete({
148+
where: {
149+
user_id: p.userId!,
150+
id: p.postId,
151+
},
152+
});
153+
return accounts;
154+
} catch (error: any) {
155+
this.logger.error(
156+
"Could not delete platform account: " + error.message
157+
);
158+
}
159+
}
160+
}

0 commit comments

Comments
 (0)