-
Notifications
You must be signed in to change notification settings - Fork 0
/
MA.ts
95 lines (90 loc) · 3.05 KB
/
MA.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* add headers here */
import { omit } from 'lodash';
import * as mongoose from 'mongoose';
import {
connectionSearch,
elasticCreateCross,
elasticUpdateCross,
} from '../ElasticSearch';
import { formatArgs } from '../Pagination/types/PaginationType';
import MarketingActivityWechatGroupModel from './MarketingActivityWechatGroupModel';
import { IMarketingActivityWechatGroupInputType } from './types/MarketingActivityWechatGroupInputType';
class MarketingActivityWechatGroup {
public static async find(_: Common.Viewer, args: object) {
return MarketingActivityWechatGroupModel.find(args);
}
public static async findOne(_: Common.Viewer, args: object) {
return MarketingActivityWechatGroupModel.findOne(args);
}
public static async findById(
_: Common.Viewer,
id: string | mongoose.Types.ObjectId,
) {
return MarketingActivityWechatGroupModel.findById(id);
}
public static async findRawById(id: string | mongoose.Types.ObjectId) {
const marketingActivityWechatGroup = await MarketingActivityWechatGroupModel.findById(id, '-__v');
if (!marketingActivityWechatGroup) {
throw new Error(/*add error message here*/);
}
const marketingActivityWechatGroupJson = {
...omit(marketingActivityWechatGroup.toJSON(), '_id'),
id: marketingActivityWechatGroup.id,
};
return marketingActivityWechatGroupJson;
}
public static async findCrossById(id: string | mongoose.Types.ObjectId) {
const marketingActivityWechatGroupJson = await this.findRawById(id);
return {
...marketingActivityWechatGroupJson,
};
}
public static async createOne(
viewer: Common.Viewer,
input: IMarketingActivityWechatGroupInputType,
) {
if (!viewer) {
throw new Error('Unauthorized');
}
try {
/* add restriction here */
const marketingActivityWechatGroupDoc = new MarketingActivityWechatGroupModel({
...input,
});
try {
const marketingActivityWechatGroup = await marketingActivityWechatGroupDoc.save();
await elasticCreateCross('marketingactivitywechatgroup', marketingActivityWechatGroupDoc.id);
return marketingActivityWechatGroup;
} catch (e) {
throw e;
}
} catch (e) {
throw e;
}
}
public static async updateById(
viewer: Common.Viewer,
id: string | mongoose.Types.ObjectId,
marketingActivityWechatGroupInput: IMarketingActivityWechatGroupInputType,
) {
const marketingActivityWechatGroup = await MarketingActivityWechatGroupModel.findByIdAndUpdate(
id,
{ $set: marketingActivityWechatGroupInput },
{ new: true },
);
await elasticUpdateCross('marketingactivitywechatgroup', id);
return marketingActivityWechatGroup;
}
public static async connectionSearch(
viewer: Common.Viewer,
args: Common.PageArgs,
) {
const newArgs = formatArgs(args);
const data = await connectionSearch(newArgs, {
index: 'marketingactivitywechatgroup',
type: 'marketingactivitywechatgroup',
});
return data;
}
}
export default MarketingActivityWechatGroup;