generated from PolymeshAssociation/typescript-boilerplate
/
modifyVenue.ts
111 lines (95 loc) · 2.35 KB
/
modifyVenue.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import BigNumber from 'bignumber.js';
import { PolymeshError, Procedure, Venue } from '~/internal';
import { ErrorCode, RoleType, TxTags, VenueType } from '~/types';
import { ProcedureAuthorization } from '~/types/internal';
import { numberToU64, stringToVenueDetails, venueTypeToMeshVenueType } from '~/utils/conversion';
export type ModifyVenueParams =
| {
description?: string;
type: VenueType;
}
| {
description: string;
type?: VenueType;
}
| {
description: string;
type: VenueType;
};
/**
* @hidden
*/
export type Params = { venueId: BigNumber } & ModifyVenueParams;
/**
* @hidden
*/
export async function prepareModifyVenue(
this: Procedure<Params, void>,
args: Params
): Promise<void> {
const {
context: {
polymeshApi: { tx },
},
context,
} = this;
const { venueId, description, type } = args;
const venue = new Venue({ id: venueId }, context);
const { description: currentDescription, type: currentType } = await venue.details();
if (currentDescription === description) {
throw new PolymeshError({
code: ErrorCode.ValidationError,
message: 'New description is the same as the current one',
});
}
if (currentType === type) {
throw new PolymeshError({
code: ErrorCode.ValidationError,
message: 'New type is the same as the current one',
});
}
if (description) {
this.addTransaction(
tx.settlement.updateVenueDetails,
{},
numberToU64(venueId, context),
stringToVenueDetails(description, context)
);
}
if (type) {
this.addTransaction(
tx.settlement.updateVenueType,
{},
numberToU64(venueId, context),
venueTypeToMeshVenueType(type, context)
);
}
}
/**
* @hidden
*/
export function getAuthorization(
this: Procedure<Params, void>,
{ venueId, description, type }: Params
): ProcedureAuthorization {
const transactions = [];
if (description) {
transactions.push(TxTags.settlement.UpdateVenueDetails);
}
if (type) {
transactions.push(TxTags.settlement.UpdateVenueType);
}
return {
roles: [{ type: RoleType.VenueOwner, venueId }],
permissions: {
tokens: [],
portfolios: [],
transactions,
},
};
}
/**
* @hidden
*/
export const modifyVenue = (): Procedure<Params, void> =>
new Procedure(prepareModifyVenue, getAuthorization);